标签:
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 18105 | Accepted: 8772 | 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
暑假艾教教的数独,留个板子。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int sudoku[10][10]; int column[10][10]; int row[10][10]; int nine[10][10]; struct node { int x,y; }; node ready[105]; int T; int num = 0; char s[12][12]; int cal(int x,int y) { return (x-1)/3*3+(y-1)/3+1; } bool cmp(node A,node B) { return A.y>B.y; } void put(int x,int y,int tt,int flag) { if(flag) sudoku[x][y] = tt; row[x][tt] = flag; column[y][tt] = flag; nine[cal(x,y)][tt] = flag; } int ok = 0; int can(int x,int y,int tt) { if(row[x][tt]) return 0; if(column[y][tt]) return 0; if(nine[cal(x,y)][tt]) return 0; return 1; } void dfs(int k) { if(ok) return; if(k==num+1) { for(int i=1;i<=9;i++) { for(int j=1;j<=9;j++) { if(j==9) printf("%d\n",sudoku[i][j]); else printf("%d",sudoku[i][j]); } } ok = 1; return; } for(int nex=1;nex<=9;nex++) { if(can(ready[k].x,ready[k].y,nex)) { put(ready[k].x,ready[k].y,nex,1); dfs(k+1); put(ready[k].x,ready[k].y,nex,0); } } } int main() { cin>>T; while(T--) { ok = 0; memset(sudoku,0,sizeof(sudoku)); memset(ready,0,sizeof(ready)); memset(column,0,sizeof(column)); memset(row,0,sizeof(row)); memset(nine,0,sizeof(nine)); num = 0; for(int i=1;i<=9;i++) { scanf("%s",s[i]+1); for(int j=1;j<=9;j++) { if(s[i][j]==‘0‘) { num++; ready[num].x = i; ready[num].y = j; } else { put(i,j,s[i][j]-‘0‘,1); } } } sort(ready+1,ready+num+1,cmp); dfs(1); } return 0; }
标签:
原文地址:http://www.cnblogs.com/littlepear/p/5789058.html