标签:
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 18193 | Accepted: 8803 | 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 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 using namespace std; 7 int mp[15][15]; 8 int block[15],h[15],r[15]; 9 int in[15][15]; 10 int sx[100],sy[100],cnt=0; 11 inline int blcnt(int x){ 12 int res=9; 13 while(x){ 14 res-=(x&1); 15 x>>=1; 16 } 17 return res; 18 } 19 void init(){ 20 for(int i=1;i<=9;i++) 21 for(int j=1;j<=9;j++) 22 in[i][j]=(i+2)/3*3+(j+2)/3-3; 23 } 24 void Print(){ 25 for(int i=1;i<=9;i++){ 26 for(int j=1;j<=9;j++) 27 printf("%d",mp[i][j]); 28 printf("\n"); 29 } 30 } 31 bool dfs(){ 32 int i,j; 33 int mini=60,pos=0; 34 int tmp,now; 35 int ctmp; 36 for(i=1;i<=cnt;i++){ 37 if(!mp[sx[i]][sy[i]]){ 38 tmp=h[sx[i]]|r[sy[i]]|block[in[sx[i]][sy[i]]]; 39 now=blcnt(tmp); 40 if(!now)return 0; 41 if(now<mini){ 42 ctmp=tmp;//记录最小值对应的tmp 43 //先前由于没有记录ctmp,而在后面计算时用tmp算,出现了bug,费了一个小时查出来错 44 mini=now; 45 pos=i; 46 } 47 } 48 } 49 if(mini==60){ 50 Print(); 51 return 1; 52 } 53 i=sx[pos]; 54 j=sy[pos]; 55 for(int k=1;k<=9;k++){ 56 if(!(ctmp&(1<<k))){ 57 mp[i][j]=k; 58 h[i]^=(1<<k); 59 r[j]^=(1<<k); 60 block[in[i][j]]^=(1<<k); 61 if(dfs())return 1; 62 //回溯 63 block[in[i][j]]^=(1<<k); 64 h[i]^=(1<<k); 65 r[j]^=(1<<k); 66 mp[i][j]=0; 67 } 68 } 69 return 0; 70 } 71 72 int main(){ 73 int i,j; 74 int T; 75 scanf("%d",&T); 76 init(); 77 while(T--){ 78 memset(h,0,sizeof h); 79 memset(r,0,sizeof r); 80 memset(block,0,sizeof block); 81 cnt=0; 82 83 char ch[20]; 84 for(i=1;i<=9;i++){ 85 scanf("%s",ch); 86 for(j=1;j<=9;j++){ 87 mp[i][j]=ch[j-1]-‘0‘; 88 } 89 } 90 for(i=1;i<=9;i++)//行 91 for(j=1;j<=9;j++){//列 92 if(mp[i][j]){ 93 r[j]^=1<<mp[i][j]; 94 h[i]^=1<<mp[i][j]; 95 block[in[i][j]]^=1<<mp[i][j]; 96 } 97 else{ 98 cnt++; 99 sx[cnt]=i; 100 sy[cnt]=j; 101 } 102 } 103 dfs(); 104 } 105 return 0; 106 }
标签:
原文地址:http://www.cnblogs.com/SilverNebula/p/5857685.html