Another Eight Puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 680 Accepted Submission(s): 414
Problem Description
Fill the following 8 circles with digits 1~8,with each number exactly once . Conntcted circles cannot be filled with two consecutive numbers.
There are 17 pairs of connected cicles:
A-B , A-C, A-D
B-C, B-E, B-F
C-D, C-E, C-F, C-G
D-F, D-G
E-F, E-H
F-G, F-H
G-H
Filling G with 1 and D with 2 (or G with 2 and D with 1) is illegal since G and D are connected and 1 and 2 are consecutive .However ,filling A with 8 and B with 1 is legal since 8 and 1 are not consecutive .
In this problems,some circles are already filled,your tast is to fill the remaining circles to obtain a solution (if possivle).
There are 17 pairs of connected cicles:
A-B , A-C, A-D
B-C, B-E, B-F
C-D, C-E, C-F, C-G
D-F, D-G
E-F, E-H
F-G, F-H
G-H
Filling G with 1 and D with 2 (or G with 2 and D with 1) is illegal since G and D are connected and 1 and 2 are consecutive .However ,filling A with 8 and B with 1 is legal since 8 and 1 are not consecutive .
In this problems,some circles are already filled,your tast is to fill the remaining circles to obtain a solution (if possivle).
Input
The first line contains a single integer T(1≤T≤10),the number of test cases. Each test case is a single line containing 8 integers 0~8,the numbers in circle A~H.0 indicates an empty circle.
Output
For each test case ,print the case number and the solution in the same format as the input . if there is no solution ,print “No answer”.If there more than one solution,print “Not unique”.
Sample Input
3 7 3 1 4 5 8 0 0 7 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
Sample Output
Case 1: 7 3 1 4 5 8 6 2 Case 2: Not unique Case 3: No answer#include<iostream> #include<cstring> #include<cstdio> using namespace std; int a[10],b[10]; int mark[10]; int flag; int judge(int num,int n) { if(n==1&&num==2) return 1; if(n==8&&num==7) return 1; if((n>1&&n<8)&&(num==(n-1)||num==(n+1))) return 1; return 0; } int cheak(int pos ,int n) { int i; if(pos==1) { for(i=2;i<=4;i++) if(judge(a[i],n)) return 0; return 1; } if(pos==2) { if(judge(a[1],n)) return 0; if(judge(a[3],n)) return 0; if(judge(a[5],n)) return 0; if(judge(a[6],n)) return 0; return 1; } if(pos==3) { if(judge(a[1],n)) return 0; if(judge(a[2],n)) return 0; for(i=4;i<=7;i++) if(judge(a[i],n)) return 0; return 1; } if(pos==4) { if(judge(a[1],n)) return 0; if(judge(a[3],n)) return 0; for(i=6;i<=7;i++) if(judge(a[i],n)) return 0; return 1; } if(pos==5) { if(judge(a[2],n)) return 0; if(judge(a[3],n)) return 0; if(judge(a[6],n)) return 0; if(judge(a[8],n)) return 0; return 1; } if(pos==6) { for(i=2;i<=5;i++) if(judge(a[i],n)) return 0; return 1; for(i=7;i<=8;i++) if(judge(a[i],n)) return 0; return 1; } if(pos==7) { if(judge(a[3],n)) return 0; if(judge(a[4],n)) return 0; if(judge(a[6],n)) return 0; if(judge(a[8],n)) return 0; return 1; } if(pos==8) { for(i=5;i<=6;i++) if(judge(a[i],n)) return 0; return 1; } } void DFS(int pos) { int i; if(pos>8) { flag++; for(i=1;i<=8;i++) b[i]=a[i]; return ; } if(a[pos]==0) { for( i=1;i<=8;i++) { if(!mark[i]&&cheak(pos,i)) { mark[i]=1; a[pos]=i; DFS(pos+1); mark[i]=0; a[pos]=0; } } } else DFS(pos+1); } int main() { int t,i,j; cin>>t; for(j=1;j<=t;j++) { memset(mark,0,sizeof(mark)); for(i=1;i<=8;i++) { cin>>a[i]; mark[a[i]]=1; } flag=0; DFS(1); if(!flag) { printf("Case %d: ",j); printf("No answer\n"); } else if(flag>1) { printf("Case %d: ",j); printf("Not unique\n"); } else { printf("Case %d: ",j); printf("%d",b[1]); for(i=2;i<=8;i++) printf(" %d",b[i]); printf("\n"); } } return 0; }