码迷,mamicode.com
首页 > 其他好文 > 详细

Sudoku(简单DFS)

时间:2019-08-08 19:37:25      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:src   输入   close   problem   cto   div   ons   sed   右上角   

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5547

数据比较少,直接暴力DFS,检验成立情况即可

AC代码:但是不知道为什么用scanf,printf输入输出就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 int flag, tot;
 27 
 28 int check()
 29 {
 30     for(int i=0; i<4; i++ )///检查每一行
 31     {
 32         vis[1]=vis[2]=vis[3]=vis[4]=0;
 33         for(int j=0; j<4; j++ )
 34         {
 35             if( p[i][j]==0 )
 36                 continue;
 37             if( vis[p[i][j]] )///填的数重复了不成立
 38                 return 0;
 39             vis[p[i][j]] = 1;
 40         }
 41     }
 42 
 43     for(int j=0; j<4; j++ )///检查每一列
 44     {
 45         vis[1]=vis[2]=vis[3]=vis[4]=0;
 46         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++ )///检查左上角的1/4方块
 58     {
 59         for(int j=0; j<2; j++ )
 60         {
 61             if( p[i][j]==0 )
 62                 continue;
 63             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块
 71     {
 72         for(int j=2; j<4; j++ )
 73         {
 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++ )///检查左下角1/4块
 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             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++ )///检查右下角1/4块
 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                     /*依次记录空节点的坐标*/
182                     tx[tot] = i;
183                     ty[tot] = j;
184                     tot++;
185                 }
186             }
187         }
188         printf("Case #%d:\n", k++);
189         dfs(0);
190     }
191     return 0;
192 }
View Code

 

Sudoku(简单DFS)

标签:src   输入   close   problem   cto   div   ons   sed   右上角   

原文地址:https://www.cnblogs.com/wsy107316/p/11322995.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!