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
题意:给出你N*N个正方形,每个正方形有上下左右4个数字,交换正方形的位置,问存不存在相交处的数字都相等
首先要考虑相交处,如何满足相交处都相同?此时就要考虑一些特殊的位置,比如大正方形的棱,发现如果除去大正方形的上棱与左棱,那么组成大正方形的小正方形只要都满足左和上相交处相同即可
然后按照这个思路暴力深搜,枚举每一个位置,看是否存在答案即可
思路是没问题,但总超时,考虑到可能会出现很多相等的正方形,把他们统计在一起,搜索时for可以在较少次数内遍历到,减少部分时间复杂度
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=30;
struct node
{
int l,r,u,d;
bool operator == (const node &a)const
{
return l==a.l&&r==a.r&&u==a.u&&d==a.d;
}
} e[N];
int map[11][11],n,sum[N];
bool flag;
bool dfs(int pos,int n)
{
if(pos==n*n)
return true;
int x=pos/n,y=pos%n;//当前位置
for(int i=0; i<n*n; i++)
{
if(sum[i])
{
if(x>0&&e[i].u!=e[map[x-1][y]].d)continue;//不是最上边,此方块上不等于上方块下
if(y>0&&e[i].l!=e[map[x][y-1]].r)continue;//不是最左边,此方块左不等于左方块右
map[x][y]=i;
sum[i]--;
if(dfs(pos+1,n)==true)return true;//下一个位置
else
sum[i]++;
}
}
return false;
}
int main()
{
int q=0;
while(~scanf("%d",&n)&&n)
{
mem(sum,0);
for(int i=0; i<n*n; i++)
{
scanf("%d%d%d%d",&e[i].u,&e[i].r,&e[i].d,&e[i].l);
for(int j=0; j<=i; j++)
{
if(e[i]==e[j])
{
sum[j]++;
break;
}
}
}
if(q)
printf("\n");
if(dfs(0,n))
printf("Game %d: Possible\n",++q);
else
printf("Game %d: Impossible\n",++q);
}
return 0;
}