zoj1008 dfs
来源:互联网 发布:js 导出表格到excel 编辑:程序博客网 时间:2024/05/29 08:48
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
刚开始时看到时间限制为10s,以为爆搜就可以过了,提交后果断超时了。后来自己写了这样一组测试数据:
n=5,然后前24个方块均为1 1 1 1,最后一个方块为0 0 0 0……这才发现需要优化。很显然我们可以合并相同的方块,这样在进行试探性搜索时就可以避免在同一位置重复放置相同的方块了。
#include <iostream>#include<cstring>#include<algorithm>#include<cstdio>using namespace std;struct squre{ int top,right,bottom,left; int count;}s[30];bool visit[30],ok;int map[6][6],n,m;bool path(int i,int j,int k){ if(i>1&&s[k].top!=s[map[i-1][j]].bottom) return false; if(j>1&&s[k].left!=s[map[i][j-1]].right) return false; return true;}bool isEqual(squre &a,squre &b){ if(a.top==b.top&&a.right==b.right&&a.bottom==b.bottom&&a.left==b.left) return true; else return false;}void dfs(int i,int j){ if(i>n) { ok=true; return; } if(ok) return; for(int k=1;k<=m;k++) { if(s[k].count&&path(i,j,k)) { map[i][j]=k; s[k].count--; if(j==n) dfs(i+1,1); else dfs(i,j+1); s[k].count++; } }}int main(){ int Case=1; while(scanf("%d",&n),n) { m=1; for(int i=1;i<=n*n;i++) { scanf("%d%d%d%d",&s[m].top,&s[m].right,&s[m].bottom,&s[m].left); s[m].count=1; for(int j=1;j<m;j++)//合并相同的方块 if(isEqual(s[m],s[j])) { s[j].count++; m--; break; } m++; } m--; ok=false; dfs(1,1); if(Case>1) printf("\n"); if(ok) printf("Game %d: Possible\n",Case++); else printf("Game %d: Impossible\n",Case++); } return 0;}
- zoj1008 dfs
- ZOJ1008 Gnome Tetravex DFS
- ZOJ1008 Gnome Tetravex(DFS)
- ZOJ1008 Gnome Tetravex(dfs)
- ZOJ1008-Gnome Tstravex(dfs)
- zoj1008
- zoj1008
- ZOJ1008
- ZOJ1008
- ZOJ1008
- zoj1008 gnome tetravex 很久之前的dfs
- zoj1008(深搜)
- ZOJ1008 Gnome Tetravex
- ZOJ1008解题报告
- ZOJ1008 Gnome Tetravex
- zoj1008-gnome tetravex
- zoj1008 Gnome Tetravex
- ZOJ1008(深搜)
- 回溯法1--算法框架
- SD卡协议学习点滴(二)
- Oracle delete高水位线处理问题
- SD卡协议学习点滴(三)
- 安卓学习笔记(一)--搭建安卓开发环境
- zoj1008 dfs
- iOS中异步函数的单元测试
- 常用设计模式之简单工厂模式
- 那点单实例的事
- Linux grep命令
- MMC卡驱动分析
- android硬件加速器及其问题小结
- Android - Calendar Provider(未完待续)
- centos更改设置默认为root用户