Sudoku (simple DFS)

Topic links : http://acm.hdu.edu.cn/showproblem.php?pid=5547

Data is relatively small, direct violence DFS, test cases can be established

AC codes: But I do not know why use scanf, printf input and output on the WA

  1 /* */
  2 # include <iostream>
  3 # include <stdio.h>
  4 # include <string.h>
  5 # include <cstdlib>
  6 # include <cmath>
  7 # include <climits>
  8 # include <deque>
  9 # include <queue>
 10 # include <stack>
 11 # include <vector>
 12 # include <map>
 13 # include <set>
 14 # include <ctime>
 15 # include <functional>
 16 using namespace std;
 17 typedef long long ll;
 18 const int inf=0x3f3f3f3f;
 19 const int maxn=5e4+50;
 20 const ll mod=1e9+7;
 21 char ma[25][25];
 22 int vis[25];
 23 int p[25][25];
 24 int tx[20];
 25 int ty[20];
26 is  int In Flag, TOT;
 27  
28  int Check ()
 29  {
 30      for ( int I = 0 ; I < . 4 ; I ++) /// examine each row 
31 is      {
 32          VIS [ . 1 ] = VIS [ 2 ] = VIS [ . 3 ] = VIS [ . 4 ] = 0 ;
 33 is          for ( int J = 0 ; J < . 4 ; J ++ )
 34 is          {
 35              IF (P [I] [J] == 0 )
36                  Continue ;
 37 [              IF (VIS [P [I] [J]]) /// fill the number of repeating is not satisfied 
38 is                  return  0 ;
 39              VIS [P [I] [J]] = . 1 ;
 40          }
 41 is      }
 42 is  
43 is      for ( int J = 0 ; J < . 4 ; J ++) /// checks each column 
44 is      {
 45          VIS [ . 1 ] = VIS [ 2 ] = VIS [ . 3 ] = VIS [ . 4 ] = 0 ;
 46 is          for (int i=0; i<4; i++ )
 47         {
 48             if( p[i][j]==0 )
 49                 continue;
 50             if( vis[p[i][j]] )
 51                 return 0;
 52                 vis[p[i][j]] = 1;
 53         }
 54     }
 55 
 56     vis[1] = vis[2] = vis[3] = vis[4] = 0;
 57     for(int= I 0 ; I < 2 ; I ++) /// top left quarter check block 
58      {
 59          for ( int J = 0 ; J < 2 ; J ++ )
 60          {
 61 is              IF (P [I] [J] = = 0 )
 62 is                  Continue ;
 63 is              IF (VIS [P [I] [J]])
 64                  return  0 ;
 65              VIS [P [I] [J]] = . 1 ;
 66          }
 67      }
 68  
69      VIS [ . 1] = VIS [ 2 ] = VIS [ . 3 ] = VIS [ . 4 ] = 0 ;
 70      for ( int I = 0 ; I < 2 ; I ++) /// 1/4 block checks the upper right corner 
71 is      {
 72          for ( int = J 2 ; J < . 4 ; J ++ )
 73 is          {
 74              IF (P [I] [J] == 0 )
 75                  Continue ;
 76              IF (VIS [P [I] [J]])
 77                  return  0 ;
 78             VIS [P [I] [J]] = . 1 ;
 79          }
 80      }
 81  
82      VIS [ . 1 ] = VIS [ 2 ] = VIS [ . 3 ] = VIS [ . 4 ] = 0 ;
 83      for ( int I = 2 ; I < . 4 ; I ++) /// lower left corner of 1/4 check block 
84      {
 85          for ( int J = 0 ; J < 2 ; J ++ )
 86          {
 87              IF (P [I] [J] == 0 )
88                  Continue ;
 89              IF (VIS [P [I] [J]])
 90                  return  0 ;
 91 is              VIS [P [I] [J]] = . 1 ;
 92          }
 93      }
 94  
95      VIS [ . 1 ] = VIS [ 2 ] VIS = [ . 3 ] = VIS [ . 4 ] = 0 ;
 96      for ( int I = 2 ; I < . 4 ; I ++) /// bottom right quarter check block 
97      {
 98          for ( int J =2; j<4; j++ )
 99         {
100             if( p[i][j]==0 )
101                 continue;
102             if( vis[p[i][j]] )
103                 return 0;
104             vis[p[i][j]] = 1;
105         }
106     }
107     return 1;
108 }
109 
110 
111 void dfs(int x)
112 {
113     if( flag )
114         return ;
115     if( x==tot )
116     {
117         for(int i=0; i<4; i++ )
118         {
119             for(int j=0; j<4; j++ )
120             {
121                 cout<<p[i][j];
122                 //printf("%d", p[i][j]);
123             }
124             cout<<endl;
125             //printf("\n");
126         }
127         flag=1;
128         return ;
129     }
130 
131     for(int i=1; i<=4; i++ )
132     {
133         p[tx[x]][ty[x]]=i;
134         if( check())
135         {
136             dfs(x+1);
137         }
138         p[tx[x]][ty[x]] = 0;
139     }
140 }
141 
142 int main()
143 {
144     int t;
145     scanf("%d", &t);
146     int k=1;
147     //getchar();
148     while( t-- )
149     {
150         for(int i=0; i<4; i++ )
151         {
152             for(int j=0; j<4; j++ )
153             {
154                 cin>>ma[i][j];
155                 //scanf("%c", &ma[i][j]);
156             }
157             //getchar();
158         }
159 
160         for(int i=0; i<4; i++ )
161         {
162             for(int j=0; j<4; j++ )
163             {
164                 if( ma[i][j]=='*' )
165                 {
166                     p[i][j] = 0;
167                 }
168                 else
169                     p[i][j] = ma[i][j] - '0';
170             }
171         }
172 
173         flag=0;
174         tot = 0;
175         for(int i=0; i<4; i++ )
176         {
177             for(int j=0; J < . 4 ; J ++ )
 178              {
 179                  IF ! ( P [I] [J])
 180 [                  {
 181                      / * sequentially recorded empty node coordinates * / 
182                      TX [TOT] = I;
 183 is                      TY [TOT] = J;
 184                      TOT ++ ;
 185                  }
 186              }
 187          }
 188          the printf ( " Case #% D: \ n- " , K ++ );
 189          DFS ( 0 );
190     }
191     return 0;
192 }
View Code

 

Guess you like

Origin www.cnblogs.com/wsy107316/p/11322995.html