UVA 1572 Self-Assembly

以颜色为节点,同一个方块上的颜色连边。。。看是不是有环。。。如果有环就可以无限延长。。。。

Self-Assembly

Problem ID: assemblyTime limit: 5 secondsMemory
limit: 1024 MB

DIFFICULTY

3.7

My Submissions

Automatic Chemical Manufacturing is experimenting with a process called self-assembly. In this process, molecules with natural affinity for each other are mixed together in a solution and allowed to spontaneously
assemble themselves into larger structures. But there is one problem: sometimes molecules assemble themselves into a structure of unbounded size, which gums up the machinery.

You must write a program to decide whether a given collection of molecules can be assembled into a structure of unbounded size. You should make two simplifying assumptions: 1) the problem is restricted to two dimensions,
and 2) each molecule in the collection is represented as a square. The four edges of the square represent the surfaces on which the molecule can connect to other compatible molecules.

In each test case, you will be given a set of molecule descriptions. Each type of molecule is described by four two-character connector labels that indicate how its edges can connect to the
edges of other molecules. There are two types of connector labels:

  • An uppercase letter (A,…,Z)
    followed by + or ?.
    Two edges are compatible if their labels have the same letter but different signs. For example, A+ is
    compatible with A? but
    is not compatible with A+ or B?.
  • Two zero digits 00.
    An edge with this label is not compatible with any edge (not even with another edge labeled 00).

Assume there is an unlimited supply of molecules of each type, which may be rotated and reflected. As the molecules assemble themselves into larger structures, the edges of two molecules may be adjacent to each
other only if they are compatible. It is permitted for an edge, regardless of its connector label, to be connected to nothing (no adjacent molecule on that edge).

Figure 1 shows an example of three molecule types and a structure of bounded size that can be assembled from them (other bounded structures are also possible with this set of molecules).

 

Figure 1: Illustration of Sample Input 1.

Input

The input consists of a single test case. A test case consists of two lines. The first contains an integer n (1≤n≤40000)
indicating the number of molecule types. The second line contains n eight-character
strings, each describing a single type of molecule, separated by single spaces. Each string consists of four two-character connector labels representing the four edges of the molecule in clockwise order.

Output

Display the word unbounded if the set of molecule types can generate a structure of unbounded size. Otherwise, display the word bounded.

Sample Input 1 Sample Output 1
3
A+00A+A+ 00B+D+A- B-C+00C+
bounded
Sample Input 2 Sample Output 2
1
K+K-Q+Q-
unbounded

Download the sample data files

Source: ACM International Collegiate Programming Contest World Finals 2013. License: Restricted, used with permission

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define prt(k) cout<<#k" = "<<k<<endl
const int m = 26;

