zoj 1008 Gnome Tetravex (dfs+剪枝)
来源:互联网 发布:微分销源码 编辑:程序博客网 时间:2024/05/25 08:14
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
感想:
这题数据还是比较强的,裸的dfs会超时,加一个相同的方块不重复搜就ok了。开始采用迭代的方法剪枝,竟然还是TLE,有点小抓狂了,后来预处理迭代过了,不过过得有点囧,跑到8000ms+了。其实可以采用把相同的方块只记录一次,然后记录下个数就够了。那样会快一点,2000ms+。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define maxn 10using namespace std;int n,m,ans,flag;bool vis[30];int next[30];struct Node{ int v[4];} node[30],mp[maxn][maxn];bool isok(int x,int y,int k){ int i,j; if(x>1) { if(mp[x-1][y].v[1]!=node[k].v[3]) return false ; } if(y>1) { if(mp[x][y-1].v[2]!=node[k].v[0]) return false ; } return true ;}void dfs(int x,int y){ int i,j; if(flag) return ; if(x>n) { flag=1; return ; } if(y>n) dfs(x+1,1); else { for(i=1; i<=m; i++) { if(!vis[i]) { if(isok(x,y,i)) { vis[i]=1; mp[x][y]=node[i]; dfs(x,y+1); vis[i]=0; } i=next[i]; i--; } } }}bool cmp(const Node&x1,const Node&x2){ if(x1.v[0]!=x2.v[0]) return x1.v[0]<x2.v[0]; if(x1.v[1]!=x2.v[1]) return x1.v[1]<x2.v[1]; if(x1.v[2]!=x2.v[2]) return x1.v[2]<x2.v[2]; return x1.v[3]<x2.v[3];}int main(){ int i,j,t=0; while(scanf("%d",&n),n) { t++; m=n*n; for(i=1; i<=m; i++) { scanf("%d%d%d%d",&node[i].v[0],&node[i].v[1],&node[i].v[2],&node[i].v[3]); } sort(node+1,node+m+1,cmp); // 将node排序 for(i=1;i<=m;i++) // 预处理node的下一个与它不同的下标 { for(j=i+1;j<=m;j++) { if(node[i].v[0]==node[j].v[0]&&node[i].v[1]==node[j].v[1]&&node[i].v[2]==node[j].v[2]&&node[i].v[3]==node[j].v[3]) continue ; else break ; } next[i]=j; } flag=0; memset(vis,0,sizeof(vis)); dfs(1,1); if(t>1) printf("\n"); printf("Game %d: ",t); if(flag) printf("Possible\n"); else printf("Impossible\n"); } return 0;}
- zoj 1008 Gnome Tetravex (dfs+剪枝)
- zoj 1008 Gnome Tetravex (DFS + 剪枝)
- ZOJ 1008 Gnome Tetravex (DFS + 剪枝)
- ZOJ--1008:Gnome Tetravex(剪枝dfs)
- ZOJ 1008(DFS+剪枝) Gnome Tetravex
- ZOJ 1008 Gnome Tetravex(DFS)
- ZOJ 1008 Gnome Tetravex(DFS)
- ZOJ 1008Gnome Tetravex DFS
- ZOJ 1008 Gnome Tetravex (使用状态进行DFS)
- zoj 1008 Gnome Tetravex (dfs+枚举)
- zoj 1008 Gnome Tetravex
- ZOJ 1008 Gnome Tetravex
- zoj 1008 Gnome Tetravex
- ZOJ 1008Gnome Tetravex
- ZOJ 1008 Gnome Tetravex
- ZOj 1008 Gnome Tetravex
- ZOJ-1008-Gnome Tetravex
- zoj 1008 Gnome Tetravex
- 使用webdriver中的JavascriptExecutor执行js改变DOM属性
- linux系统ssh的验证与安装
- HDU 2489 Prim+DFS
- 深入云存储系统Swift核心组件:Ring实现原理剖析
- hdu4640 && hdu4281
- zoj 1008 Gnome Tetravex (dfs+剪枝)
- java jdk API中英文下载
- 类似淘宝的星星打分
- PHP程序员函数注释规格
- C# BitArray 实例
- hdu 4276 The Ghost Blows Light(树形DP)
- 游戏编程入门学习笔记34——中场休息——近期的一些心得
- opencv中有和matlab中conv2一样功能的函数吗?
- 正则表达式(上篇)