标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6798 Accepted Submission(s): 2117
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <string> #include <map> using namespace std; int mp[15][15]; char s[10]; int dir[][2] ={{-1,0},{1,0},{0,1},{0,-1}}; struct Node{ int x,y; }node[100]; bool vis[100]; int tot; bool flag; void getmp(char c,int a,int b){ if(isdigit(c)) mp[a][b] = c-‘0‘; else { mp[a][b] = 0; node[tot].x = a; node[tot++].y = b; } } bool judge(int x,int y,int v){ for(int i=1;i<=9;i++){ if(mp[i][y]==v||mp[x][i]==v) return false; } int a = (x-1)/3,b = (y-1)/3; for(int i=a*3+1;i<=a*3+3;i++){ for(int j=b*3+1;j<=b*3+3;j++){ if(mp[i][j]==v) return false; } } return true; } void dfs(int num){ if(!flag) return; if(num==tot){ flag = false; for(int i=1;i<=9;i++){ for(int j=1;j<=9;j++){ if(j!=9) printf("%d ",mp[i][j]); else printf("%d\n",mp[i][j]); } } return; } for(int i=1;i<=9;i++){ if(judge(node[num].x,node[num].y,i)){ vis[num] = true; mp[node[num].x][node[num].y] = i; dfs(num+1); mp[node[num].x][node[num].y] = 0; vis[num] = false; } } } int main() { int k = 0; while(scanf("%s",s)!=EOF){ tot = 0; flag = true; getmp(s[0],1,1); for(int i=2;i<=9;i++){ scanf("%s",s); getmp(s[0],1,i); } for(int i=2;i<=9;i++){ for(int j=1;j<=9;j++){ scanf("%s",s); getmp(s[0],i,j); } } memset(vis,false,sizeof(vis)); if(k++) printf("\n"); dfs(0); } }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5746740.html