看来这题不能用简单DFS
#include <iostream>
#include <stdio.h>
using namespace std;
int map[25][5],n,map1[25][5],cot[25];
bool flag ,flag1;
void Move(int t)
{
int i,j;
if(flag1) return;
if(t == n*n)
{
/*for(i = 0;i < n*n;i ++)
{
for(j = 0;j< 4;j ++)
cout<<map1[i][j]<<" ";
cout<<endl;
}
cout<<endl;*/
for(i = 0;i < n*n;i ++)
{
if(i < n*(n-1))
{
if(i%n < n-1){
if(map1[i][1] != map1[i+1][3]||map1[i][2] != map1[i+n][0])
{
flag = 1;
return ;
}
}
else
{
if(map1[i][2]!=map1[i+n][0])
{
flag = 1;
return ;
}
}
}
else
{
if(i%n < n-1){
if(map1[i][1] != map1[i+1][3])
{
flag = 1;
return ;
}
}
}
}
flag1 = 1;
return;
}
for(i = 0;i < n*n;i ++)
{
if(cot[i] == 0)
{
//cout<<i<<" "<<t<<endl;
for(j = 0;j < 4;j ++)
map1[t][j] = map[i][j];
cot[i] = 1;
flag = 0;
Move(t+1);
cot[i] = 0;
}
}
}
int main()
{
int i,j,cnt = 0;
while(scanf("%d",&n)!=EOF&&n)
{
for(i = 0;i < n*n;i ++)
{
for(j = 0;j < 4;j ++)
{
cin>>map[i][j];
}
}
flag1 = 0;
for(i = 0;i < n*n;i ++) cot[i] = 0;
Move(0);
if(flag1) printf("Game %d: Possible\n\n",++cnt);
else printf("Game %d: Impossible\n\n",++cnt);
//cout<<endl;
}
return 0;
}