Poj 2676 Sudoku[dfs]

题目大意:

九宫格问题,也有人叫数独问题

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

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

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

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

思路:

DFS 深搜

char map[10][10];/*数据存储*/
bool row[10][10];/*行存在数*/
bool col[10][10];/*列存在数*/
bool grid[10][10];/*格存在数*/

  1 /*poj2676 Sudoku*/
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cstring>
  5 using namespace std;
  6 
  7 char map[10][10];
  8 bool row[10][10];/*行存在数*/
  9 bool col[10][10];/*列存在数*/ 
 10 bool grid[10][10];/*格存在数*/
 11 
 12 bool dfs(int x, int y)
 13 {
 14     if(x == 10)
 15         return true;
 16     bool flag = false;
 17     if(map[x][y] - '0')
 18     {
 19         if(y == 9)
 20             flag = dfs(x+1, 1);
 21         else{
 22             flag = dfs(x, y+1);
 23         }
 24         if(flag)
 25             return true;
 26         else{
 27             return false;
 28         }
 29     }
 30     else{
 31         for(int i = 1; i<= 9; i++)
 32         {
 33             if(!row[x][i] && !col[y][i] && !grid[3*((x-1)/3) + (y-1)/3 + 1][i])
 34             {
 35                 map[x][y] = i+'0';
 36                 row[x][i] = true;
 37                 col[y][i] = true;
 38                 grid[3*((x-1)/3) + (y-1)/3 + 1][i] = true;
 39                 
 40                 if(y == 9)
 41                     flag = dfs(x+1, 1);
 42                 else{
 43                     flag = dfs(x, y+1);
 44                 }
 45                 if(!flag)
 46                 {
 47                     map[x][y] = '0';
 48                     row[x][i] = false;
 49                     col[y][i] = false;
 50                     grid[3*((x-1)/3) + (y-1)/3 + 1][i] = false;
 51                 
 52                 }
 53                 else{
 54                     return true;
 55                 }        
 56             }
 57         }
 58     }
 59     return false;
 60 }
 61 
 62 void judge()
 63 {
 64     memset(row, 0, sizeof(row));
 65     memset(col, 0, sizeof(col));
 66     memset(grid, 0, sizeof(grid));
 67 
 68     for(int i = 1; i<= 9; i++)
 69     {
 70         for(int j = 1; j<= 9; j++)
 71         {
 72             if(map[i][j] != '0')
 73             {
 74                 row[i][map[i][j]-'0'] = true;
 75                 col[j][map[i][j]-'0'] = true;
 76                 grid[3*((i-1)/3) + (j-1)/3 + 1][map[i][j]-'0'] = true;
 77             }
 78         }
 79     }
 80 /*    for(int i = 1; i<= 9; i++)
 81     {
 82         for(int j = 1; j<= 9; j++)
 83             cout<<grid[i][j];
 84         printf("
");
 85     }
 86     printf("
");    
 87 */    
 88 }
 89 
 90 void solve()
 91 {
 92     memset(map, 0, sizeof(map));
 93     for(int i = 1; i<= 9; i++)
 94     {
 95         gets(map[i] +1);
 96     }
 97     judge();
 98     dfs(1, 1);
 99     
100     for(int i = 1; i<= 9; i++)
101         puts(map[i] + 1);
102     printf("
");
103 }
104 
105 int main()
106 {
107     int t;
108 //    freopen("test.txt", "r", stdin);
109     scanf("%d", &t);
110     getchar();
111     while(t--)
112     {
113         solve();
114     }
115     return 0;
116 }
转载请注明出处:http://www.cnblogs.com/ygdblogs
原文地址:https://www.cnblogs.com/ygdblogs/p/4929849.html