ZOJ Problem Set - 1008 Gnome Tetravex (DFS+剪枝)

ZOJ Problem Set - 1008

 

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.node

 


Fig. 1 The initial state with 2*2 squares ios

 

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.
ide

 


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.ui

 


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.spa

 


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.rest

 


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
0code

 


Output for the Sample Input

Game 1: Possibleblog

 

Game 2: Impossible
ci

 


Source: Asia 2001, Shanghai (Mainland China)

#include <iostream> #include <algorithm> #include <cstring>

using namespace std ; #define maxn 10000

struct node { int top , right, botom, left ; } ; int visit[maxn] ; node num[maxn] ; int Map[maxn] ; bool result_flag ; int n ; int a , b , c , d ; int total ; void DFS(int pos){ if(pos == n*n){ result_flag = true ; return; } if(result_flag){ return; } for(int i=0 ; i<total ; i++){ if(visit[i]){ if(pos == 0){ visit[i] -- ; Map[pos] = i ; DFS(pos+1) ; visit[i] ++ ; }else if(pos<n){ if(num[Map[pos-1]].right == num[i].left){ visit[i] -- ; Map[pos] = i ; DFS(pos+1) ; visit[i] ++ ; } }else if(pos%n == 0 ){ if(num[Map[pos-n]].botom == num[i].top){ visit[i] -- ; Map[pos] = i ; DFS(pos+1) ; visit[i] ++ ; } }else{ if(num[Map[pos-1]].right == num[i].left && num[Map[pos-n]].botom == num[i].top){ visit[i] -- ; Map[pos] = i ; DFS(pos+1) ; visit[i] ++ ; } } if(result_flag){ return; } } } return; } int main(){ int Case = 1 ; while(cin >> n && n ){ total = 0 ; memset(visit , 0 , sizeof(visit)) ; for(int i=0 ; i<n*n ; i++){ cin >> a >> b >> c >> d ; bool flag = false ; // 不加上會超時 // 因此必定要加上此處的剪枝
            for(int k=0 ; k<total ; k++){ if(num[k].top == a && num[k].right == b && num[k].botom == c && num[k].left == d){ visit[k] ++ ; flag = true ; break ; } } if(!flag){ num[total].top = a ; num[total].right = b ; num[total].botom = c ; num[total].left = d ; visit[total] ++ ; total ++ ; } } result_flag = false ; memset(Map , 0 , sizeof(Map)) ; DFS(0) ; if(Case != 1 ){ cout << endl ; } if(result_flag == true){ cout << "Game " << Case ++ << ": " << "Possible"<< endl ; }else{ cout << "Game " << Case ++ << ": " << "Impossible"<<endl ; } } return 0 ; }
/* 題意:給出一個矩形由n*n個小矩形,每一個小矩形由四個三角形組成,分別在上下左右,每一個三角形有一個數 字,經過調換這些矩形的位置,找出一種狀況能使得任意兩個相鄰的小矩形之間有公共邊的兩個三角形的值 同樣,能找出這張狀況則輸出possible,不然輸出impossible。 題解:DFS,搜索剪枝; n*n個位置分別對應填入n*n個矩形,從左往右,自上而下的順序依次搜索,對應每一個位置都遍歷一遍全部的 矩形,而且記錄該矩形的值。 注意:測試數據包含有重複的矩形,所以在搜索的時候若是沒有處理重複的矩形,則會出現一種狀況:遍歷 的兩種擺法中兩個相同的矩形分別在兩個位置,會由於沒有處理重複而經過顛倒兩個矩形的出現順序來從新 搜索一遍,其實是同樣的,這裏的剪枝很重要,不然會超時,估計數據有不少重複的矩形。 */ #include <cstdio> #include <cstring> #include <algorithm>

using namespace std; struct square { int top,right,bottom,left; }s[50];//記錄矩形

int n,q;//n記錄大矩形的邊,q記錄矩形的種類數
int vis[50];//記錄第i種矩形由多少個
int m[50];//記錄n*n個位置中對應的矩形的種類
bool flag;//記錄是否能找到相鄰狀態

