思路:
用奇数存放A+,B+,C+,D+......Z+
用偶数存放A-,B-,C-,D-........Z-;
所以只需要数组52存放即可!
给有向图做一次拓扑排序,
用G[u][v]表示u 与 v 相连。
例如: A+ 与B+相连接,那么直接让A- 与B +对应就可以了!(这里想了很久)
然后就是c数组表示访问状态!
-1表示正在访问,0未访问,1表示访问完成!
发现环后return true即可!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 55;
int G[maxn][maxn];
int c[maxn];
bool dfs(int u){
c[u] = -1;
for (int i = 1; i < maxn; ++i){
if (G[u][i]){
if (c[i] < 0)return true;
if (!c[i] && dfs(i))return true;
}
}
c[u] = 1;
return false;
}
bool solve(){
memset(c,0,sizeof(c));
for (int i = 1; i < maxn; ++i)if (!c[i])
if (dfs(i))return true;
return false;
}
bool read(){
memset(G,0,sizeof(G));
int T;
if (scanf("%d",&T) != 1)return false;
while(T--){
char s[10];
scanf("%s",s+1);
int id[5]={};
for (int i = 1; i <= 8; i += 2){
if (s[i] == '0')id[(i+1)/2] = 0;
else {
int key = (s[i] - 'A' + 1) * 2 - 1;
int flag = s[i+1] == '+' ? 0 : 1;
id[(i+1)/2] = key + flag;
}
}
for (int i = 1; i <= 4; ++i){
if (!id[i])continue;
for (int j = 0; j <= 4; ++j){
if (!id[j] || i == j)continue;
int u = id[i] % 2 == 1 ? id[i] + 1 : id[i] - 1;
int v = id[j];
G[u][v] = 1;
}
}
}
return true;
}
int main()
{
while(read())
printf("%s\n",solve() == true ? "unbounded" : "bounded");
return 0;
}