Im_hear

导航

zoj 1008

View Code
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int n;
 7 int kinds;
 8 bool ok;
 9 struct square{
10     int top,right,bottom,left;
11     int counts;
12 };
13 square way[30];
14 square record[30];
15 
16 void dfs(int pos);
17 
18 int main()
19 {    
20     square node;
21     int cas = 1;
22     bool flag = false;
23     while(cin >> n && n){
24         if(flag)cout << endl;
25         else flag = true;
26         
27         kinds = 0;
28         for(int i = 0; i < n*n; ++i){
29             cin >> node.top >> node.right >> node.bottom >> node.left;
30             node.counts = 1;
31             bool find = false;
32             for(int k = 0; k < kinds; ++k){
33                 if((way[k].top == node.top) && (way[k].right == node.right) 
34                     && (way[k].bottom == node.bottom) && (way[k].left == node.left)){
35                     find = true;
36                     ++way[k].counts;
37                     break;
38                 }
39             }
40             if(!find){
41                 way[kinds++] = node;            
42             }
43         }
44         ok = false;        
45         dfs(0);
46             
47         cout << "Game " << cas++ << ": " ;    
48         if(ok)cout << "Possible" << endl;
49         else cout << "Impossible" << endl;
50     }
51     return 0;
52 }
53 void dfs(int pos)
54 {
55     if(ok)return ;
56     if(pos == n*n){
57         ok = true;
58         return ;
59     }    
60     for(int i = 0; i < kinds ; ++i){
61         if(way[i].counts 
62             &&(pos%n == 0 || record[pos-1].right == way[i].left)
63             &&(pos/n == 0 || record[pos-n].bottom == way[i].top))
64         {
65             --way[i].counts;
66             record[pos] = way[i];
67             dfs(pos+1);
68             ++way[i].counts;
69         }
70     }
71 }

posted on 2012-04-20 20:32  Im_hear  阅读(202)  评论(0)    收藏  举报