Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is given n*n squares. Each square is divided into four triangles marked four numbers (range from 0 to 9). In a square, the triangles are the left triangle, the top triangle, the right triangle and the bottom triangle. For example, Fig. 1 shows the initial state of 2*2 squares.
Fig. 1 The initial state with 2*2 squares
The player is required to move the squares to the termination state. In the termination state, any two adjoining squares should make the adjacent triangle marked with the same number. Fig. 2 shows one of the termination states of the above example.
Fig. 2 One termination state of the above example
It seems the game is not so hard. But indeed, Hart is not accomplished in the game. He can finish the easiest game successfully. When facing with a more complex game, he can find no way out.
One day, when Hart was playing a very complex game, he cried out, "The computer is making a goose of me. It's impossible to solve it." To such a poor player, the best way to help him is to tell him whether the game could be solved. If he is told the game is unsolvable, he needn't waste so much time on it.
Input
The input file consists of several game cases. The first line of each game case contains one integer n, 0 <= n <= 5, indicating the size of the game.
The following n*n lines describe the marking number of these triangles. Each line consists of four integers, which in order represent the top triangle, the right triangle, the bottom triangle and the left triangle of one square.
After the last game case, the integer 0 indicates the termination of the input data set.
Output
You should make the decision whether the game case could be solved. For each game case, print the game number, a colon, and a white space, then display your judgment. If the game is solvable, print the string "Possible". Otherwise, please print "Impossible" to indicate that there's no way to solve the problem.
Print a blank line between each game case.
Note: Any unwanted blank lines or white spaces are unacceptable.
Sample Input
2
5 9 1 4
4 4 5 6
6 8 5 4
0 4 4 3
2
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
0
Output for the Sample Input
Game 1: Possible
Game 2: Impossible
题意:移动上面的每个四方格,如果能使所有的四方格相邻的边的三角形上面数字相同,则输出Possible,否则Impossible,这里采取将相同的方格分组。注意输出格式。因为Case后面少了一个空格,一直WA,实在是无语。。。。。
#include<stdio.h>
#include<string.h>
#define N 30
struct block{
int ll,uu,rr,dd,v;
}b[N],res[N],p;
int n,len;
int dfs(int pos){
int i;
if(pos==n*n){
return 1;
}
for(i=0;i<len;i++){
if(b[i].v>0){
if(pos>=n&&b[i].uu!=res[pos-n].dd) continue;
if(pos%n!=0&&b[i].ll!=res[pos-1].rr) continue;
b[i].v--;
res[pos]=b[i];
if(dfs(pos+1)==1)
return 1;
b[i].v++;
}
}
return 0;
}
int main(){
int i,t=0,j,ans;
while(scanf("%d",&n),n){
memset(b,0,sizeof(b));
memset(res,0,sizeof(res));
for(i=0,len=0;i<n*n;i++){
scanf("%d %d %d %d",&p.uu,&p.rr,&p.dd,&p.ll);
p.v=1;
for(j=0;j<len;j++){
if(b[j].uu==p.uu&&b[j].rr==p.rr&&b[j].dd==p.dd&&b[j].ll==p.ll){
b[j].v++;//v表示这种方格的个数
break;
}
}
if(j==len) b[len++]=p;
}
ans=dfs(0);
if(t!=0){
printf("\n");
}
printf("Game %d: ",++t);
if(ans)
printf("Possible\n");
else
printf("Impossible\n");
}
return 0;
}
/*
4
1 1 1 1
2 5 6 3
1 1 1 1
5 6 8 4
1 1 1 1
2 1 5 8
1 1 1 1
5 8 4 6
1 1 1 1
0 1 5 6
1 1 1 1
3 1 5 7
1 1 1 1
3 2 5 6
1 1 1 1
8 4 5 6
*/
#include<stdio.h>
#define N 30
struct block{
int ll,uu,rr,dd,v;
}b[N],res[N],p;
int flag,n,len;
void dfs(int row, int col){
int i,up,left,s;
if(flag)
return;
if(row==n){
flag=1;
return;
}
for(i=0;i<len;i++){
if(b[i].v>0){
s=row*n+col;
up=s-n;;
left=s-1;
if(row!=0&&b[i].uu!=res[up].dd) continue;
if(col!=0&&b[i].ll!=res[left].rr) continue;
b[i].v--;
res[s]=b[i];
if(col+1==n)
dfs(row+1,0);
else
dfs(row,col+1);
b[i].v++;
}
}
}
int main(){
int i,t=0,j;
while(scanf("%d",&n),n){
for(i=0,flag=len=0;i<n*n;i++){
scanf("%d %d %d %d",&p.uu,&p.rr,&p.dd,&p.ll);
p.v=1;
for(j=0;j<len;j++){
if(b[j].uu==p.uu&&b[j].rr==p.rr&&b[j].dd==p.dd&&b[j].ll==p.ll){
b[j].v++;//v表示这种方格的个数
break;
}
}
if(j==len) b[len++]=p;
}
dfs(0,0);
if(t!=0){
printf("\n");
}
printf("Game %d: ",++t);
if(flag)
printf("Possible\n");
else
printf("Impossible\n");
}
return 0;
}
/*
4
1 1 1 1
2 5 6 3
1 1 1 1
5 6 8 4
1 1 1 1
2 1 5 8
1 1 1 1
5 8 4 6
1 1 1 1
0 1 5 6
1 1 1 1
3 1 5 7
1 1 1 1
3 2 5 6
1 1 1 1
8 4 5 6
*/