【洛谷P2927 [USACO08DEC]拼图游戏Jigsaw Puzzles】深搜
其实,随便搜了一搜就可以AC了!!!
- -
亏我纠结了这么久= =
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #include<queue> 7 #include<cmath> 8 using namespace std; 9 #define Maxn 110 10 11 struct node 12 { 13 int num,ax[5]; 14 }t[Maxn]; 15 16 bool cmp(node x,node y) {return x.num<y.num;} 17 18 int r,c; 19 char s[0]; 20 21 int w[10][10],b[10][10]; 22 bool vis[Maxn]; 23 24 void output() 25 { 26 for(int i=1;i<=r;i++) 27 for(int j=1;j<=c;j++) 28 { 29 printf("%d ",t[b[i][j]].num); 30 for(int k=0;k<4;k++) 31 { 32 int x=t[b[i][j]].ax[(k+w[i][j])%4]; 33 if(x==0) printf("0 "); 34 else printf("%c ",x+'a'-1); 35 } 36 printf("\n"); 37 } 38 } 39 40 bool ok=0; 41 42 void ffind(int x,int y) 43 { 44 if(x==r+1) 45 { 46 output(); 47 ok=1; 48 return; 49 } 50 if(y==c+1) {ffind(x+1,1);return;} 51 for(int i=1;i<=r*c;i++) if(!vis[i]) 52 { 53 for(int k=0;k<4;k++) 54 if(t[i].ax[k]==t[b[x-1][y]].ax[(w[x-1][y]+2)%4] && t[i].ax[(k+3)%4]==t[b[x][y-1]].ax[(w[x][y-1]+1)%4]) 55 { 56 if(y==c&&t[i].ax[(k+1)%4]!=0) continue; 57 if(y!=c&&t[i].ax[(k+1)%4]==0) continue; 58 if(x!=r&&t[i].ax[(k+2)%4]==0) continue; 59 if(x==r&&t[i].ax[(k+2)%4]!=0) continue; 60 w[x][y]=k; 61 b[x][y]=i; 62 vis[i]=1; 63 ffind(x,y+1); 64 vis[i]=0; 65 if(ok==1) return; 66 } 67 } 68 } 69 70 int main() 71 { 72 scanf("%d%d",&r,&c); 73 for(int i=1;i<=r*c;i++) 74 { 75 scanf("%d",&t[i].num); 76 for(int k=0;k<4;k++) 77 { 78 scanf("%s",s); 79 if(s[0]=='0') t[i].ax[k]=0; 80 else t[i].ax[k]=s[0]-'a'+1; 81 } 82 } 83 memset(vis,0,sizeof(vis)); 84 for(int i=0;i<4;i++) t[0].ax[i]=0; 85 memset(b,0,sizeof(b)); 86 memset(w,0,sizeof(w)); 87 sort(t+1,t+1+r*c,cmp); 88 ffind(1,1); 89 return 0; 90 }
2016-11-14 21:56:40