[TOJ 1965] Gnome Tetravex
来源:互联网 发布:java 高级编程 书籍 编辑:程序博客网 时间:2024/06/05 15:03
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 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
25 9 1 44 4 5 66 8 5 40 4 4 321 1 1 12 2 2 23 3 3 34 4 4 40
Output for the Sample Input
Game 1: PossibleGame 2: Impossible
直接进行搜索即可。
#include<cstring>#include<iostream>using namespace std;int n;int a[25][4];int b[25];int pos[25];int cnt;int dfs(int dep){ int i; if(dep==n*n) { return 1; } else { for(i=0;i<cnt;i++) { if(b[i]==0) { continue; } else { if(dep>=n) { if(a[pos[dep-n]][2]!=a[i][0]) { continue; } } if(dep%n!=0) { if(a[pos[dep-1]][1]!=a[i][3]) { continue; } } b[i]--; pos[dep]=i; if(dfs(dep+1)==1) return 1; else b[i]++; } } } return 0;}int main(){ int i,j,cas=0; int e,r,t,y; while(cin>>n,n) { if(cas) cout<<endl; cas++; cnt=0; memset(b,0,sizeof(b)); memset(a,0,sizeof(a)); memset(pos,0,sizeof(pos)); for(i=0;i<n*n;i++) { cin>>e>>r>>t>>y; for(j=0;j<cnt;j++) { if(a[j][0]==e&&a[j][1]==r&&a[j][2]==t&&a[j][3]==y) { b[j]++; break; } } if(j==cnt) { a[cnt][0]=e; a[cnt][1]=r; a[cnt][2]=t; a[cnt][3]=y; b[cnt]=1; cnt++; } } if(dfs(0)) { cout<<"Game "<<cas<<": Possible"<<endl; } else { cout<<"Game "<<cas<<": Impossible"<<endl; } }}
- [TOJ 1965] Gnome Tetravex
- Gnome Tetravex
- Gnome Tetravex
- zoj 1008 Gnome Tetravex
- 1008 Gnome Tetravex
- ZOJ 1008 Gnome Tetravex
- ZOJ1008 Gnome Tetravex
- zju 1008 Gnome Tetravex
- zoj 1008 Gnome Tetravex
- ZOJ 1008Gnome Tetravex
- zju1008题解Gnome Tetravex
- ZOJ1008 Gnome Tetravex
- ZOJ 1008 Gnome Tetravex
- ZOj 1008 Gnome Tetravex
- 1008-Gnome Tetravex -zju
- zoj1008-gnome tetravex
- ZOJ-1008-Gnome Tetravex
- Gnome Tetravex zoj
- C# 窗体间传值方法大汇总(转)
- hdu2066-一个人的旅行(迪杰斯特拉求最短路)
- 《失恋33天》励志一生
- Qt中Q_PROPERTY
- day22
- [TOJ 1965] Gnome Tetravex
- intelliJ一键部署远程tomcat
- iOS项目开发实战——实现视图切换动画
- Codeforces 455B
- poj 3259 bellman最短路判断有无负权回路
- 排序算法和查询算法的介绍
- hdoj 1905 Pseudoprime numbers
- 简单工厂,工厂模式和抽象工厂模式
- C++中虚函数和非虚函数重载在继承时的区别