Test instructions
Give some squares, each side of these squares has a label that has two forms: 1. An uppercase letter + a plus minus sign (e.g. A +, B-, a-......), 2. Two 0 (e.g. 00); These squares can be flipped and rotated at will, when two squares are rotated or flipped, So that their public side is the same capital letter and the symbol opposite, they can join each other together, now give the N square, each square has an infinite variety, ask whether these squares can be put into an infinite structure.
Exercises
It is easy to think that to make these squares into an infinite structure, then these squares through the splicing must be able to cycle (that is, through the continuous splicing appeared and the same square), then you can judge the square all possible splicing way to the edge, and then determine whether there is a forward ring, Can be judged by topological ordering.
Code:
#include <queue> #include <vector> #include <cstdio> #include <string> #include <sstream> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int maxn = 10;i NT G[MAXN][MAXN], Vis[maxn];int ID (char A, char b) {return (A-' a ') * * + (b = = ' + '? 0:1);} void Conect (char A1, Char A2, Char B1, char B2) {if (a1 = = ' 0 ' | | b1 = ' 0 ') {return; } int u = ID (a1, A2) ^1, V = ID (b1, B2); G[U][V] = 1;} bool Dfs (int u) {Vis[u] =-1; for (int v = 0; v < v++) if (G[u][v]) {if (vis[v] = = 1) return true; if (!vis[v] && dfs (v)) return true; } Vis[u] = 1; return false;} BOOL Find_cycle () {memset (Vis, 0, sizeof (VIS)); for (int i = 0; i < i++) if (!vis[i]) {if (Dfs (i)) return true; } return false;} int main () {//Freopen ("/users/apple/desktop/in.txt", "R", stdin); int n; while (scanf ("%d", &n) = = 1 && N) { memset (g, 0, sizeof (g)); while (n--) {char s[10]; scanf ("%s", s); for (int i = 0, i < 4; i++) {for (int j = 0; J < 4; j + +) if (i! = j) { Conect (S[i*2], s[i*2+1], s[j*2], s[j*2+1]); }}} if (Find_cycle ()) printf ("unbounded\n"); else printf ("bounded\n"); } return 0;}
Uvalive 6393 (UVA 1572) self-assembly topological ordering