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能否由其 ...
随机推荐
- Ubuntu 16.04 LTS 正式发布:系统将持续更新5年
Canonical 刚刚正式发布了Ubuntu 16.04 LTS (Xenial Xerus),这是一个长期支持版本,官方会提供长达5年的技术支持(包括常规更新/Bug修复/安全升级),一直到202 ...
- hdu 5640 King's Cake(模拟)
Problem Description It is the king's birthday before the military parade . The ministers prepared ...
- [转]Binarized Neural Networks_ Training Neural Networks with Weights and Activations Constrained to +1 or −1
原文: 二值神经网络(Binary Neural Network,BNN) 在我刚刚过去的研究生毕设中,我在ImageNet数据集上验证了图像特征二值化后仍然具有很强的表达能力,可以在检索中达到较好的 ...
- 酷狗音乐QQ显示(VC源代码)
效果图: 原理网上有,只是都是易语言,自己分析一下.知道原理. 但近期喜欢用酷狗听课就写这个小软件认出来,你自己能够随意改动. 不说了直接丢代码.. http://pan.baidu.com/s/1q ...
- JAVA wait(), notify(),sleep具体解释
在CSDN开了博客后,一直也没在上面公布过文章,直到前一段时间与一位前辈的对话,才发现技术博客的重要,立志要把CSDN的博客建好.但一直没有找到好的开篇的主题,今天再看JAVA线程相互排斥.同步的时候 ...
- 关于Latch
Latch是什么 Latch是SQL Server引擎保证内存中的结构的一致性的轻量同步机制.比如索引,数据页和内部结构(比如非叶级索引页).SQL Server使用Buffer Latch保护缓冲池 ...
- [Regex Expression] Match mutli-line number, number range
/^-?\d{,}\.\d+$/gm
- Android系统匿名共享内存Ashmem(Anonymous Shared Memory)在进程间共享的原理分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6666491 在前面一篇文章Android系统匿 ...
- CSS彻底研究(3) - 浮动,定位
Github pages 博文 CSS彻底研究(3)-浮动,定位 一 . 浮动float I . 定义及规则 float默认为none,对应标准流的情况.当float : left;时,元素就会向其父 ...
- Highcharts属性
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...