package test;
public final class Shudu {
public static int GRID_NUM = 9;
public static byte[][] gameSudoku(byte[][] initBoard) {
// byte[][] resultBoard = new byte[GRID_NUM][GRID_NUM];
getResult(initBoard, 0, 0);
return initBoard;
}
public static boolean getResult(byte[][] board, int i, int j) {
while (i < GRID_NUM && j < GRID_NUM && board[i][j] != 0) {
j++;
if (j == GRID_NUM) {
j = 0;
i++;
}
}
if (i >= GRID_NUM)
return true;
byte v;
for (v = 1; v <= 9; v++) {
if (search(board, i, j, v)) {
board[i][j] = v;
if (getResult(board, i, j))
return true;
board[i][j] = 0;
}
}
return false;
}
private static boolean search(byte[][] board, int i, int j, byte v) {
byte k;
for (k = 0; k < GRID_NUM; k++) {
if (board[i][k] == v)
return false;
}
for (k = 0; k < GRID_NUM; k++) {
if (board[k][j] == v)
return false;
}
int nl = i - (i % 3);
int nr = j - (j % 3);
int nh = nl + 3;
int nw = nr + 3;
for (int x = nl; x < nh; x++)
for (int y = nr; y < nw; y++) {
if (board[x][y] == v)
return false;
}
return true;
}
}
public final class Shudu {
public static int GRID_NUM = 9;
public static byte[][] gameSudoku(byte[][] initBoard) {
// byte[][] resultBoard = new byte[GRID_NUM][GRID_NUM];
getResult(initBoard, 0, 0);
return initBoard;
}
public static boolean getResult(byte[][] board, int i, int j) {
while (i < GRID_NUM && j < GRID_NUM && board[i][j] != 0) {
j++;
if (j == GRID_NUM) {
j = 0;
i++;
}
}
if (i >= GRID_NUM)
return true;
byte v;
for (v = 1; v <= 9; v++) {
if (search(board, i, j, v)) {
board[i][j] = v;
if (getResult(board, i, j))
return true;
board[i][j] = 0;
}
}
return false;
}
private static boolean search(byte[][] board, int i, int j, byte v) {
byte k;
for (k = 0; k < GRID_NUM; k++) {
if (board[i][k] == v)
return false;
}
for (k = 0; k < GRID_NUM; k++) {
if (board[k][j] == v)
return false;
}
int nl = i - (i % 3);
int nr = j - (j % 3);
int nh = nl + 3;
int nw = nr + 3;
for (int x = nl; x < nh; x++)
for (int y = nr; y < nw; y++) {
if (board[x][y] == v)
return false;
}
return true;
}
}