标签:搜索
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 14120 | Accepted: 6976 | 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
深搜。。
题意:就是传统的数独游戏。。找出一种方案即可。。有spj
对 行,列,以及9个3*3的方块哈希。。然后暴力深搜就行了。。实践证明倒搜比较快
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #define ll long long #define maxn 360 #define pp pair<int,int> #define INF 0x3f3f3f3f #define max(x,y) ( ((x) > (y)) ? (x) : (y) ) #define min(x,y) ( ((x) > (y)) ? (y) : (x) ) using namespace std; char ma[11][11]; bool row[11][11],col[11][11],mar[11][11],ok; int change(int x,int y) { if(x>=1&&x<=3) return y%3==0?y/3:y/3+1; else if(x>=4&&x<=6) return 3+(y%3==0?y/3:y/3+1); else return 6+(y%3==0?y/3:y/3+1); } void dfs(int num) { if(ok) return ; if(num==0) { for(int i=1;i<=9;i++) { for(int j=1;j<=9;j++) putchar(ma[i][j]); puts(""); } ok=1; return ; } int x=num%9==0?num/9:num/9+1; int y=num%9==0?9:num%9; if(ma[x][y]-'0') dfs(num-1); else { for(int i=1;i<=9;i++) { if(!row[x][i]&&!col[y][i]&&!mar[change(x,y)][i]) { ma[x][y]=i+'0';row[x][i]=1;col[y][i]=1;mar[change(x,y)][i]=1; dfs(num-1); ma[x][y]='0';row[x][i]=0;col[y][i]=0;mar[change(x,y)][i]=0; } } } } int main() { int T; scanf("%d",&T); while(T--) { memset(row,0,sizeof(row)); memset(col,0,sizeof(col)); memset(mar,0,sizeof(mar)); for(int i=1;i<=9;i++) scanf("%s",ma[i]+1); for(int i=1;i<=9;i++) for(int j=1;j<=9;j++) if(ma[i][j]-'0') { row[i][ma[i][j]-'0']=1; col[j][ma[i][j]-'0']=1; mar[change(i,j)][ma[i][j]-'0']=1; } ok=0;dfs(81); } return 0; }
标签:搜索
原文地址:http://blog.csdn.net/qq_16255321/article/details/41366269