Post

UVa1572 - Self-Assembly

UVa1572 - Self-Assembly

题设

Automatic Chemical Manufacturing is experimenting with a process called self-assembly. In this pro- cess, molecules with natural affinity for each other are mixed together in a solution and allowed to spon- taneously 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).

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

1
2
3
4
  3
  A+00A+A+ 00B+D+A- B-C+00C+
  1
  K+K-Q+Q-

Sample Output

1
2
  bounded
  unbounded

题解

这道题的目标就是判断通过这几个方块的组合,能否形成一个无限延伸的化合物(有点像化学中的有机合成)。无限延伸并不好直接判断,因为即使化合物非常庞大,但也很有可能不会一直延伸下去。而把这一问题转化为判断图中是否有环便非常好解决,以下是抽象过程:对于每一个方块,我们将其看成一些边的集合,这些边由方块的一条侧边到另一条侧边构成。例如上面输入样例中K+到Q+就是一条边。如果这张图存在环,那么便可以让这个环上的方块连接无穷多次,即无限延伸。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstring>
#include <iostream>
using namespace std;
const int N = 52;

int g[64][64];

int n;

/* floyd */
bool check() {
  int flag = 0;
  for (int k = 0; k < N; k++)
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++) g[i][j] |= g[i][k] & g[k][j];

  for (int i = 0; i < N; i++) flag |= g[i][i];
  return flag;
}

int main() {
  char s[50];
  while (scanf("%d", &n) == 1) {
    memset(g, 0, sizeof g);
    for (int i = 0; i < n; i++) {
      scanf("%s", s);
      for (int j = 0; j < 4; j++) {
        for (int k = 0; k < 4; k++) {
          if (j == k || s[2 * j] == '0' || s[2 * k] == '0') continue;
          int x = s[2 * j] - 'A' + (s[2 * j + 1] == '+' ? 0 : 26);
          int y = s[2 * k] - 'A' + (s[2 * k + 1] == '+' ? 26 : 0);
          g[x][y] = 1;
        }
      }
    }
    puts(check() ? "unbounded" : "bounded");
  }
  return 0;
}
This post is licensed under CC BY 4.0 by the author.