标签:题解 uniq std cells mis ima sof output read
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 21055 | Accepted: 10043 | Special Judge |
Description
Input
Output
Sample Input
1 103000509 002109400 000704000 300502006 060000050 700803004 000401000 009205800 804000107
Sample Output
143628579 572139468 986754231 391542786 468917352 725863914 237481695 619275843 854396127
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 7 inline void read(int &x) 8 { 9 x = 0;char ch = getchar(),c = ch; 10 while(ch < ‘0‘ || ch > ‘9‘) c = ch, ch = getchar(); 11 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar(); 12 if(c == ‘-‘)x = -x; 13 } 14 15 int num[10][10], col[10][10], row[10][10], ge[10][10],ok[10][10],flag; 16 char ch[10]; 17 18 //还剩下cnt个数可以填 19 int dfs(int x, int y) 20 { 21 register int pp; 22 if(x == 10)return 1; 23 if(num[x][y]) 24 { 25 if(y == 9)flag = dfs(x + 1, 1); 26 else flag = dfs(x, y + 1); 27 if(flag)return 1; 28 else return 0; 29 } 30 int i = x, j = y,p,q; 31 pp = (i - 1)/3 * 3 + (j + 2)/3; 32 for(register int t = 1;t <= 9;++ t) 33 { 34 if(row[i][t] || col[j][t] || ge[pp][t])continue; 35 row[i][t] = col[j][t] = ge[pp][t] = 1; 36 num[i][j] = t; 37 p = i, q = j; 38 if(q == 9)q = 0, ++ p; 39 flag = dfs(p, q + 1); 40 if(flag)return 1; 41 num[i][j] = 0, col[j][t] = row[i][t] = ge[pp][t] = 0; 42 } 43 return 0; 44 } 45 46 int main() 47 { 48 register int tmp = 0, t; 49 read(t); 50 for(;t;--t) 51 { 52 memset(col, 0, sizeof(col)); 53 memset(row, 0, sizeof(row)); 54 memset(ge, 0, sizeof(ge)); 55 for(int i = 1;i <= 9;++ i) 56 { 57 scanf("%s", ch + 1); 58 for(register int j = 1;j <= 9;++ j) 59 { 60 num[i][j] = ch[j] - ‘0‘; 61 if(num[i][j]) 62 ++ tmp,row[i][num[i][j]] = col[j][num[i][j]] = ge[(i - 1)/3 * 3 + (j + 2)/3][num[i][j]] = 1; 63 } 64 } 65 dfs(1,1); 66 for(int i = 1;i <= 9;++ i) 67 { 68 for(register int j = 1;j <= 9;++ j) 69 printf("%d", num[i][j]); 70 putchar(‘\n‘); 71 } 72 } 73 return 0; 74 }
标签:题解 uniq std cells mis ima sof output read
原文地址:http://www.cnblogs.com/huibixiaoxing/p/7354535.html