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能否由其 ...
随机推荐
- Mysql update 错误
今天在工作的时候发现自己update 一个表的某个字段超时,想了好久,首先想到的办法是,延长操作时间: mysql> set innodb_lock_wait_timeout=100 mysql ...
- Longest Consecutive Sequence 解答
Question Given an unsorted array of integers, find the length of the longest consecutive elements se ...
- Hdu5381-The sum of gcd(莫队)
题意我就不说了 解析: 莫队,先预处理出以i为右端点的区间的gcd值,有一些连续的区间的gcd值是相同的,比如[j,i],[j+1,i],[j+2,i]的gcd值是相同的,我们可以把[j,j+2] ...
- JUnit基础及第一个单元测试实例(JUnit3.8)
单元测试 单元测试(unit testing) ,是指对软件中的最小可测试单元进行检查和验证. 单元测试不是为了证明您是对的,而是为了证明您没有错误. 单元测试主要是用来判断程序的执行结果与自己期望的 ...
- Bin & Jing in wonderland(概率,组合数学)
Problem 2103 Bin & Jing in wonderland Accept: 201 Submit: 1048 Time Limit: 1000 mSec Memor ...
- 仿桌面通知pnotify插件
在做网站的时候,alert弹出框是非常常见的情形.但是,有些情况下,弹框对用户来说是并不友好的.调研了几个其他的提示插件了,发现pnotify比较好用,可配置性也高. 使用示例: <!DOCTY ...
- Docker快速搭建neural style环境
## 概览 相关的代码都在Github上,请参见我的Github,https://github.com/lijingpeng/neural-style 敬请多多关注哈~~~ ## Docker镜像构建 ...
- Makefile 工程管理
Makefile 工程管理 Makefile 规则 --变量 在Makefile中,用户除了可以自己定义变量外,还可以使用存在系统已经定义好的默认变量 $^:代表所有的依赖文件 $@:代表目标 $&l ...
- 【jquery ,ajax,php】加载更多实例
jquery $(function() { //初始化 getData(0); var index = 1; $("#more").click(function() { getDa ...
- UILabel自适应高度,自动换行
CGRect rect; rect = self.labelInfo.frame; //UILabel高度自适应 rect.size.height = [self.labelInfo.text bou ...