-3

I recently wrote a simple straightforward C code that solves Sudoku and that uses recursion and backtracking. You will find below the code that I just described. Everything works fine.

#include <stdio.h>
# define INsigned 0

int grid[9][9];
void printGrid(){
int row,col;

for(row=0;row<9;row++){
    for(col=0;col<9;col++){
        printf("%2d",grid[row][col]);
        }
    printf("\n");
        }   
}

int Unsigned(int *row, int* col){
int i,j;
for(i=0;i<9;i++){
    for(j=0;j<9;j++){

        if(grid[i][j]==0){
            *row=i;
            *col=j;
            return 1;


        }
    }

}
return 0;

}
int check(){
int row=0;
int col=0;
int i;
if(Unsigned(&row, &col)==0)
    return 1;


for(i=1;i<=9;i++){
    if(conflict(i,row-row%3, col-col%3, row, col)==0){

        grid[row][col]=i;
        if(check()==1)
            return 1;
    }


    grid[row][col]=0;
}

return 0;
}

int conflict (int num, int srow, int scol, int row, int col){

int i,j;
for(i=0;i<9;i++){
    if(grid[row][i]==num) return 1;
    if(grid[i][col]==num) return 2;
}

for(i=0;i<3;i++){
    for(j=0;j<3;j++){
        if(grid[i+srow][j+scol]==num) return 3;
    }
}
return 0;

}   

int main(){

int i;

for(i=0;i<9;i++){
    scanf("%d %d %d %d %d %d %d %d %d",&grid[i][0],&grid[i][1],&grid[i][2],
                                       &grid[i][3],&grid[i][4],&grid[i][5],
                                       &grid[i][6],&grid[i][7],&grid[i][8]);
}

check();
printf("\n\n");
printGrid();

}

The input is :

0 0 0 0 0 9 0 0 3
0 0 5 0 0 0 0 9 0
3 0 0 8 1 0 2 0 0
0 0 0 0 4 3 1 0 0
5 6 0 0 9 0 0 7 8
0 0 3 5 6 0 0 0 0
9 0 6 0 8 7 0 0 4
0 2 0 0 0 0 8 0 0
1 0 0 9 0 0 0 0 0

So the problem that I have occurs when I tried to write the exact algorithm in java. Since java does not allow the pass by reference for primitive types and since I used the pass by reference in the Unsigned function that looks for the next empty cell. I decided to use a wrapper class in java in order to overcome this obstacle however the java equivalent of this code that not work as it should be Here is the code.

    public class intObj {

        public int value;
    }





public class Grid {

int grid[][] = new int[9][9];
int r = 0;
int c = 0;

public Grid(int grid[][]) {
    this.grid = grid;
}

void printGrid() {
    int row;
    int col;
    System.out.println("");
    for (row = 0; row < 9; row++) {
        for (col = 0; col < 9; col++) {
            System.out.printf("%2d", grid[row][col]);
        }
        System.out.println("");

    }
}

int conflict(int num, int srow, int scol, int row, int col) {

    int i, j;
    for (i = 0; i < 9; i++) {
        if (grid[row][i] == num) {
            return 1;
        }
        if (grid[i][col] == num) {
            return 2;
        }
    }

    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            if (grid[i + srow][j + scol] == num) {
                return 3;
            }
        }
    }
    return 0;

}

int Unsigned(intObj row, intObj col) {

    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            if (grid[i][j] == 0) {

                row.value = i;
                col.value = j;
                return 1;
            }
        }
    }
    return 0;

}

int check() {

    intObj row = new intObj();
    intObj col = new intObj();

    if (Unsigned(row, col) == 0) {
        return 1;
    }

    for (int i = 1; i <= 9; i++) {
        if (conflict(i, (row.value) - (row.value) % 3,
                (col.value) - (col.value) % 3, row.value, col.value) == 0) {


            grid[row.value][col.value] = i;
            if (check() == 1) {
                return 1;
            }

        }

        grid[row.value][col.value] = 0;
    }

    return 0;

}
}

The main class:

public class Main {

public static void main(String args[]) {

    int grid[][] = {{0, 0, 0, 0, 0, 9, 0, 0, 3},
        {0, 0, 5, 0, 0, 0, 0, 9, 3},
        {3, 0, 0, 8, 1, 0, 2, 0, 0},
        {0, 0, 0, 0, 4, 3, 1, 0, 0},
        {5, 6, 0, 0, 9, 0, 0, 7, 8},
        {0, 0, 3, 5, 6, 0, 0, 0, 0},
        {9, 0, 6, 0, 8, 7, 0, 0, 4},
        {0, 2, 0, 0, 0, 0, 8, 0, 0},
        {1, 0, 0, 9, 0, 0, 0, 0, 0}};

    Grid g = new Grid(grid);


    g.check();

    g.printGrid();

}
}

Sorry for the long post any help will be very much appreciated.

Mes M
  • 17
  • 2

1 Answers1

0

The simplest solution that I can think of Is "Move your unsigned code inside check() itself"

int check(){
int row=0;
int col=0;
int i;
if(Unsigned(&row, &col)==0) //Move Unsigned() method code here and that's it
    return 1;

II'nd Solution: These two objects that you are creating in check() method must be part of your Grid class,do not create them locally in check() method.It should resolve your problem

 intObj row = new intObj();
    intObj col = new intObj();
Algorithmist
  • 6,657
  • 7
  • 35
  • 49