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-;
const double PI=acos(-1.0);
#define maxn 30 struct Node
{
int u, r, d, l;
};
Node node[maxn];
int num[maxn];
int map1[][];
int n, cnt;
bool fk;
void dfs(int p)
{
if(fk) return;
if(p == n*n)
{
fk = ;
return;
}
for(int i = ; i < cnt; i++)
{
if(num[i] == ) continue;
int x = p/n;
int y = p%n;
if(y> && node[map1[x][y-]].r ^ node[i].l) continue;
if(x> && node[map1[x-][y]].d ^ node[i].u) continue;
map1[x][y] = i;
num[i]--;
dfs(p+);
if(fk) return ;
num[i]++;
}
}
int main()
{
int cas = ;
int u, r, l, d;
int flag = ;
while(~scanf("%d",&n) && n)
{
cnt = ;
memset(num, , sizeof num);
for(int i = ; i < n*n; i++)
{
scanf("%d%d%d%d",&u, &r, &d, &l);
int f = ;
for(int j = ; j < cnt; j++)
{
if(node[j].d == d && node[j].l == l && node[j].r == r && node[j].u == u)
{
num[j]++;
f = ;
break;
}
}
if(!f)
{
node[cnt].d = d; node[cnt].l = l; node[cnt].r = r; node[cnt].u = u;
num[cnt] = ;
cnt++;
}
}
fk = ;
dfs();
if(cas > )
puts("");
printf("Game %d: ",cas++);
if(fk)
printf("Possible\n");
else
printf("Impossible\n");
}
return ;
}
ZOJ 1008 Gnome Tetravex(DFS)的更多相关文章
- [ZOJ 1008]Gnome Tetravex (dfs搜索 + 小优化)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008 题目大意:给你n*n的矩阵,每个格子里有4个三角形,分别是 ...
- ZOJ 1008 Gnome Tetravex(DFS)
题目链接 题意 : 将n*n个正方形进行排列,需要判断相邻的正方形的相邻三角形上边的数字是不是都相等. 思路 : 只知道是个深搜,一开始不知道怎么搜,后来看了题解才明白,就是说不是自己去搜,而是将给定 ...
- zoj 1008 Gnome Tetravex
开放式存储阵列为每平方米有几个,否则,超时-- #include <stdio.h> #include <string.h> #include <iostream> ...
- 1008 Gnome Tetravex
练习使用DPS的题,不知道有无别的做法,思路不复杂.形式是统计并且进行数字配对. #include <stdio.h> ][],note[],ans[]; void ini(){ int ...
- zoj 1008 暴力枚举求解dfs+优化
/* 现将相同的合并计数. 再枚举判断是否符合当cou==n*n是符合就退出 */ #include<stdio.h> #include<string.h> #define N ...
- Gnome Tetravex
zoj1008:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008 题目意思是有一个游戏,即给出一个图,该图是由n*n个 ...
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
Sum It Up Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- ZOJ 3436 July Number(DFS)
题意 把一个数替换为这个数相邻数字差组成的数 知道这个数仅仅剩一位数 若最后的一位数是7 则称原来的数为 July Number 给你一个区间 求这个区间中July Number的个数 ...
- [ZOJ 1003] Crashing Balloon (dfs搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3 题目大意:给你a,b两个数,问当b由约数1到100组成时,a能否由其 ...
随机推荐
- Hdu2425-Hiking Trip(优先队列搜索)
Hiking in the mountains is seldom an easy task for most people, as it is extremely easy to get lost ...
- Java Class 字节码文件结构详解
Class字节码中有两种数据类型: 字节数据直接量:这是基本的数据类型.共细分为u1.u2.u4.u8四种,分别代表连续的1个字节.2个字节.4个字节.8个字节组成的整体数据. 表:表是由多个基本数据 ...
- 12 个 Linux 进程管理命令介绍
执行中的程序在称作进程.当程序以可执行文件存放在存储中,并且运行的时候,每个进程会被动态得分配系统资源.内存.安全属性和与之相关的状态.可以有多个进程关联到同一个程序,并同时执行不会互相干扰.操作系统 ...
- C# 使用枚举获取对应的数组值时
using System; enum Move { walk, run } class Program { static float[] speedAry = { 50.0f, 200.0f }; p ...
- pyqt menu子级方向例子学习
代码和UI文件:http://yunpan.cn/QCkXbX8mnSNke(提取码:51e1) 图片如: 代码如下: from PyQt4 import QtCore,QtGui,Qt import ...
- Fabricate equation(dfs + 模拟)
Fabricate equation Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Other ...
- 我为什么放弃Go语言
有好几次,当我想起来的时候,总是会问自己:我为什么要放弃Go语言?这个决定是正确的吗?是明智和理性的吗?事实上我一直在认真思考这个问题. 开门见山地说,我当初放弃Go语言(golang),就是由于两个 ...
- EffectiveC#9--明白几个相等运算之间的关系
1.当你创建你自己的类型时(不管是类还是结构),你要定义类型在什么情况下是相等的.C#提供了4个不同的方法来断定两个对象是否是相等的 public static bool ReferenceEqual ...
- 整合 新浪 腾讯 人人 qq空间 分享地址
function snsShare(snsId, title, content, image, url) { var snsUrl; // 新浪 腾讯 要申请appkey switch (snsId) ...
- html表格,列表
1. 表格由 <table> 标签来定义.每个表格均有若干行(由 <tr> 标签定义),每行被分割为若干单元格(由 <td> 标签定义).字母 td 指表格数据(t ...