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

题意:在一个N*N矩形区域中,N*N个小矩形重新拼成一个符合规则(左右相连,上下相连值相等)的矩形。

思路:判断出有多种小矩形,接着一个一拼。

收获:异或:两者不相等时为1.

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std;

const int INF=0x3f3f3f3f;
const double eps=1e-10;
const double PI=acos(-1.0);
#define maxn 30

struct Node
{
    int u, r, d, l;
};
Node node[maxn];
int num[maxn];
int map1[8][8];
int n, cnt;
bool fk;
void dfs(int p)
{
    if(fk) return;
    if(p == n*n)
    {
        fk = 1;
        return;
    }
    for(int i = 0; i < cnt; i++)
    {
        if(num[i] == 0) continue;
        int x = p/n;
        int y = p%n;
        if(y>0 && node[map1[x][y-1]].r ^ node[i].l) continue;
        if(x>0 && node[map1[x-1][y]].d ^ node[i].u) continue;
        map1[x][y] = i;
        num[i]--;
        dfs(p+1);
        if(fk) return ;
        num[i]++;
    }
}
int main()
{
    int cas = 1;
    int u, r, l, d;
    int flag = 0;
    while(~scanf("%d",&n) && n)
    {
        cnt = 0;
        memset(num, 0, sizeof num);
        for(int i = 0; i < n*n; i++)
        {
            scanf("%d%d%d%d",&u, &r, &d, &l);
            int f = 0;
            for(int j = 0; j < cnt; j++)
            {
                if(node[j].d == d && node[j].l == l && node[j].r == r && node[j].u == u)
                    {
                        num[j]++;
                        f = 1;
                        break;
                    }
            }
            if(!f)
            {
            node[cnt].d = d; node[cnt].l = l; node[cnt].r = r; node[cnt].u = u;
            num[cnt] = 1;
            cnt++;
            }
        }
        fk = 0;
        dfs(0);
        if(cas > 1)
            puts("");
        printf("Game %d: ",cas++);
        if(fk)
            printf("Possible\n");
        else
            printf("Impossible\n");
    }
    return 0;
}
时间: 2024-07-28 19:57:56

ZOJ 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搜索 + 小优化)

题目链接: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了.后来才发现,如果有很多个方块是相同的,会重复搜索,所以需要将相同的方块一起处

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 divide

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

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(

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

ZOJ 1008

这一题用了DFS对每一种方法进行尝试,直到有一种成功的就possible: #include <iostream>#include "string.h"using namespace std;int diff;int card[26][4]; //用于记录不同卡片的上.右.下.左.方向的数字int cardnum[26]; //记录每一种卡片的个数int trueorder[26]; //记录已经排好序的卡片是哪一种卡片,int n; bool dfs(int now){ 

POJ 1071 &amp; HDU 1364 &amp; ZOJ 1019 Illusive Chase(DFS)

题目链接: POJ:http://poj.org/problem?id=1071 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1364 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=19 Description Tom the robocat is presented in a Robotics Exhibition for an enthusiastic audien