主要利用了拓扑排序的算法,connect函数的作用是如果正方块的两条边都不为00,且设一条边为Char1+,一条边为Char2-,则Char1+可通过这个正方块和Char2+相连,即有向图上的两点Char1+和Char2+之间有边,然后拓扑排序,如果图内有环,就说明可以组成无限大的结构。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
int G[100][100];
int vis[100];
char str[30];
int getID(char a1, char a2)
{
return (a1 - 'A')*2 + (a2 == '+' ? 1:0);
}
void connect(char a1, char a2, char b1, char b2)
{
if(a2 == '0' || b2 == '0') return ;
int u = getID(a1, a2)^1;
int v = getID(b1, b2);
G[u][v] = 1;
}
bool dfs(int u)
{
vis[u] = -1;
for(int i = 0; i < 52; i++)
{
if(G[u][i] && vis[i] < 0) return true;
if(G[u][i] && !vis[i] && dfs(i)) return true;
}
vis[u] = 1;
return false;
}
bool solve()
{
for(int i = 0; i < 52; i++)
if(!vis[i])
if(dfs(i)) return true;
return false;
}
int main()
{
//freopen("ztest.txt","r",stdin);
//freopen("zans.txt","w",stdout);
int n;
while(scanf("%d", &n) == 1 && n)
{
memset(vis, 0, sizeof(vis));
memset(G, 0, sizeof(G));
for(int k = 1; k <= n; k++)
{
scanf("%s", str);
//printf("%s\n",str);
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
if(i != j)
connect(str[i*2], str[i*2+1], str[j*2], str[j*2+1]);
}
if(solve())
printf("unbounded\n");
else
printf("bounded\n");
}
return 0;
}