Problem UVA1572-Self-Assembly(拓扑排序)

Problem UVA1572-Self-Assembly

Accept: 196  Submit: 1152

Time Limit: 3000 mSec

Problem Description

Automatic Chemical Manufacturing is experimenting with a process called self-assembly. In this process, molecules with natural affinity for each other are mixed together in a solution and allowed to spontaneously assemble themselves into larger structures. But there is one problem: sometimes molecules assemble themselves into a structure of unbounded size, which gums up the machinery. You must write a program to decide whether a given collection of molecules can be assembled into a structure of unbounded size. You should make two simplifying assumptions: 1) the problem is restricted to two dimensions, and 2) each molecule in the collection is represented as a square. The four edges of the square represent the surfaces on which the molecule can connect to other compatible molecules. In each test case, you will be given a set of molecule descriptions. Each type of molecule is described by four two-character connector labels that indicate how its edges can connect to the edges of other molecules. There are two types of connector labels:

• An uppercase letter (A, ..., Z) followed by + or -. Two edges are compatible if their labels have the same letter but different signs. For example, A+ is compatible with A- but is not compatible with A+ or B-.

• Two zero digits 00. An edge with this label is not compatible with any edge (not even with another edge labeled 00).
Assume there is an unlimited supply of molecules of each type, which may be rotated and reected. As the molecules assemble themselves into larger structures, the edges of two molecules may be adjacent to each other only if they are compatible. It is permitted for an edge, regardless of its connector label, to be connected to nothing (no adjacent molecule on that edge). Figure A.1 shows an example of three molecule types and a structure of bounded size that can be assembled from them (other bounded structures are also possible with this set of molecules).

 Input

The input consists of several test cases. A test case consists of two lines. The first contains an integer n (1 ≤ n ≤ 40000) indicating the number of molecule types. The second line contains n eight-character strings, each describing a single type of molecule, separated by single spaces. Each string consists of four two-character connector labels representing the four edges of the molecule in clockwise order.

 Output

For each test case, display the word ‘unbounded’ if the set of molecule types can generate a structure of unbounded size. Otherwise, display the word ‘bounded’.

 Sample Input

3

A+00A+A+

00B+D+A-

B-C+00C+

1

K+K-Q+Q

 Sample Output

bounded

unbounded

题解:这个题关键在于可以翻转,旋转倒在其次,能够翻转让这个题难度降低了很多,以正方形为边,连接可以相互转化的字符串,我一开始考虑的是直接连接一个正方形内的字符串,但是这样操作就要在

形如A-、A+形式的字符串之间连边,相对比较麻烦。看了lrj的代码,发现如果直接将连出边的那个字符串^1,就可以将边的含义转化为通过一个正方形,u可以转化为v,这样一来就不用添加刚才说的边了,这个操作看似简单,但是感觉很机智(orz)。

有一个地方值得注意,尝试对每一个字符串拓扑排序时,不用每一次都把vis清空,因为当你再一次遇到已经vis过的字符串时,后面的就不用操作了,如果有环,早就输出了,如果没环,也不会在这个地方再出现环。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 using namespace std;
 6 
 7 const int kind = 52;
 8 int n;
 9 int gra[kind][kind];
10 
11 void connect(char a1,char a2,char b1,char b2){
12     if(a1=='0' || b1=='0') return;
13     int u = ((a1-'A')<<1)+(a2 == '+' ? 0 : 1);
14     int v = ((b1-'A')<<1)+(b2 == '+' ? 0 : 1);
15     gra[u^1][v] = 1;
16 }
17 
18 int vis[kind];
19 
20 bool dfs(int u){
21     vis[u] = -1;
22     for(int i = 0;i < kind;i++){
23         if(gra[u][i]){
24             if(vis[i] == -1) return true;
25             if(!vis[i] && dfs(i)) return true;
26         }
27     }
28     vis[u] = 1;
29     return false;
30 }
31 
32 int main()
33 {
34     //freopen("input.txt","r",stdin);
35     while(~scanf("%d",&n) && n){
36         char str[50];
37         memset(gra,0,sizeof(gra));
38         for(int k = 0;k < n;k++){
39             scanf("%s",str);
40             for(int i = 0;i < 4;i++){
41                 for(int j = 0;j < 4;j++){
42                     if(i == j) continue;
43                     connect(str[i<<1],str[(i<<1)+1],str[j<<1],str[(j<<1)+1]);
44                 }
45             }
46         }
47         memset(vis,false,sizeof(vis));
48         int i;
49         for(i = 0;i < kind;i++){
50             if(!vis[i] && dfs(i)) break;
51         }
52         if(i == kind) printf("bounded
");
53         else printf("unbounded
");
54     }
55     return 0;
56 }
原文地址:https://www.cnblogs.com/npugen/p/9515381.html