Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 402 Accepted Submission(s): 191
php
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> using namespace std; int map[12][12]; char str[12]; int soso[110][3]; int the_search_number; int the_last_flag; int the_time; int the_search_flag[110]; int ans; bool the_last_can() { int bool_number[12]; for(int i = 1;i <= 9;i ++) { memset(bool_number,0,sizeof(bool_number)); for(int j = 1;j <= 9;j ++) { if(bool_number[map[i][j]] == 1) return false; bool_number[map[i][j]] = 1; } } for(int i = 1;i <= 9;i ++) { memset(bool_number,0,sizeof(bool_number)); for(int j = 1;j <= 9;j ++) { if(bool_number[map[j][i]] == 1) return false; bool_number[map[j][i]] = 1; } } for(int i = 1;i < 7;i += 3) { for(int j = 1;j < 7;j += 3) { memset(bool_number,0,sizeof(bool_number)); for(int k = i;k <= i + 3;k ++) { for(int h = j;h <= j + 3;h ++) { if(bool_number[map[k][h]] == 1) return false; bool_number[map[k][h]] == 1; } } } } return true; } bool yes_you_can(int x,int y,int k) { for(int i = 1;i <= 9;i ++) if(k == map[i][y]) return false; for(int i = 1;i <= 9;i ++) if(k == map[x][i]) return false; if(x <= 3) { if(y <= 3) { for(int i = 1;i <= 3;i ++) for(int j = 1;j <= 3;j ++) if(map[i][j] == k) return false; } if(y <= 6 && y > 3) { for(int i = 1;i <= 3;i ++) for(int j = 4;j <= 6;j ++) if(map[i][j] == k) return false; } if(y <= 9 && y > 6) { for(int i = 1;i <= 3;i ++) for(int j = 7;j <= 9;j ++) if(map[i][j] == k) return false; } } if(x > 3 && x <= 6) { if(y <= 3) { for(int i = 4;i <= 6;i ++) for(int j = 1;j <= 3;j ++) if(map[i][j] == k) return false; } if(y <= 6 && y > 3) { for(int i = 4;i <= 6;i ++) for(int j = 4;j <= 6;j ++) if(map[i][j] == k) return false; } if(y <= 9 && y > 6) { for(int i = 4;i <= 6;i ++) for(int j = 7;j <= 9;j ++) if(map[i][j] == k) return false; } } if(x > 6 && x <= 9) { if(y <= 3) { for(int i = 7;i <= 9;i ++) for(int j = 1;j <= 3;j ++) if(map[i][j] == k) return false; } if(y <= 6 && y > 3) { for(int i = 7;i <= 9;i ++) for(int j = 4;j <= 6;j ++) if(map[i][j] == k) return false; } if(y <= 9 && y > 6) { for(int i = 7;i <= 9;i ++) for(int j = 7;j <= 9;j ++) if(map[i][j] == k) return false; } } return true; } void DFS(int now) { if(the_last_flag != -1) return ; if(now == the_search_number + 1) { //for(int i = 1;i <= 9;i ++) //{ //for(int j = 1;j <= 9;j ++) //printf("%d",map[i][j]); //printf("\n"); //} if(the_last_can() == 0) { the_last_flag = 0; return ; } else { the_last_flag = 1; return ; } } for(int p = 1;p <= 9;p ++) { if(yes_you_can(soso[now][0],soso[now][1],p) == 1) { map[soso[now][0]][soso[now][1]] = p; the_search_flag[now] = 1; DFS(now + 1); if(the_last_flag != -1) return ; } } if(the_search_flag[now] == 0) { the_last_flag = 0; return ; } if(the_last_flag != -1) return ; } int main() { while(~scanf("%d",&the_time)) { for(int t = 1;t <= the_time;t ++) { memset(map,0,sizeof(map)); memset(soso,0,sizeof(soso)); memset(the_search_flag,0,sizeof(the_search_flag)); ans = 0; for(int i = 1;i <= 9;i ++) { memset(str,0,sizeof(str)); scanf("%s",str); for(int j = 0;j < 9;j ++) { map[i][j + 1] = str[j] - '0'; if(map[i][j + 1] == 0) { soso[++ ans][0] = i; soso[ans][1] = j + 1; } } } the_search_number = ans; the_last_flag = -1; DFS(1); if(the_last_flag == 0 || the_last_flag == -1) { printf("Could not complete this grid.\n"); } if(the_last_flag == 1) { for(int i = 1;i <= 9;i ++) { for(int j = 1;j <= 9;j ++) printf("%d",map[i][j]); printf("\n"); } } if(t != the_time) printf("\n"); } } return 0; }