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.

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)

#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;
}

#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 ;
}

原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/9152500.html

时间: 2024-08-07 00:46:41

ZOJ Problem Set - 1008 Gnome Tetravex (DFS+剪枝)的相关文章

zoj 1008 Gnome Tetravex (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 m

ZOJ 1008 Gnome Tetravex(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 m

[ZOJ 1008]Gnome Tetravex (dfs搜索 + 小优化)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008 题目大意:给你n*n的矩阵,每个格子里有4个三角形,分别是上右下左,每个三角形里面标记了数字,问你能否通过移动这n*n个格子,使得相邻两个三角形具有相同的数字? dfs暴搜,dfs(x,y)代表当前要放(x,y)这个位置. 然后枚举给定的每个格子. 如果说可以放,就dfs下一个格子. 这一道题需要把相同的格子压缩起来,也就是说为了节省时间,可以记录相同的格

ZOJ 1008 Gnome Tetravex (DFS + 剪枝)

Gnome Tetravex 题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 题意:有N*N个方格,每个方格分为上下左右四个部分,每个部分填数字.现在要求重排方块,使得每两个有边相连的方块对应的数字相同. 思路:就是一个简单的搜索,我想了个剪枝,将上下左右四个方向上每个数字对应的是哪几个方块记录下来,但是这个剪枝并没有起很大的作用,还是T了.后来才发现,如果有很多个方块是相同的,会重复搜索,所以需要将相同的方块一起处

ZOJ1008 Gnome Tetravex(DFS)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008 题意: 哈特近来一直在玩有趣的 Gnome Tetravex 游戏.在游戏开始时,玩家会得到 n×n(n≤5) 个正方形.每个正方形都被分成 4个标有数字的三角形(数字的范围是 0到9).这四个三角形分 别被称为"左三角形"."右三角形"."上三角形"和"下三角形".例如,图 2.12(

zoj 1008 Gnome Tetravex

开放式存储阵列为每平方米有几个,否则,超时-- #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <st

1008 Gnome Tetravex

练习使用DPS的题,不知道有无别的做法,思路不复杂.形式是统计并且进行数字配对. 1 #include <stdio.h> 2 3 int m,n,f,sub[25][4],note[25],ans[25]; 4 5 void ini(){ 6 int i,j,top,right,bottom,left; 7 for(i=0;i<25;i++){ 8 for(j=0;j<4;j++) 9 sub[i][j]=0; 10 note[i]=0; 11 ans[i]=0; 12 } 13

DFS+剪枝 HDOJ 5323 Solve this interesting problem

题目传送门 1 /* 2 题意:告诉一个区间[L,R],问根节点的n是多少 3 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - len -1,r]; 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 #include <queue> 10

POJ 1564 Sum It Up (DFS+剪枝)

 Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5820   Accepted: 2970 Description Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4