Gnome Tetravex Time limit: Seconds Memory Limit: 32768 KB
Hart is engaged in playing a interesting game, Gnome Tetravex, these days. The game, at the beginning, and the player is given n*n squares. Each square was divided into four triangles marked four numbers (range from 0 to 9). In a square, the triangles is 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 and 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 isn't so hard. But indeed, Hart isn't accomplished in the game. He can finish the easiest game successfully. When facing + a more complex game, he can find the no-out.
One day, when Hart were 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-of-the-help him are to-tell him whether the game could be solved. If He is told the game was unsolvable, he needn ' t waste so much time on it.
Input
The Input file consists of several game cases. The first line of all 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 an D 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 to solve the problem.
Print a blank line between each game case.
Note:any unwanted blank lines or white spaces is 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
Test instructions: In a n*n rectangular region, the N*n small rectangle is re-spelled into a rectangle that conforms to the rule (connected to the left and right, with equal values up and down).
Idea: To judge a variety of small rectangles, followed by a spell.
Harvest: XOR: 1 When the two are not equal.
#include <cstdio>#include<iostream>#include<cstdlib>#include<algorithm>#include<ctime>#include<cmath>#include<string>#include<cstring>#include<stack>#include<queue>#include<list>#include<vector>#include<map>#include<Set>using namespacestd;Const intinf=0x3f3f3f3f;Const Doubleeps=1e-Ten;Const DoublePi=acos (-1.0);#defineMAXN 30structnode{intu, R, D, l;}; Node NODE[MAXN];intNUM[MAXN];intmap1[8][8];intN, CNT;BOOLFK;voidDfsintp) { if(FK)return; if(P = = nN) {FK=1; return; } for(inti =0; I < CNT; i++) { if(Num[i] = =0)Continue; intx = p/N; inty = p%N; if(y>0&& node[map1[x][y-1]].R ^ node[i].l)Continue; if(x>0&& node[map1[x-1][y]].d ^ node[i].u)Continue; Map1[x][y]=i; Num[i]--; DFS (P+1); if(FK)return ; Num[i]++; }}intMain () {intCAS =1; intu, R, L, D; intFlag =0; while(~SCANF ("%d", &n) &&N) {cnt=0; memset (num,0,sizeofnum); for(inti =0; i < n*n; i++) {scanf ("%d%d%d%d", &u, &r, &d, &l); intf =0; for(intj =0; J < CNT; J + +) { if(NODE[J].D = = d && node[j].l = = L && NODE[J].R = r && node[j].u = =u) {num[j]++; F=1; Break; } } if(!f) {NODE[CNT].D= D; NODE[CNT].L = l; NODE[CNT].R = R; NODE[CNT].U =u; NUM[CNT]=1; CNT++; }} FK=0; DFS (0); if(CAs >1) puts (""); printf ("Game%d:", cas++); if(FK) printf ("possible\n"); Elseprintf ("impossible\n"); } return 0;}
ZOJ 1008 Gnome Tetravex (DFS)