uva 1572 Self-Assembly

题目:Self-Assembly


题意:输入几种正方形的面,每一种正方形的4个边上都有标号,标号为 大写字母+符号(+/-) 或00。有同一字母标号符号不同的的两条边可以靠在一起,00不能和其它边相连。问能否用这几种正方形拼成一个无限的结构。


思路:如书上所述,把标号看成点,正方形看成边。如果一个标号在一个正方形中,那么它和正方形其它边上的标号反过来(如A+反过来得A-)连一条边。如果图中有环,说明无限。


注:本题按书上程序思路写的。


代码:

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<vector>
using namespace std;

int n;
int a[100][100]= {0};
int b[100]={0};

int cnt(string x) {
	if(x[0]=='0') return -1;
	return 2*(x[0]-'A')+(x[1]=='+'?0:1);
}

void make(int x,int y){
	if(x==-1||y==-1||a[x^1][y]) return ;
	a[x^1][y]=true;
	b[x^1]++;
}

void init() {
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	for(int i=1; i<=n; i++) {
		string x;
		cin>>x;
		int y[5]= {0};
		y[1]=cnt(x.substr(0,2));
		y[2]=cnt(x.substr(2,2));
		y[3]=cnt(x.substr(4,2));
		y[4]=cnt(x.substr(6,2));
		for(int j=1;j<=4;j++){
			for(int k=1;k<=4;k++){
				if(j!=k){
					make(y[j],y[k]);
				}
			}
		}
	}
}

bool top_sort() {
	int still=52,last=52;
	bool use[100]= {0};
	while(still) {
		vector<int> vec;
		for(int i=0; i<52; i++) {
			if(!b[i]&&!use[i]) {
				use[i]=true,still--;
				for(int j=0; j<52; j++) {
					if(a[j][i]) a[j][i]=false,vec.push_back(j);
				}
			}
		}
		for(int j=0; j<vec.size(); j++) {
			b[vec[j]]--;
		}
		if(still==last) return false;
		last=still;
	}
	return true;
}

int main() {
	while(scanf("%d",&n)==1) {
		init();
		if(top_sort()) printf("bounded\n");
		else printf("unbounded\n");
	}
	return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值