ZOJ1008解题报告
来源:互联网 发布:网络最火的歌2017 编辑:程序博客网 时间:2024/06/05 09:55
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<=5,故用element[25][4]方块最多25块,每一块有4个三角形,顶三角、右三角、底三角和左三角,对应的坐标分别为0,1,2,3。用state来保存每一种类型的方块出现的次数。
backtrack这个回溯函数,从ipos=0处进行搜索,知道ipos==n*n,这样就到了终止条件。
在每个ipos位置,分别放入各个状态的方块(要记录,用完一个状态的方块,那个状态的方块个数减去1;用result记录当前放入的方块的类型,并与正上方的方块、正左方的方块进行比对,看可否放入,不能放入返回0,否则返回1)。
代码如下:
#include<stdio.h>//n表示游戏的大小,n小于等于5 int n;//存放每一个格子int element[25][4];//每个状态int state[25];//存放的结果int result[25]; //状态的个数int q; //初始化void initial(){ int i,j; for(i=0;i<25;i++) { for(j=0;j<4;j++) { element[i][j]=0; } state[i]=0; result[i]=0; } q=0;} //搜索到ipos位 int backtrack(int ipos){ int i; if(ipos==n*n) { return 1; } else { //在ipos位把每个状态放一次 for(i=0;i<q;i++) { if(state[i]==0) { continue; } else { //判断能否符合要求 if(ipos>=n) { if(element[result[ipos-n]][2]!=element[i][0]) { continue; } } if(ipos%n!=0) { if(element[result[ipos-1]][1]!=element[i][3]) { continue; } } state[i]--; result[ipos]=i; if(backtrack(ipos+1)==1) return 1; state[i]++; } } } return 0;}int main(){ int i,j,index; index=0; int top,right,bottom,left; scanf("%d",&n); while(n) { initial(); index++; for(i=0;i<n*n;i++) { scanf("%d %d %d %d",&top,&right,&bottom,&left); for(j=0;j<q;j++) { if(element[j][0]==top&&element[j][1]==right&&element[j][2]==bottom&&element[j][3]==left) { state[j]++; break; } } if(j==q) { element[q][0]=top; element[q][1]=right; element[q][2]=bottom; element[q][3]=left; state[q]=1; q++; } } //阴险的,就是每个结果之间要有空白行 if(index>1) { printf("\n"); } printf("Game %d: ",index); if(backtrack(0)) { printf("Possible\n"); } else { printf("Impossible\n"); } scanf("%d",&n); } system("pause"); return 0; }
这道题目有个非常阴险的地方,就是输出之间还要有一个空白行。在这一处PE了,太崩溃了,不该的。浪费了一次。
- ZOJ1008解题报告
- zoj1008
- zoj1008
- ZOJ1008
- ZOJ1008
- ZOJ1008
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- zoj1008 dfs
- zoj1008(深搜)
- Antiprime解题报告
- expr解题报告
- 华容道解题报告
- Android OpenGL ES(十):绘制三角形Triangle
- 虚函数表工作原理(1)
- 2012年2月28日报资源错误
- SET LOCK_TIMEOUT
- 【C/C++】概念:重载(overload),覆盖(override),隐藏(hide)的区别
- ZOJ1008解题报告
- 引用和指针的区别,*和&操作符
- Android_Intent详解
- redo 和 undo 之一
- C#使用sqlite
- 图片的存储与传递
- Android 小工具以及Context常用常量
- 深入理解C++中的mutable关键字
- 使用m2e将工程转化为maven工程后eclipse报Plugin execution not covered by lifecycle configuration:xxx plugin问题的解决方法