POJ 2676 Sudoku

                                                                                     Sudoku

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6771   Accepted: 3277   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.
POJ 2676 - 地平线 - 超越地平线

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

Source

Southeastern Europe 2005

 

  1 #include <cstdlib>
  2 #include <iostream>
  3 #include <stdio.h>
  4 #include <string.h>
  5 using namespace std;
  6 int map[10][10];
  7 const int NUM=1;
  8 struct Node{
  9        int data[10];
 10        int visited[10];
 11        int rear;
 12        }q[10];
 13        
 14 void output()
 15 {
 16      int i,j;
 17      for(i=1;i<=9;i++)
 18      {for(j=1;j<=9;j++)
 19      cout<<map[i][j];
 20      cout<<endl;
 21      }
 22 }
 23 
 24 
 25 
 26 int check2()
 27 {int i,j,k,g;
 28 int t[10];
 29 int f[10][10];
 30 memset(f,0,sizeof(f));
 31  
 32  for(i=7;i>=1;i-=3)
 33 {
 34                   for(j=7;j>=1;j-=3)
 35                   {                 memset(t,0,sizeof(t));
 36                                     for(k=i;k<(i+3);k++)
 37                                     for(g=j;g<(j+3);g++)
 38                                     {t[map[k][g]]++;
 39                                      f[g][map[k][g]]++;
 40                                      if((f[g][map[k][g]]>1)&&(map[k][g]!=0))
 41                                      return 0;
 42                                      if((t[map[k][g]]>1)&&(map[k][g]!=0))
 43                                      return 0;
 44                                     }
 45                                     
 46                   }
 47 }
 48 
 49  return 1;
 50 }
 51 
 52 
 53     
 54 int dfs(int x,int y)
 55 {int i,j,k;
 56 
 57  if(y<NUM)
 58  {x--;y=9;}
 59  if(x<NUM)
 60  {
 61  if(check2())
 62  {output();return 1;}
 63  return 0;
 64  }
 65  
 66  
 67  
 68  
 69 if(map[x][y])
 70 {if(dfs(x,y-1))
 71  return 1;
 72  else
 73  return 0;
 74 }
 75 
 76 
 77 if(!check2())
 78 return 0; 
 79     
 80  
 81  
 82  
 83  
 84  for(i=0;i<q[x].rear;i++)
 85  {
 86                          if(q[x].visited[i])
 87                          continue;
 88                          
 89                         
 90                          map[x][y]=q[x].data[i];
 91                          q[x].visited[i]=1;
 92                         
 93                          if(dfs(x,y-1))
 94                           return 1;
 95                           map[x][y]=0;
 96                           q[x].visited[i]=0;
 97                        
 98  }
 99  return 0;
100 }
101 
102 
103 
104 
105 int main(int argc, char *argv[])
106 {
107 int n;
108 
109 scanf("%d",&n);
110 getchar();
111 int i,j,k;
112 char c;
113 int tag[10];
114 while(n--)
115 {
116           memset(map,0,sizeof(map));
117           
118           
119           
120           
121  for(i=1;i<=9;i++)
122  {q[i].rear=0;
123  memset(tag,0,sizeof(tag));
124  memset(q[i].visited,0,sizeof(q[i].visited));
125  for(j=1;j<=9;j++)
126  {scanf("%c",&c);
127   map[i][j]=(int)(c-'0');
128   tag[map[i][j]]=1;
129  }
130  getchar();
131  for(k=1;k<=9;k++)
132  {if(tag[k])
133   continue;
134   q[i].data[q[i].rear++]=k;
135  }
136  }
137  
138  dfs(9,9);
139  
140  
141  
142  
143           
144           
145           
146           
147           
148           
149 }
150           
151     system("PAUSE");
152     return EXIT_SUCCESS;
153 }
posted @ 2012-05-31 09:54  cseriscser  阅读(228)  评论(0编辑  收藏  举报