POJ2676-Sudoku(dfs)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2676-Sudoku(dfs)相关的知识,希望对你有一定的参考价值。


Description:

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task.

POJ2676-Sudoku(dfs)_git

Input

The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

Output

For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

Sample Input

1 103000509 002109400 000704000 300502006 060000050 700803004 000401000 009205800 804000107

Sample Output

143628579 572139468 986754231 391542786 468917352 725863914 237481695 619275843 854396127

数独问题

把一个9行9列的网格,再细分为9个3*3的子网格,要求每行、每列、每个子网格内都只能使用一次1~9中的一个数字,即每行、每列、每个子网格内都不允许出现相同的数字。

0是待填位置,其他均为已填入的数字。

要求填完九宫格并输出(如果有多种结果,则只需输出其中一种)

如果给定的九宫格无法按要求填出来,则输出原来所输入的未填的九宫格

解题思路:

DFS试探,失败则回溯

用三个数组进行标记每行、每列、每个子网格已用的数字,用于剪枝

bool row[10][10];    //row[i][x]  标记在第i行中数字x是否出现了

bool col[10][10];    //col[j][y]  标记在第j列中数字y是否出现了

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<vector>
#include<math.h>
const int INF = 0x3f3f3f3f;
using namespace std;
typedef long long ll;
typedef double ld;
struct point

int x, y;
p[100];
//row[i][j]表示第i行是否有数字j,col[i][j]表示第i列是否有数字j,
//mat[i][j][k]表示第(i,j)位置的小九宫格里是否有数字k
int sudoku[15][15];
bool flag, row[15][15], col[15][15], mat[3][3][15];
int cnt;
int i,j,k;
int t;
void dfs(int num)

if(flag)
return;
int i,j;
if(num==-1)

for(i=0; i<9&&!flag; i++)

for(j=0; j<9; j++)
printf("%d",sudoku[i][j]);
printf("\\n");

flag=true;
return;

for(i=1;i<=9&&!flag; i++)

int x=p[num].x;
int y=p[num].y;
if (!row[x][i]&&!col[y][i]&&!mat[x/3][y/3][i])

row[x][i]=col[y][i]=mat[x/3][y/3][i]=true;
sudoku[x][y]=i;
dfs(num-1);
row[x][i]=col[y][i]=mat[x/3][y/3][i]=false;
sudoku[x][y]=0;




int main()

scanf("%d",&t);
while(t--)

memset(row,false,sizeof(row));
memset(col,false,sizeof(col));
memset(mat,false,sizeof(mat));
cnt=0;
for (i=0; i<9; i++)

for (j=0; j<9; j++)

scanf("%1d",&sudoku[i][j]);
int num=sudoku[i][j];
if (num)
row[i][num]=col[j][num]=mat[i/3][j/3][num]=true;
else

p[cnt].x=i;
p[cnt].y=j;
cnt++;



flag=false;
dfs(cnt-1);

return 0;

 

 

 

以上是关于POJ2676-Sudoku(dfs)的主要内容,如果未能解决你的问题,请参考以下文章

POJ2676-Sudoku(dfs)

POJ2676 – Sudoku(数独)—DFS

POJ 2676 Sudoku (数独 DFS)

ACM : POJ 2676 SudoKu DFS - 数独

POJ2676 Sudoku

poj 2676 Sudoku