Problem UVA1572-Self-Assembly

Accept: 196  Submit: 1152

Time Limit: 3000 mSec

Problem Description

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 reected. 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 A.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).

 Input

The input consists of several test cases. 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

For each test case, display the word ‘unbounded’ if the set of molecule types can generate a structure of unbounded size. Otherwise, display the word ‘bounded’.

 Sample Input

3

A+00A+A+

00B+D+A-

B-C+00C+

1

K+K-Q+Q

 Sample Output

bounded

unbounded

题解:这个题关键在于可以翻转,旋转倒在其次,能够翻转让这个题难度降低了很多,以正方形为边,连接可以相互转化的字符串,我一开始考虑的是直接连接一个正方形内的字符串,但是这样操作就要在

形如A-、A+形式的字符串之间连边,相对比较麻烦。看了lrj的代码,发现如果直接将连出边的那个字符串^1,就可以将边的含义转化为通过一个正方形,u可以转化为v,这样一来就不用添加刚才说的边了,这个操作看似简单,但是感觉很机智(orz)。

有一个地方值得注意,尝试对每一个字符串拓扑排序时,不用每一次都把vis清空,因为当你再一次遇到已经vis过的字符串时,后面的就不用操作了,如果有环,早就输出了,如果没环,也不会在这个地方再出现环。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std; const int kind = ;
int n;
int gra[kind][kind]; void connect(char a1,char a2,char b1,char b2){
if(a1=='' || b1=='') return;
int u = ((a1-'A')<<)+(a2 == '+' ? : );
int v = ((b1-'A')<<)+(b2 == '+' ? : );
gra[u^][v] = ;
} int vis[kind]; bool dfs(int u){
vis[u] = -;
for(int i = ;i < kind;i++){
if(gra[u][i]){
if(vis[i] == -) return true;
if(!vis[i] && dfs(i)) return true;
}
}
vis[u] = ;
return false;
} int main()
{
//freopen("input.txt","r",stdin);
while(~scanf("%d",&n) && n){
char str[];
memset(gra,,sizeof(gra));
for(int k = ;k < n;k++){
scanf("%s",str);
for(int i = ;i < ;i++){
for(int j = ;j < ;j++){
if(i == j) continue;
connect(str[i<<],str[(i<<)+],str[j<<],str[(j<<)+]);
}
}
}
memset(vis,false,sizeof(vis));
int i;
for(i = ;i < kind;i++){
if(!vis[i] && dfs(i)) break;
}
if(i == kind) printf("bounded\n");
else printf("unbounded\n");
}
return ;
}

Problem UVA1572-Self-Assembly(拓扑排序)的更多相关文章

  1. UVA-1572 Self-Assembly(拓扑排序判断有向环)

    题目: 给出几种正方形,每种正方形有无穷多个.在连接的时候正方形可以旋转.翻转. 正方形的每条边上都有一个大写英文字母加‘+’或‘-’.00,当字母相同符号不同时,这两条边可以相连接,00不能和任何边 ...

  2. Problem 1014 xxx游戏 暴力+拓扑排序

    题目链接: 题目 Problem 1014 xxx游戏 Time Limit: 1000 mSec Memory Limit : 32768 KB 问题描述 小M最近很喜欢玩XXX游戏.这个游戏很简单 ...

  3. UVA-1572 Self-Assembly (图+拓扑排序)

    题目大意:每条边上都有标号的正方形,两个正方形能通过相匹配的边连接起来,每种正方形都有无限多个.问能否无限延展下去. 题目分析:将边视为点,正方形视为边,建立无向图,利用拓扑排序判断是图否为DAG. ...

  4. UVa 1572 Self-Assembly (拓扑排序)

    题目链接: https://cn.vjudge.net/problem/UVA-1572 Automatic Chemical Manufacturing is experimenting with ...

  5. UVA 1572 Self-Assembly(拓扑排序)

    1 // 把一个图的所有结点排序,使得每一条有向边(u,v)对应的u都排在v的前面. 2 // 在图论中,这个问题称为拓扑排序.(toposort) 3 // 不难发现:如果图中存在有向环,则不存在拓 ...

  6. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  7. 图——拓扑排序(uva10305)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  8. poj 3687(拓扑排序)

    http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...

  9. *HDU1285 拓扑排序

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

随机推荐

  1. /proc文件系统 - 汇总

    0. /proc目录简介 Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制. proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间 ...

  2. css布局------左右宽度固定,中间宽度自适应容器

    HTML /*适用方法1,方法2*/<body> <div class="container"> <div class="left" ...

  3. C#实现加简单的Http请求

    通过.Net中的两个类 HttpWebRequest 类, HttpWebResponse 类来实现Http的请求,响应处理. 第一个小测试是请求百度首页( http://www.baidu.com ...

  4. C#基础知识回顾-- 反射(4)

    从程序集获得类型 先说点题外话,现在技术真的发展太快了.我这边还很多东西半生不熟 呢,那边又出现了好多有趣的新东西让你眼红不已.学还是不学这还真是 个问题.Node.js .bootstrap,我最近 ...

  5. Linux路由表信息-route命令

    使用命令 :route route 命令    显示和设置Linux路由表 -A:设置地址类型: -C:打印将Linux核心的路由缓存: -v:详细信息模式: -n:不执行DNS反向查找,直接显示数字 ...

  6. C# 单例模式和窗体的单例打开方法

    第一种最简单,但没有考虑线程安全,在多线程时可能会出问题,不过俺从没看过出错的现象,表鄙视我…… public class Singleton{    private static Singleton ...

  7. minitab 转换语言

    在使用minitab的时候,我们会以中文版熟悉界面,但是我们在实际操作的时候就要使用英文版的功能. 那么如何改变其语言环境呢. 很简单: 工具->选项->语言

  8. python全局解释器锁(GIL)

    文章作者:卢钧轶(cenalulu) 本文原文地址:http://cenalulu.github.io/python/gil-in-python/ ,对文章做了适当的修改,加入了一些自己的理解. CP ...

  9. js事件冒泡和事件捕获详解

    Javascript与HTML之间的交互是通过事件实现. 一.事件流 事件,是文档或浏览器窗口中发生的一些特定的交互瞬间.事件流,描述的是页面中接受事件的顺序.IE9,chrome,Firefox,O ...

  10. 1788:Pell数列

    1788:Pell数列 查看 提交 统计 提问 总时间限制:  3000ms 内存限制:  65536kB 描述 Pell数列a1, a2, a3, ...的定义是这样的,a1 = 1, a2 = 2 ...