ZOJ 1008 Gnome Tetravex(DFS)
来源:互联网 发布:千股千评软件下载 编辑:程序博客网 时间:2024/05/18 23:57
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
#include<stdio.h>#include<string.h>int map[30][4];int counting[30], mark[30];int DFS(int num,int n,int cur){if(num==n*n) return 1;for(int i=0;i<cur;i++){if(!counting[i]) continue; //如果用完就跳过if(num%n!=0){//把不是第一列的来判断if(map[i][3]!=map[mark[num-1]][1])continue;}if(num/n!=0){ //把不是第一行的来判断if(map[i][0]!=map[mark[num-n]][2])continue;}mark[num]=i; counting[i]--;if(DFS(num+1,n,cur)) return 1;else counting[i]++;}return 0;}int main(){int T=1;int n,up,down,left,right,cur;while(scanf("%d",&n)!=EOF&&n){cur=0; //初始化memset(map,0,sizeof(map));memset(mark,0,sizeof(mark));memset(counting,0,sizeof(counting));for(int i=0,j;i<n*n;i++){scanf("%d%d%d%d",&up,&right,&down,&left);for(j=0;j<cur;j++){if(up==map[j][0]&&right==map[j][1]&&down==map[j][2]&&left==map[j][3]){ //把相同的存入同一个数组,记录个数counting[j]++; break;}}if(j==cur){//把不同的存入数组map[j][0]=up;map[j][1]=right;map[j][2]=down; map[j][3]=left;counting[j]++; cur++;}}if(T>1)printf("\n");//用来处理格式问题if(DFS(0,n,cur)) printf("Game %d: Possible\n", T++);elseprintf("Game %d: Impossible\n", T++);}return 0;}
- ZOJ 1008 Gnome Tetravex(DFS)
- zoj 1008 Gnome Tetravex (dfs+剪枝)
- zoj 1008 Gnome Tetravex (DFS + 剪枝)
- ZOJ 1008 Gnome Tetravex (DFS + 剪枝)
- ZOJ 1008 Gnome Tetravex(DFS)
- ZOJ--1008:Gnome Tetravex(剪枝dfs)
- ZOJ 1008Gnome Tetravex DFS
- ZOJ 1008 Gnome Tetravex (使用状态进行DFS)
- ZOJ 1008(DFS+剪枝) Gnome Tetravex
- 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
- [浮点数加法运算]浮点数表示及运算
- 解决问题ModuleNotFoundError: No module named 'PyQt4'
- HTML5复习 (2)
- Android Studio自定义模板 做开发竟然可以如此轻松
- 团体程序设计天梯赛L2-021 点赞狂魔
- ZOJ 1008 Gnome Tetravex(DFS)
- MAC下PYCHARM环境中openCV不显示图片的问题
- 常用的AngularJS过滤器
- 纯代码布局控件
- AngularJS 控制器controller 连载3
- Spring boot项目 使用ip+port+contextPath进行访问的时候会直接显示出项目中的一些接口信息和相关数据
- 完全背包
- mysql练习
- 快速判断一个数是否是2的幂次方,若是,并判断出来是多少次方!