int f(string a)
{
        if (a=="00") return -1;
        int c = a[0] - 'A';
        return 2 * c + (a[1]=='+');
}
int mp[77];
int du[77];
vector<int> g[77];
void add(int u, int v)
{
        du[v]++;
    //    printf("%d --> %d\n", u, v);
        g[u].push_back(v);
}
bool gao()
{
        queue<int> q;
        for (int i=0;i<m;i++) if(du[i]==0) q.push(i);
        int cnt = 0;
        while (!q.empty()) {
                int u = q.front(); q.pop();
                cnt++;
                for (int v : g[u]) {
                        du[v]--;
                        if (du[v]==0) q.push(v);
                }
        }
        return cnt==m;
}
int main()
{
        int n;
        while (scanf("%d", &n)==1) {
                memset(mp, 0, sizeof mp);
                memset(du, 0, sizeof du);
                bool g[75][75];
                 memset(g, 0, sizeof g);
              //  for (int i=0;i<55;i++) g[i].clear();
                for (int i=0;i<n;i++) {
                        char buf[10];
                        scanf("%s", buf);
                        string s(buf);
                        int t[55];
                        for (int j=0;j<4;j++)
                                t[j] = f(s.substr(j*2, 2));//,prt(t[j]);

                        for (int j=0;j<4;j++)
                        for (int k=0;k<4;k++) if (j - k && t[j]!=-1 && t[k]!=-1) {
                                g[t[j]][t[k]^1] = true;
                        }
                }
                n = 52;
                for(int k = 0; k < n; k++) {
                for(int i = 0; i < n; i++)
                                for(int j = 0; j < n; j++)
                                        g[i][j] |= g[i][k]&&g[k][j];
                }
                bool f = 0;
                for (int i=0;i<n;i++) f|=g[i][i];
                puts(!f ? "bounded" : "unbounded");
        }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-10 14:22:14

UVA 1572 Self-Assembly的相关文章

UVA 1572 Self-Assembly(拓扑排序)

1 // 把一个图的所有结点排序,使得每一条有向边(u,v)对应的u都排在v的前面. 2 // 在图论中,这个问题称为拓扑排序.(toposort) 3 // 不难发现:如果图中存在有向环,则不存在拓扑排序,反之则存在. 4 // 不包含有向环的有向图称为有向无环图(DAG). 5 // 可以借助DFS完成拓扑排序:在访问完一个结点之后把它加到当前拓扑序的首部. 6 7 int c[maxn]; 8 int topo[maxn],t; 9 bool dfs(int u) 10 { 11 c[u]

uva 1572 self-assembly ——yhx

1 #include<cstdio> 2 #include<queue> 3 #include<cstring> 4 using namespace std; 5 int map[60][60],cnt[60]; 6 queue<int> q; 7 char s[10]; 8 int main() 9 { 10 int i,j,k,m,n,p,t,x,y,z,w,temp[5]; 11 while (~scanf("%d",&n)

UVa 1572 (拓扑排序) Self-Assembly

题意: 有n种正放形,每种正方形的数量可视为无限多.已知边与边之间的结合规则,而且正方形可以任意旋转和反转,问这n中正方形是否可以拼成无限大的图案. 分析: 首先因为可以旋转和反转,所以可以保证在拼接的过程中正方形不会自交. 把边的标号看成点,将正方形的边界A+变成B+可以看做是一条边.比如说,一个正方形中有A-和B+两条边,则A-与其他正方形中A+结合后,结合前边界为A-,结合后变为B+. 这样就得到图中的一条有向边A+ → B+ 如果能在图中找到一个环,则可以无限循环拼接正方形. 1 #in

Uva 1572 自组合

贴个源码// UVa1572 Self-Assembly // Rujia Liu #include<cstdio> #include<cstring> #include<vector> using namespace std; int ID(char a1, char a2) { return (a1-'A')*2 + (a2 == '+' ? 0 : 1); } int G[52][52]; // connect(A+, B-) means a border lab

uvalive 6393(uva 1572) Self-Assembly 拓扑排序

题意: 给出一些正方形,这些正方形的每一条边都有一个标号,这些标号有两种形式:1.一个大写字母+一个加减号(如:A+, B-, A-......), 2.两个0(如:00):这些正方形可以随意翻转和旋转,当两个正方形通过旋转或翻转,使得他们的公共边为相同大写字母并且符号相反时,他们就可以彼此结合拼在一起,现在给出n中正方形,每种正方形有无限多种,问这些正方形能否拼成一个无限大的结构. 题解: 容易想到,要使这些正方形形成无限大地结构,那么这些正方形通过拼接后一定能循环(即通过不断地拼接出现了和以

UVa 1572 Self-Assembly (构造+拓扑排序。。。。。)

题意:给定n个带标号的正方形,标号要么是一个大写字母加一个+或-,要么是00, 当且仅当大写字母相同并且符号相反时可以连接,问你给定的能不能拼成一个无限大的的东西. 析:说实话,真心没有看出来是拓扑排序,后来知道是,可是还是不会写. 既然要拼成无限大,那么只要拼的时候拼出一个环来,又由于每个是无限多的,那么可以一直重复, 就能拼起来无限大的东西,把每个正方形看成一条边,那么不就是一个拓扑排序,看看能不能找到一个环么, 如果能找到,那么就可以,找不到,就不可以.注意的是正方形可以从四面都拼,所以要

Linux C中内联汇编的语法格式及使用方法(Inline Assembly in Linux C)

在阅读Linux内核源码或对代码做性能优化时,经常会有在C语言中嵌入一段汇编代码的需求,这种嵌入汇编在CS术语上叫做inline assembly.本文的笔记试图说明Inline Assembly的基本语法规则和用法(建议英文阅读能力较强的同学直接阅读本文参考资料中推荐的技术文章 ^_^). 注意:由于gcc采用AT&T风格的汇编语法(与Intel Syntax相对应,二者的区别参见这里),因此,本文涉及到的汇编代码均以AT&T Syntax为准. 1. 基本语法规则 内联汇编(或称嵌入汇

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te