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
看数据不大直接dfs就行了~ 但是写出的代码还是超时~ 已经很优化了~
真心给跪了,没看题,貌似还要有个判重~
TLE 代码:
#include<iostream>
#include<cstring>
using namespace std;
struct Node
{
int shang,you,xia,zuo;
}circle[100];
int map[101][101];
int N;
int vis[100];
int flag;
int bian;
int m=0;
int p[100];
void dfs(int k)
{
/*
cout<<endl<<endl;;
cout<<k<<" sdadfa "<<endl;
cout<<N*N-1<<endl;
cout<<endl;*/
if (k==N*N-1)
{
flag=1;
}
for (int i=0;i<N*N;i++)
{
if(!vis[i])
{
int hang=k/N;
int lie=k%N;
// cout<<k<<" kkkkkkk "<<endl;
//cout<<k/N<<endl;
// cout<<lie<<" eeeeeee"<<endl;
// cout<<i<<" PPPPPPPPPPPPP "<<endl;
//cout<<circle[3].xia<<" "<<circle[1].shang<<endl;
if(k/N!=0&&circle[i].shang!=circle[p[k-N]].xia)
continue;
if(k%N!=0&&circle[i].zuo!=circle[p[k-1]].you)
continue;
vis[i]=1;
p[k]=i;
dfs(k+1);
vis[i]=0;
}
}
}
int main()
{
int T=0;
while (cin>>N)
{
bian=0;
if (N==0)
break;
memset(map,0,sizeof(map));
memset(circle,0,sizeof(circle));
for (int i=0;i<N*N;i++)
{
cin>>circle[i].shang>>circle[i].you;
cin>>circle[i].xia>>circle[i].zuo;//在输入的时候还需要一个判重 否则超时,现在才知道,把相同的数据存到数组里,可以省好长好长时间~~~~~~~~
}
flag=0;
bian=N*N;
dfs(0);
if (flag==1)
cout<<"Game "<<++T<<": Possible"<<endl;
else
cout<<"Game "<<++T<<": Impossible"<<endl;
cout<<endl;
}
return 0;
}
/*
2
0 4 4 3
6 8 5 4
4 4 5 6
5 9 1 4
*/
ac代码:
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<string.h>
using namespace std;
int circle[30][4];
int N,countn;
int Count[30];
int p[30];
bool DFS(int cur)
{
if( cur == N * N)
return true;
for(int i = 0;i < countn;i++ )
{
if(Count[i] == 0)continue;
if( cur % N != 0 && circle[p[cur - 1]][1] != circle[i][3])
continue;
if( cur / N != 0 && circle[p[cur - N]][2] != circle[i][0])
continue;
p[cur] = i;
Count[i]--;
if(DFS(cur+ 1))
return true;
else
Count[i]++;
}
return false;
}
int main()
{
int T = 0;
while(scanf("%d",&N)&&N)
{
countn = 0;
int up,down,left,right,k;
for(int i = 0;i < N*N;i++)
{
scanf("%d%d%d%d",&up,&right,&down,&left);
for( k = 0 ;k < countn;k++)
{
if(up == circle[k][0] && right == circle[k][1] && down == circle[k][2] && left == circle[k][3])
break;
}
if( k == countn )
{
countn++;
circle[k][0] = up;
circle[k][1] = right ;
circle[k][2]= down ;
circle[k][3] = left;
Count[k] = 1;
}
else
{
Count[k]++;
}
}
if(T != 0)
printf("\n");
T++;
if(DFS(0))
printf("Game %d: Possible\n",T);
else
printf("Game %d: Impossible\n",T);
}
return 0;
}