void dfs(int pos)//pos指當前在位置
{ if (pos == n*n)//搜索到n*n使表示全部的位置都已經填滿
 { flag = true; return ; } if (flag) return ; for(int i=0; i<q; i++) {//遍歷全部存在的小矩形填入當前的pos位置中
        if (vis[i]) { if (pos==0)//位於第一個位置,則只需直接填入矩形i
 { vis[i]--;//第i種矩形數目減一
                m[pos] = i;//記錄當前pos位置填入的矩形種類爲i
                dfs(pos+1);//搜索下一個位置
                vis[i]++; } else if (pos < n)//當前位置位於第一行
 { if (s[m[pos-1]].right == s[i].left)//判斷與左邊是否能相鄰(pos-1表示當前位置的左邊)
 { vis[i]--; m[pos] = i; dfs(pos+1); vis[i]++; } } else if (pos % n == 0)//位於最左列,左邊沒有位置
 { if (s[m[pos-n]].bottom == s[i].top)//判斷與上方的矩形是否能相鄰(pos-n表示當前位置的上方)
 { vis[i]--; m[pos] = i; dfs(pos+1); vis[i]++; } } else {//其他的狀況都判斷是否能與上方和左邊的矩形相鄰
                if (s[m[pos-1]].right==s[i].left && s[m[pos-n]].bottom==s[i].top) { vis[i]--; m[pos] = i; dfs(pos+1); vis[i]++; } } if (flag) return ; } } } int main(void) { int cas=1,a,b,c,d; while (~scanf("%d",&n) && n) { memset(vis,0,sizeof(vis)); q = 0; for(int i=0; i<n*n; i++) { scanf("%d%d%d%d",&a,&b,&c,&d); int j; for(j=0; j<q; j++) { if (s[j].top==a && s[j].right==b && s[j].bottom==c && s[j].left==d) { vis[j]++; break; } } if (j == q) { s[q].top = a; s[q].right = b; s[q].bottom = c; s[q].left = d; vis[q]++; q++; } } flag = false; memset(m,0,sizeof(m)); dfs(0); if (cas != 1) printf("\n"); if (flag) printf("Game %d: Possible\n",cas); else printf("Game %d: Impossible\n",cas); cas++; } return 0; }
View Code

 

#include <iostream>#include <algorithm>#include <cstring>using namespace std ; #define maxn 10000struct node {    int top ,        right,        botom,        left ; } ; int visit[maxn] ; node num[maxn] ; int Map[maxn] ; bool result_flag ;int n ; int a , b , c , d ; int total ; void DFS(int pos){    if(pos == n*n){        result_flag = true ;         return;    }    if(result_flag){        return;    }    for(int i=0 ; i<total ; i++){        if(visit[i]){            if(pos == 0){                visit[i] -- ;                 Map[pos] = i ;                 DFS(pos+1) ;                 visit[i] ++ ;             }else if(pos<n){                if(num[Map[pos-1]].right == num[i].left){                    visit[i] -- ;                     Map[pos] = i ;                     DFS(pos+1) ;                     visit[i] ++ ;                 }            }else if(pos%n == 0 ){                if(num[Map[pos-n]].botom == num[i].top){                    visit[i] -- ;                     Map[pos] = i ;                     DFS(pos+1) ;                     visit[i] ++ ;                 }            }else{                if(num[Map[pos-1]].right == num[i].left && num[Map[pos-n]].botom == num[i].top){                    visit[i] -- ;                     Map[pos] = i ;                     DFS(pos+1) ;                     visit[i] ++ ;                 }            }            if(result_flag){                return;            }        }    }    return;}int main(){    int Case = 1 ;     while(cin >> n && n ){        total = 0 ;         memset(visit , 0 , sizeof(visit)) ;         for(int i=0 ; i<n*n ; i++){            cin >> a >> b >> c >> d ;            bool flag = false ;             //  不加上會超時              //  因此必定要加上此處的剪枝            for(int k=0 ; k<total ; k++){                if(num[k].top == a && num[k].right == b && num[k].botom == c && num[k].left == d){                    visit[k] ++ ;                     flag = true ;                     break ;                  }            }            if(!flag){                num[total].top = a ;                 num[total].right = b ;                 num[total].botom = c ;                 num[total].left = d ;                 visit[total] ++ ;                 total ++ ;             }        }        result_flag = false ;         memset(Map , 0 , sizeof(Map)) ;         DFS(0) ;        if(Case != 1 ){            cout << endl ;         }                if(result_flag == true){            cout << "Game " << Case ++ << ": " << "Possible"<< endl  ;         }else{            cout << "Game " << Case ++ << ": " << "Impossible"<<endl  ;        }    }    return 0 ; }