ZOJ1008

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.

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.

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个平方。每个正方形被分成四个三角形,标记四个数字(范围从0到9)。在正方形中,三角形是左三角形、顶三角形、右三角形和底部三角形。例如,图1显示了2×2平方的初始状态。
图1具有2×2平方的初始状态
玩家必须将方块移动到终止状态。在终止状态下,任何两个相邻的正方形都应该用相同的数字标记相邻的三角形。图2示出了上述示例的终止状态之一。
图2上述示例的一个终止状态
看来比赛并不那么难。但事实上,哈特在这场比赛中还没有完成。他能成功地完成最简单的比赛。当面对一个更复杂的游戏时,他找不到出路。
有一天,当哈特在玩一个非常复杂的游戏时,他喊道:“计算机在使我感到骄傲。”。对这样一个可怜的球员来说,帮助他最好的办法就是告诉他比赛是否能解决。如果告诉他游戏是无法解决的,他不必浪费太多时间了。
输入
输入文件由几个游戏用例组成。每一个游戏案例的第一行包含一个整数n,0个< = n = 5,表示游戏的大小。
下面的n行描述了这些三角形的标记号。每一行由四个整数组成,它们依次代表一个三角形,右三角形,底部三角形和一个正方形的左三角形。
在最后一个游戏案例之后,整数0表示输入数据集的终止。
输出
你应该决定游戏是否可以解决。对于每一个游戏案例,打印游戏号码,冒号和空白,然后显示你的判断。如果游戏是可解决的,打印字符串“可能”。否则,请打印“不可能”,表示没有办法解决问题。
在每个游戏案例之间打印空白行。
注意:任何不需要的空行或空白都是不可接受的。


#include
   
   
    
      
//n表示游戏的大小,n小于等于5   
int n;  
//存放每一个格子  
int element[25][4];  
//每个状态  
int state[25];  
//存放的结果  
int result[25];   
//状态的个数  
int q;   
//初始化  
void initial()  
{  
    int i,j;  
    for(i=0;i<25;i++)  
    {  
        for(j=0;j<4;j++)  
        {  
            element[i][j]=0;  
        }  
        state[i]=0;   
        result[i]=0;  
    }   
    q=0;  
}   
//搜索到ipos位   
int DFS(int ipos)  
{  
     int i;  
     if(ipos==n*n)             //遍历
     {  
         return 1;  
     }  
     else  
     {  
                              //在ipos位把每个状态放一次  
         for(i=0;i
    
    
     
     =n)    //不在最上,垂直对比
               {  
                     if(element[result[ipos-n]][2]!=element[i][0])  
                     {  
                            continue;  
                     }  
               }   
               if(ipos%n!=0)  //不在最左,水平对比
               {  
                      if(element[result[ipos-1]][1]!=element[i][3])  
                      {  
                             continue;  
                      }  
               }  
               state[i]--;                //此类方格-1
               result[ipos]=i;            //iPos放第i个方格
               if(backtrack(ipos+1)==1)   //下一格
                    return 1;  
               state[i]++;                //恢复该类型方格一个,便于下次搜索
            }    
         }   
     }  
     return 0;  
}  
int main()  
{  
    int i,j,index;  
    index=0;  
    int top,right,bottom,left;  
    scanf("%d",&n);  
    while(n)  
    {  
          initial();  
          index++;  
          for(i=0;i
     
     
      
      1)  
          {  
             printf("\n");   
          }   
          printf("Game %d: ",index);  
          if(backtrack(0))  
          {  
             printf("Possible\n");   
          }  
          else  
          {  
             printf("Impossible\n");   
          }  
          scanf("%d",&n);  
    }  
   
    system("pause");  
    return 0;   
}  
     
     
    
    
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值