Gnome Tetravex
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.
|
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 |
这道题给N*N个小方块,每个小方块有4个数字,输入方法时 上右下左,然后直接爆搜就行,注意有可能有的方块一样,为了避免重复搜索就需要把一样的合并在一起记录数量
#include<cstdio>
#include<iostream>
#include<cmath>
#include<queue>
#include<vector>
#include<cstring>
#include<algorithm>
#include<map>
#include<stack>
using namespace std;
#define inf 0x3f3f3f3f
struct pp
{
int a,b,c,d;
int cs;
}ss[50],kk,ma[6][6];
int n;
inline int ok(pp d, pp a,pp b,int x,int y)
{
//if(x<0||y<0||x>=n||y>=n) return 0;
if(x!=1&&d.b!=b.d) return 0;
if(y!=1&&a.c!=b.a) return 0;
return 1;
}
int flagg;
int o;
void dfs(int x,int y)
{
if(y==n+1)
{
x++;y=1;
}
if(flagg) return;
if(x==n+1)
{
flagg=1;
return;
}
for(int i=0;i<o;i++)
{
if(ss[i].cs!=0&&(ok(ma[x-1][y],ma[x][y-1],ss[i],x,y)))
{
ss[i].cs--;
ma[x][y]=ss[i];
dfs(x,y+1);
ss[i].cs++;
if(flagg) return;
}
}
}
int main()
{
// ios::sync_with_stdio(false);
int t;
int cnt=0;
while(/*cin>>n*/~scanf("%d",&n))
{ if(n==0) return 0;
o=0;
cnt++;
for(int i=0;i<n*n;i++)
{ int a,b,c,d;
//cin>>a>>b>>c>>d;
scanf("%d%d%d%d",&ss[o].a,&ss[o].b,&ss[o].c,&ss[o].d);
int flag=0;
for(int j=0;j<o;j++)
{
if(ss[j].a==ss[o].a&&ss[j].b==ss[o].b&&ss[j].c==ss[o].c&&ss[j].d==ss[o].d)
{
ss[j].cs++;
flag=1;
break;
}
}
if(!flag)
{
ss[o++].cs=1;
}
}
flagg=0;
if(cnt!=1) //cout<<endl;
printf("\n");
dfs(1,1);
if(flagg) //cout<<"Game "<<cnt<<": Possible"<<endl;
printf("Game %d: Possible\n",cnt);
else //cout<<"Game "<<cnt<<": Impossible"<<endl;
printf("Game %d: Impossible\n",cnt);
}
return 0;
}