POJ 2676 挺复杂的一道深搜

这道题之前就做了,一直没写题解,很不错的一道深搜题。这道题没什么剪枝优化,思路就是将空白格子的位置放入一个数组,然后用dfs尝试每个空白格子所放的数字。

Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15861   Accepted: 7753   Special Judge

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. 

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
 1 #include<iostream>
 2 #include<cstring>
 3 #include<vector>
 4 #include<algorithm>
 5 using namespace std;
 6 short row[9][10],col[9][10],block[9][10];//rowFlag[i][num]=1表示在第i行已经放了数字num,后2个分别表示列、块
 7 int board[9][10];
 8 struct pos
 9 {
10     int x,y;
11 }blank[81];
12 int Getnum(int x,int y)//状态压缩
13 {
14     int xx=x/3;
15     int yy=y/3;
16     return xx*3+yy;
17 }
18 void setallflag(int x,int y,int num,int f)
19 {
20     row[x][num]=f;
21     col[y][num]=f;
22     block[Getnum(x,y)][num]=f;
23 }
24 bool isok(int x,int y,int num)
25 {
26     return !row[x][num]&&!col[y][num]&&!block[Getnum(x,y)][num];
27 }
28 bool dfs(int n)
29 {
30     if(n<0) return true;
31     int x=blank[n].x;
32     int y=blank[n].y;
33     for(int num=1;num<=9;num++)
34     {
35         if(isok(x,y,num))
36         {
37             board[x][y]=num;
38             setallflag(x,y,num,1);
39             if(dfs(n-1))
40                 return true;
41             setallflag(x,y,num,0);
42         }
43     }
44     return false;
45 }
46 int main()
47 {
48     int t;
49     cin>>t;
50     while(t--)
51     {
52         memset(row,0,sizeof(row));
53         memset(col,0,sizeof(col));
54         memset(block,0,sizeof(block));
55         memset(blank,0,sizeof(blank));
56         int flag=0;
57         for(int i=0;i<9;i++)
58             for(int j=0;j<9;j++)
59         {
60             char c;
61             cin>>c;
62             board[i][j]=c-'0';
63             if(board[i][j])
64                 setallflag(i,j,board[i][j],1);
65             else
66             {
67                 blank[flag].x=i;
68                 blank[flag].y=j;
69                 flag++;
70             }
71         }
72         if(dfs(flag-1))
73         {
74             for(int i=0;i<9;i++)
75             {
76                 for(int j=0;j<9;j++)
77                     cout<<board[i][j];
78                 cout<<endl;
79             }
80         }
81     }
82     return 0;
83 }
View Code
原文地址:https://www.cnblogs.com/zero-zz/p/4704454.html