当前位置: 代码迷 >> 综合 >> uva 1572 Self-Assembly
  详细解决方案

uva 1572 Self-Assembly

热度:63   发布时间:2023-12-06 08:46:03.0

题目: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;
}


  相关解决方案