Gnome Tetravex
来源:互联网 发布:清风网络 编辑:程序博客网 时间:2024/04/29 00:02
Total Submit: 1907 Accepted Submit: 397
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
My Solution
#include <stdio.h>
#include <string.h>
struct Node
{
int top, right, bottom, left;
};
struct Node Squares[5][5];
int pos[5][5];
struct Node* GetFromPos(int N, int Pos)
{
return &Squares[Pos/N][Pos%N];
}
int FindSolution(int N, int startpos)
{
int i, j, a, b;
if(startpos == N*N) return 1;
for(i = 0; i < N*N; i++){
for(j = startpos - 1; j >= 0; j--)
if(i == pos[j/N][j%N]) break;
if(j < 0){
a = startpos / N; b = startpos % N;
pos[a][b] = i;
if(a - 1 >= 0)
if(GetFromPos(N, pos[a-1][b])->bottom != GetFromPos(N, pos[a][b])->top) continue;
if(b - 1 >= 0)
if(GetFromPos(N, pos[a][b-1])->right != GetFromPos(N, pos[a][b])->left) continue;
if(FindSolution(N, startpos+1)) return 1;
}
}
return 0;
}
int main()
{
struct Node *pNode;
int currpos;
int N, i, j;
int count = 1;
while(1){
scanf("%d", &N);
if(N == 0) break;
for(i = 0; i < N*N; i++){
pNode = &Squares[i/N][i%N];
scanf("%d%d%d%d", &pNode->top, &pNode->right, &pNode->bottom, &pNode->left);
}
if(FindSolution(N, 0)){
printf("Game %d: Possible/n/n", count++);
}else{
printf("Game %d: Impossible/n/n", count++);
}
}
return 0;
}
郁闷,超时了!
- 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
- zoj 1008 Gnome Tetravex
- 项目组启动
- 第一次会议
- 2003服务器防范海洋木马
- 实现了一个完整的chain 链表
- SecureCRT下的批命令脚本
- Gnome Tetravex
- 今天是我第一次使用播客
- 实现了两个普通的队列,一个优先权队列
- 网上的一个面试题 (c#)(配有解答)
- 软件开发的基本内容(1)——数据采集
- 关于Windows与Linux的Weblogic组合开发
- 程序员的进化
- 关于SOA
- Problem H: Largest Rectangle in a Histogram