ZOJ 1008 Gnome Tetravex
2015-09-07 13:49
417 查看
B - Gnome Tetravex
Time Limit:10000MS Memory Limit:32768KB 64bit IO Format:%lld
& %llu
SubmitStatusPracticeZOJ
1008
Description
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里比较难的了。(个人感觉,大牛绕道)。
暴力的搜会超时。
这也是第一次使用剪枝。不过算法的思想不是自己独立想出来的,看了网上的题解才渐渐明白了一些。以后还要继续努力呀。
Time Limit:10000MS Memory Limit:32768KB 64bit IO Format:%lld
& %llu
SubmitStatusPracticeZOJ
1008
Description
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里比较难的了。(个人感觉,大牛绕道)。
暴力的搜会超时。
这也是第一次使用剪枝。不过算法的思想不是自己独立想出来的,看了网上的题解才渐渐明白了一些。以后还要继续努力呀。
#include <stdio.h> #include <string.h> #include <algorithm> #define N 55 using namespace std; typedef struct node { int t,r,d,l; bool operator==(const node &a) { return t==a.t&&r==a.r&&d==a.d&&l==a.l; } }node; node g ; node graph ; node tmp; int num ,issolve,k,n; void dfs(int cur,int n) { if(issolve) return ; if(cur==n*n) { issolve=1; return ; } int x=cur/n; int y=cur%n; for(int i=0;i<k;i++) { if(num[i]==0) continue; if(x>0&&g[i].t!=graph[x-1][y].d) continue; if(y>0&&g[i].l!=graph[x][y-1].r) continue; graph[x][y]=g[i]; num[i]--; dfs(cur+1,n); num[i]++; } return ; } int main() { int cnt=0; while(scanf("%d",&n)>0&&n) { if(cnt) puts(""); memset(num,0,sizeof(num)); for(int i=k=0;i<n*n;i++) { scanf("%d%d%d%d",&tmp.t,&tmp.r,&tmp.d,&tmp.l); int j; for( j=0;j<k;j++) { if(tmp==g[j]) { num[j]++; break; } } if(j==k) { g[k]=tmp; num[k++]=1; } } issolve=0; dfs(0,n); if(issolve) printf("Game %d: Possible\n",++cnt); else printf("Game %d: Impossible\n",++cnt); } return 0; }
相关文章推荐
- git生成ssh key及本地解决多个ssh key的问题
- JS 打开新窗口的方法
- 你的第一台负载均衡器 - 15分钟的入门课
- ubuntu下tcpdump总结
- ListView使用过程记录
- 用户·角色·权限·表
- Java代理模式
- Android软键盘强制弹出,隐藏输入法.
- ORACLE 全局索引和本地索引
- liunx下swf转换 相关问题____http://bbs.csdn.net/topics/390797316
- json集合字符串转对象
- Play 起步
- AFNetworking作用和用法详解
- zookeeper安装小记
- 多媒体
- 面试题 14
- 面试题(一)
- XssSniper 扩展介绍
- R语言数据框
- C++ 16进制转10进制