# 037 - Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character '.'.

You may assume that there will be only one unique solution.

A sudoku puzzle...

...and its solution numbers marked in red.

static int find = 0;
int locok(char **sudu, int x, int y, int num)
{
int i, j, i1, j1;
if (sudu[x][y] != '.') return 2;
/* check row */
for (i = 0; i < 9; i++)
if (i != x && sudu[i][y] == num)
return 0;
/* check col */
for (i = 0; i < 9; i++)
if (i != y && sudu[x][i] == num)
return 0;
/* check area */
i = x / 3 * 3;
j = y / 3 * 3;
i1 = i + 3;
j1 = j + 3;
for (i = i1 - 3; i < i1; i++)
for (j = j1 - 3; j < j1; j++){
if (i == x && j ==y) continue;
if (sudu[i][j] == num)
return 0;
}

return 1;
}

int xsudu(char *sudu[], int x, int y)
{
if (x > 8 || y > 8 || x < 0 || y < 0) return -1;
int i;
if (x == 8 && y == 8) {
for (i = 1; i < 10; i++) {
int ok = locok(sudu, x, y, i + '0');
if (!ok) continue;
if (ok == 1)
sudu[x][y] = i + '0';
return find = 1;
}
} else {
for (i = 1; i < 10; i++) {
int ok = locok(sudu, x, y, i + '0');
if (!ok) continue;
if (ok == 1) sudu[x][y] = i + '0';
xsudu(sudu, y == 8? x + 1:x, y == 8? 0:y + 1);
if (find) return 0;
if (ok == 1) sudu[x][y] = '.';
if (ok == 2) break;
}
}

return 0;
}

void solveSudoku(char** board, int boardRowSize, int boardColSize)
{
find = 0;
xsudu(board, 0, 0);
}

#### [LeetCode] 037. Sudoku Solver (Hard) (C++)

2015-03-13 19:33:27

#### Sudoku Solver -- LeetCode

2014-03-10 03:56:50

#### LeetCode 37 Sudoku Solver (C,C++,Java,Python)

2015-05-19 13:11:05

#### Leetcode 37 Sudoku Solver

2013-02-17 21:53:25

#### (Java)LeetCode-37. Sudoku Solver

2016-08-09 21:36:31

#### [leetcode-37]Sudoku Solver(java)

2015-09-01 22:20:17

#### LeetCode 037 Sudoku Solver

2016-05-21 13:18:43

#### 37. Sudoku Solver Leetcode Python

2015-03-27 04:58:37

#### 37. Sudoku Solver 回溯

2016-11-21 10:20:01

#### leetcode 037 —— Sudoku Solver

2015-07-15 21:42:53