zoj1008 dfs

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

    剛開始時看到時間限制爲10s,以爲爆搜就可以過了,提交後果斷超時了。後來自己寫了這樣一組測試數據:

n=5,然後前24個方塊均爲1 1 1 1,最後一個方塊爲0 0 0 0……這才發現需要優化。很顯然我們可以合併相同的方塊,這樣在進行試探性搜索時就可以避免在同一位置重複放置相同的方塊了。

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

using namespace std;
struct squre
{
    int top,right,bottom,left;
    int count;
}s[30];
bool visit[30],ok;
int map[6][6],n,m;

bool path(int i,int j,int k)
{
    if(i>1&&s[k].top!=s[map[i-1][j]].bottom)
        return false;
    if(j>1&&s[k].left!=s[map[i][j-1]].right)
        return false;
    return true;
}

bool isEqual(squre &a,squre &b)
{
    if(a.top==b.top&&a.right==b.right&&a.bottom==b.bottom&&a.left==b.left)
        return true;
    else
        return false;
}

void dfs(int i,int j)
{
    if(i>n)
    {
        ok=true;
        return;
    }
    if(ok)
        return;
    for(int k=1;k<=m;k++)
    {
        if(s[k].count&&path(i,j,k))
        {
            map[i][j]=k;
            s[k].count--;
            if(j==n)
                dfs(i+1,1);
            else
                dfs(i,j+1);
            s[k].count++;
        }
    }
}

int main()
{
    int Case=1;
    while(scanf("%d",&n),n)
    {
        m=1;
        for(int i=1;i<=n*n;i++)
        {
            scanf("%d%d%d%d",&s[m].top,&s[m].right,&s[m].bottom,&s[m].left);
            s[m].count=1;
            for(int j=1;j<m;j++)//合併相同的方塊
                if(isEqual(s[m],s[j]))
                {
                    s[j].count++;
                    m--;
                    break;
                }
            m++;
        }
        m--;
        ok=false;
        dfs(1,1);
        if(Case>1)
            printf("\n");
        if(ok)
            printf("Game %d: Possible\n",Case++);
        else
            printf("Game %d: Impossible\n",Case++);
    }
    return 0;
}


 

發佈了117 篇原創文章 · 獲贊 45 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章