ZOJ 1008 Gnome Tetravex (使用状态进行DFS)

来源:互联网 发布:linux php环境 编辑:程序博客网 时间:2024/05/24 04:16
Gnome Tetravex

Time Limit: 10 Seconds      Memory Limit: 32768 KB

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


Source: Asia 2001, Shanghai (Mainland China)


自己写了一个自认为剪枝很强的。。自己构造的数据也秒跑的。。但是却在OJ T了。。。觉得应该是哪里越界了或者死了。。。但一直没发现哪里有问题。。

参考了前辈的写法后。。发现自己剪枝剪了很多地方,有一个最重要的去没减!那就是重合状态!尽管很多随机数据都能秒过,但发现一旦出现了大量重合状态,速度简直感人。。。

一下就是偶然发现的一组TLE数据

5
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 1 1 1
1 1 1 1 
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1


最后把重合状态排除掉后,只跑了500ms就AC了。算是比较优秀的剪枝。

#include<stdio.h>#include<string.h>int P[6][6][4];int K[11][11][11][11];int A[11][11][30][2];int B[11][30][3];int C[11][30][3];int mark;int G[30][4];int num;int n;void addK(int a,int b,int c,int d){K[a][b][c][d]++;} void delK(int a,int b,int c,int d){K[a][b][c][d]--;}void addA(int a,int d){A[d][a][0][1]++;}void delA(int a,int d){A[d][a][0][1]--;}void addB(int d){B[d][0][1]++;}void delB(int d){B[d][0][1]--;}void addC(int a){C[a][0][1]++;}void delC(int a){C[a][0][1]--;}int T;int hh=0;void dfs(int cnt){if(mark==1)return;if(cnt==n*n+1){mark=1;return;}int x=(cnt-1)/n+1;int y=(cnt-1)%n+1;int a,b,c,d,e,f,g;if(cnt==1){for(a=1;a<=num;a++){c=G[a][0];d=G[a][1];e=G[a][2];f=G[a][3];if(y+1>n||B[d][0][1]>0)if(x+1>n||C[e][0][1]>0){delK(c,d,e,f);delA(c,f);delB(f);delC(c);P[x][y][0]=c;P[x][y][1]=d;P[x][y][2]=e;P[x][y][3]=f;dfs(cnt+1);if(mark==1)return;addK(c,d,e,f);addA(c,f);addB(f);addC(c);}}}else{if(x==1){g=P[x][y-1][1];if(B[g][0][1]>0){for(a=1;a<=B[g][0][0];a++){c=B[g][a][0];d=B[g][a][1];e=B[g][a][2];f=g;    if(K[c][d][e][f]>0)    if(y+1>n||B[d][0][1]>0)        if(x+1>n||C[e][0][1]>0)        {        delK(c,d,e,f);delA(c,f);delB(f);delC(c);        P[x][y][0]=c;P[x][y][1]=d;P[x][y][2]=e;P[x][y][3]=f;        dfs(cnt+1);if(mark==1)return;        addK(c,d,e,f);addA(c,f);addB(f);addC(c);        }}}}else if(y==1){g=P[x-1][y][2];if(C[g][0][1]>0){for(a=1;a<=C[g][0][0];a++){c=g;d=C[g][a][0];e=C[g][a][1];f=C[g][a][2];    if(K[c][d][e][f]>0)    if(y+1>n||B[d][0][1]>0)        if(x+1>n||C[e][0][1]>0)        {        delK(c,d,e,f);delA(c,f);delB(f);delC(c);        P[x][y][0]=c;P[x][y][1]=d;P[x][y][2]=e;P[x][y][3]=f;        dfs(cnt+1);if(mark==1)return;        addK(c,d,e,f);addA(c,f);addB(f);addC(c);        }}}}else{f=P[x][y-1][1];c=P[x-1][y][2];if(A[f][c][0][1]>0){for(a=1;a<=A[f][c][0][0];a++){d=A[f][c][a][0];e=A[f][c][a][1];if(K[c][d][e][f]>0)    if(y+1>n||B[d][0][1]>0)        if(x+1>n||C[e][0][1]>0)        {        delK(c,d,e,f);delA(c,f);delB(f);delC(c);        P[x][y][0]=c;P[x][y][1]=d;P[x][y][2]=e;P[x][y][3]=f;        dfs(cnt+1);if(mark==1)return;        addK(c,d,e,f);addA(c,f);addB(f);addC(c);        }}}}}}int main(){int a,b,c,d,e,f,g,h;scanf("%d",&n);T=0;while(n!=0){T++;memset(K,0,sizeof(K));memset(P,0,sizeof(P));memset(A,0,sizeof(A));memset(B,0,sizeof(B));memset(C,0,sizeof(C));memset(G,0,sizeof(G));mark=0;num=0;b=n*n;for(a=1;a<=b;a++){scanf("%d%d%d%d",&c,&d,&e,&f);K[c][d][e][f]++;A[f][c][0][1]++;B[f][0][1]++;C[c][0][1]++;if(K[c][d][e][f]==1){   num++;   G[num][0]=c;G[num][1]=d;G[num][2]=e;G[num][3]=f;   h=++A[f][c][0][0];A[f][c][h][0]=d;A[f][c][h][1]=e;   h=++B[f][0][0];B[f][h][0]=c;B[f][h][1]=d;B[f][h][2]=e;       h=++C[c][0][0];C[c][h][0]=d;C[c][h][1]=e;C[c][h][2]=f;}}dfs(1);if(mark==1){if(T==1)printf("Game %d: Possible",T);else printf("\n\nGame %d: Possible",T);}else{if(T==1)printf("Game %d: Impossible",T);else printf("\n\nGame %d: Impossible",T);}scanf("%d",&n);}return 0;}


看了看前辈的写法,很简洁,没这么多乱七八糟的剪枝。虽然跑了1700多ms。但这种简洁思路值得借鉴。

#include<stdio.h>  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;  }   int backtrack(int ipos)  {       int i;       if(ipos==n*n)       {           return 1;       }       else       {            for(i=0;i<q;i++)           {              if(state[i]==0)              {                 continue;               }               else              {                 if(ipos>=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]--;                  result[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<n*n;i++)            {               scanf("%d %d %d %d",&top,&right,&bottom,&left);               for(j=0;j<q;j++)               {                    if(element[j][0]==top&&element[j][1]==right&&element[j][2]==bottom&&element[j][3]==left)                    {                             state[j]++;                               break;                    }               }               if(j==q)               {                    element[q][0]=top;                    element[q][1]=right;                    element[q][2]=bottom;                    element[q][3]=left;                    state[q]=1;                    q++;                }            }            if(index>1)            {               printf("\n");             }             printf("Game %d: ",index);            if(backtrack(0))            {               printf("Possible\n");             }            else            {               printf("Impossible\n");             }            scanf("%d",&n);      }       return 0;   }  




原创粉丝点击