UVa1572 UVaLive6393 Self-Assembly

填坑系列(p.172)

注意“可以旋转和翻转”

然后将每个字母看成点

不然边数就是n^2级的

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<algorithm>
 5 #include<iostream>
 6 
 7 using namespace std;
 8 
 9 void setIO(const string& s) {
10     freopen((s + ".in").c_str(), "r", stdin);
11     freopen((s + ".out").c_str(), "w", stdout);
12 }
13 template<typename Q> Q read(Q& x) {
14     static char c, f;
15     for(f = 0; c = getchar(), !isdigit(c); ) if(c == '-') f = 1;
16     for(x = 0; isdigit(c); c = getchar()) x = x * 10 + c - '0';
17     if(f) x = -x;
18     return x;
19 }
20 template<typename Q> Q read() {
21     static Q x; read(x); return x;
22 }
23 
24 int ID(char a1, char a2) {
25     return (a1 - 'A') << 1 | (a2 == '-');
26 }
27 
28 int G[52][52];
29 
30 void AddEdge(char a1, char a2, char b1, char b2) {
31     if(a1 == '0' || b1 == '0') return;
32     int u = ID(a1, a2) ^ 1, v = ID(b1, b2);
33     G[u][v] = 1;
34 }
35 
36 int vis[52];
37 
38 bool dfs(int u) {
39     vis[u] = -1;
40     for(int v = 0; v < 52; v++) if(G[u][v]) {
41         if(vis[v] < 0) return 1;
42         if(!vis[v] && dfs(v)) return 1;
43     }
44     vis[u] = 1;
45     return 0;
46 }
47 
48 bool find() {
49     memset(vis, 0, sizeof vis);
50     for(int u = 0; u < 52; u++) if(!vis[u]) {
51         if(dfs(u)) return 1;
52     }
53     return 0;
54 }
55 
56 int main() {
57 #ifdef DEBUG
58     freopen("in.txt", "r", stdin);
59     freopen("out.txt", "w", stdout);
60 #endif
61     
62     int n;
63     while(scanf("%d", &n) == 1 && n) {
64         memset(G, 0, sizeof G);
65         char s[10];
66         for(int i = 0; i < n; i++) {
67             scanf("%s", s);
68             for(int i = 0; i < 4; i++) {
69                 for(int j = 0; j < 4; j++) if(i ^ j) {
70                     AddEdge(s[i << 1], s[i << 1 | 1], s[j << 1] , s[j << 1 | 1]);
71                 }
72             }
73         }
74         if(find()) puts("unbounded");
75         else puts("bounded");
76     }
77     
78     return 0;
79 }
View Code

 

posted @ 2015-12-27 22:26  Showson  阅读(130)  评论(0编辑  收藏  举报