Uva - 1572 - Self-Assembly


把A+~Z+,A-~Z-看成52个点,正方形看成边,就得到一个有向图,当存在有向图的时候有解。和10129有点像,做拓扑排序即可。

吐槽:刚开始把循环输入用cin,读取字符串用了scanf,直接WA了,要用还是用成一致的都cin,或者都scanf,这样就没问题了。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset> 
#include <cassert> 

using namespace std;

const int maxn = 52;

int G[maxn][maxn];

// 把52种字符A+~Z+,A-~Z-,转化成数字
int ID(char a1, char a2)
{
	return (a1 - 'A') * 2 + ((a2 == '+') ? 0 : 1);
}

void connect(char a1, char a2, char b1, char b2)
{
	if (a1 == '0' || b1 == '0') { // 有00边不能连接
		return;
	}
	int u = ID(a1, a2) ^ 1;
	int v = ID(b1, b2);
	G[u][v] = 1;
}

int c[maxn]; // 表示访问状态

// 判断是否从u出发存在有向环
bool dfs(int u)
{
	c[u] = -1; // 表示正在访问
	for (int v = 0; v < maxn; v++) {
		if (G[u][v]) {
			if (c[v] < 0) { //走到起点了
				return true;
			}
			else if (!c[v] && dfs(v)) {
				return true;
			}
		}
	}
	c[u] = 1;
	return false;
}

// 是否存在有向环
bool findCycle()
{
	memset(c, 0, sizeof(c));
	for (int i = 0; i < maxn; i++) {
		if (!c[i]) {
			if (dfs(i)) {
				return true;
			}
		}
	}
	return false;
}

int main()
{
	ios::sync_with_stdio(false);
	int n;
	//while (scanf("%d", &n) == 1 && n) {
	while (cin >> n && n) {
		memset(G, 0, sizeof(G));
		while (n--) {
			char s[10];
			//scanf("%s", s);
			cin >> s;
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < 4; j++) {
					if (i != j) {
						connect(s[i * 2], s[i * 2 + 1], s[j * 2], s[j * 2 + 1]);
					}
				}
			}
		}
		if (findCycle()) {
			printf("unbounded\n");
		}
		else {
			printf("bounded\n");
		}
	}

	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值