标签:
Description
Input
Output
Sample Input
Sample Output
#include<cstdio> #include<cstring> using namespace std; int map[10][10]; int cut,flag,s[90][2]; int f(int k,int p) { int i,j,a,b; for (i=0;i<9;i++) if (map[s[k][0]][i]==p&&i!=s[k][1]) return 0; else if (map[i][s[k][1]]==p&&i!=s[k][0]) return 0; a=(s[k][0]/3)*3; b=(s[k][1]/3)*3; for (i=0;i<3;i++) for (j=0;j<3;j++) { if (a+i==s[k][0]&&b+j==s[k][1]) continue; if (map[a+i][b+j]==p) return 0; } return 1; } void dfs(int k) { int i,j; if (flag) return ; if (cut==k) { for (i=0;i<9;i++){ for (j=0;j<8;j++) printf("%d ",map[i][j]); printf("%d\n",map[i][j]);} flag=1; return ; } for (i=1;i<=9;i++) { if (f(k,i)) { map[s[k][0]][s[k][1]]=i; dfs(k+1); map[s[k][0]][s[k][1]]=0; } } return ; } int main() { int i,j,k=1; char c; while (~scanf(" %c",&c)) { cut=0; if (c==‘?‘) { s[cut][0]=0; s[cut++][1]=0; map[0][0]=0; } else map[0][0]=c-‘0‘; flag=0; for (i=0;i<9;i++) for (j=0;j<9;j++) { if (i+j==0) continue; scanf(" %c",&c); if (c==‘?‘) { s[cut][0]=i; s[cut++][1]=j; map[i][j]=0; } else map[i][j]=c-‘0‘; } if (k!=1) printf("\n");k++; dfs(0); } return 0; }
标签:
原文地址:http://www.cnblogs.com/pblr/p/4711954.html