-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku Solver.cpp
77 lines (75 loc) · 2.06 KB
/
Sudoku Solver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*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. */
//思路:一共81个位置,每插入一个数,就验证一下这个数是否valid,如果试遍了1-9所有得数,都没有正确的
//那么就回溯到前一个位置,不断地尝试,回溯直到到达第81个位置
//难点是回溯的函数和检验valid这个函数怎么写
class Solution {
public:
bool check(vector<vector<char>>& board,int pos)
{
int row=pos/9;
int col=pos%9;
char tp=board[row][col];
for(int i=0;i<9;i++)//the row
{
if(i!=col)
{
if(board[row][i]==tp)
return false;
}
}
for(int i=0;i<9;i++)//the col
{
if(i!=row)
{
if(board[i][col]==tp)
return false;
}
}
int br=row/3*3;
int bc=col/3*3;
for(int i=br;i<br+3;i++)//the subgrid
{
for(int j=bc;j<bc+3;j++)
{
if(i!=row&&j!=col)
{
if(board[i][j]==tp)
return false;
}
}
}
return true;
}
bool solve(vector<vector<char>>& board,int pos)
{
if(pos==81)
return true;
int row=pos/9;
int col=pos%9;
if(board[row][col]=='.')
{
for(int i=1;i<10;i++)
{
board[row][col]=i+'0';
if(check(board,pos))
{
if(solve(board,pos+1))//如果失败的话,要把修改的点还原
return true;
}
board[row][col]='.';
}
}
else
{
if(solve(board,pos+1))
return true;
}
return false;
}
void solveSudoku(vector<vector<char>>& board)
{
solve(board,0);
}
};