Sodoku classical backtracking
2017-01-21 06:37
246 查看
Sodoku classical backtracking
backtracking the same as 8 queens and prime number circle
//pass
#include<cstdio>
#include<iostream>
using namespace std;
#define N 9
int grid
= {{3, 0, 6, 5, 0, 8, 4, 0, 0},
{5, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 8, 7, 0, 0, 0, 0, 3, 1},
{0, 0, 3, 0, 1, 0, 0, 8, 0},
{9, 0, 0, 8, 6, 3, 0, 0, 5},
{0, 5, 0, 0, 9, 0, 6, 0, 0},
{1, 3, 0, 0, 0, 0, 2, 5, 0},
{0, 0, 0, 0, 0, 0, 0, 7, 4},
{0, 0, 5, 2, 0, 6, 3, 0, 0}};
/* Returns whether any assigned entry n the specified row matches
the given number. */
bool UsedInRow(int grid
, int row, int num)
{
for (int col = 0; col < N; col++)
if (grid[row][col] == num)
return true;
return false;
}
/* Returns whether any assigned entry in the specified column matches
the given number. */
bool UsedInCol(int grid
, int col, int num)
{
for (int row = 0; row < N; row++)
if (grid[row][col] == num)
return true;
return false;
}
/* Returns whether any assigned entry within the specified 3x3 box matches
the given number. */
bool UsedInBox(int grid
, int boxStartRow, int boxStartCol, int num)
{
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
if (grid[row+boxStartRow][col+boxStartCol] == num)
return true;
return false;
}
/* Returns whether it will be legal to assign num to the given row,col location.
*/
bool isSafe(int row, int col, int num)
{
return !UsedInRow(grid, row, num) && !UsedInCol(grid, col, num) &&
!UsedInBox(grid, row - row % 3 , col - col % 3, num);
}
bool SodokuSolver(int seq){
//int seq=row*9+col;
int row=seq/9;
int col=seq%9;
while(grid[row][col]!=0&&row<=8&&col<=8){
//move to next position
seq++;
row=seq/9;
col=seq%9;
}
//if row=9,col=0,it means we have finish its
if(seq==81) return true;//Indicate that we have finish all the 81 blocks
else{
//grid[row][col]==0,So we should try every number from 1-9 and check
for(int num=1;num<10;num++){
if(isSafe(row,col,num)){//if it is safe and not only it is safe it will finish the whole bolck. if it can not fisnish the whole block ,it must roll back
//move to next position
grid[row][col]=num;
if(SodokuSolver(seq+1)) return true;
row=seq/9;
col=seq%9;
grid[row][col]=0;
}
}
return false;
}
}
void printGrid(int grid
)
{
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
cout<<grid[row][col]<<" ";
cout<<endl;
}
}
int main()
{
if (SodokuSolver(0) == true)
printGrid(grid);
else
cout<<"No solution exists"<<endl;
return 0;
}
backtracking the same as 8 queens and prime number circle
//pass
#include<cstdio>
#include<iostream>
using namespace std;
#define N 9
int grid
= {{3, 0, 6, 5, 0, 8, 4, 0, 0},
{5, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 8, 7, 0, 0, 0, 0, 3, 1},
{0, 0, 3, 0, 1, 0, 0, 8, 0},
{9, 0, 0, 8, 6, 3, 0, 0, 5},
{0, 5, 0, 0, 9, 0, 6, 0, 0},
{1, 3, 0, 0, 0, 0, 2, 5, 0},
{0, 0, 0, 0, 0, 0, 0, 7, 4},
{0, 0, 5, 2, 0, 6, 3, 0, 0}};
/* Returns whether any assigned entry n the specified row matches
the given number. */
bool UsedInRow(int grid
, int row, int num)
{
for (int col = 0; col < N; col++)
if (grid[row][col] == num)
return true;
return false;
}
/* Returns whether any assigned entry in the specified column matches
the given number. */
bool UsedInCol(int grid
, int col, int num)
{
for (int row = 0; row < N; row++)
if (grid[row][col] == num)
return true;
return false;
}
/* Returns whether any assigned entry within the specified 3x3 box matches
the given number. */
bool UsedInBox(int grid
, int boxStartRow, int boxStartCol, int num)
{
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
if (grid[row+boxStartRow][col+boxStartCol] == num)
return true;
return false;
}
/* Returns whether it will be legal to assign num to the given row,col location.
*/
bool isSafe(int row, int col, int num)
{
return !UsedInRow(grid, row, num) && !UsedInCol(grid, col, num) &&
!UsedInBox(grid, row - row % 3 , col - col % 3, num);
}
bool SodokuSolver(int seq){
//int seq=row*9+col;
int row=seq/9;
int col=seq%9;
while(grid[row][col]!=0&&row<=8&&col<=8){
//move to next position
seq++;
row=seq/9;
col=seq%9;
}
//if row=9,col=0,it means we have finish its
if(seq==81) return true;//Indicate that we have finish all the 81 blocks
else{
//grid[row][col]==0,So we should try every number from 1-9 and check
for(int num=1;num<10;num++){
if(isSafe(row,col,num)){//if it is safe and not only it is safe it will finish the whole bolck. if it can not fisnish the whole block ,it must roll back
//move to next position
grid[row][col]=num;
if(SodokuSolver(seq+1)) return true;
row=seq/9;
col=seq%9;
grid[row][col]=0;
}
}
return false;
}
}
void printGrid(int grid
)
{
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
cout<<grid[row][col]<<" ";
cout<<endl;
}
}
int main()
{
if (SodokuSolver(0) == true)
printGrid(grid);
else
cout<<"No solution exists"<<endl;
return 0;
}
相关文章推荐
- 【Leetcode】Permutations (Backtracking)
- (Leetcode)39&40. Combination Sum--Using Backtracking
- (M)Backtracking:40. Combination Sum II
- 回溯线搜索 Backtracking line search
- 【Leetcode】Permutations II (Backtracking)
- Sudoku Solver Backtracking
- The backtracking algorithm
- 17. Letter Combinations of a Phone Number (backtracking)
- (M)Backtracking:131. Palindrome Partitioning
- 【Leetcode】Word Ladder II (Backtracking)
- Backtracking questions
- General Approach for backtracking problem
- (M)Backtracking:47. Permutations II
- Sudoku backtracking with one dimension array
- 【Leetcode】Generate Parentheses (Backtracking)
- 【原创】回溯线搜索 Backtracking line search
- Backtracking line search的理解
- 【Leetcode】N-Queens II (Backtracking)
- 回溯算法(BackTracking)--八皇后问题
- LeetCode随笔之backtracking