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能否由其 ...
随机推荐
- 【转】NAT路由器打洞原理
什么是打洞,为什么要打洞 由于Internet的快速发展 IPV4地址不够用,不能每个主机分到一个公网IP 所以使用NAT地址转换. 下面是我在网上找到的一副图 一般来说都是由私网内主机(例如上图中“ ...
- Linux SD/MMC/SDIO驱动分析
一.SD/MMC/SDIO概念区分 SD(SecureDigital)与 MMC(MultimediaCard) SD 是一种 flash memory card 的标准,也就是一般常见的 SD 记忆 ...
- jsp页面判断文件上传类型
<script language="javascript" type="text/javascript"> function check_file( ...
- MVC 简单数据传递
Mode: namespace MVCDemo.Models { public class Data { //申明为静态 归类所有,取数据不要实例化 ; public static string st ...
- 【Oracle】逻辑结构(TableSpace→Segment→Extent→Block)
一.逻辑体系结构图 二.逻辑结构图组成介绍 从上表能够看出,一个数据库是由多个表空间(tablespace)组成,一个表空间又由多个段(segment)组成,一个段又由多个区(extent)组成 ...
- HDU 1248 寒冰王座(全然背包:入门题)
HDU 1248 寒冰王座(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票 ...
- ACM-简单题之Ignatius and the Princess II——hdu1027
转载请注明出处:http://blog.csdn.net/lttree Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Othe ...
- Js异步级联选择框实践方法
HTML: <li> <span>所在地区:</span> <select name="prov" id="ddl_prov&q ...
- jQuery对象和dom对象之间的相互转化
var domObj = document.getElementById("demo"); var $Obj = $("#demo"); DOM转jQuery: ...
- dtree基础
最近用到了dtree来建立树,纠结过好久后,终于有了些门道,下面的总结希望对咪咪们有些帮助: dtree用来建立静态树或者动态树都是很方便的,老外给提供了整个的JS,然后我们只是操心这个树中存放的元素 ...