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. PowerDesigner反向生成物理数据模型

    什么是PowerDesigner Power Designer 是Sybase公司的CASE工具集,使用它可以方便地对管理信息系统进行分析设计,它几乎包括了数据库模型设计的全过程.利用Power De ...

  2. MVC中 jquery.validate取消忽略对hidden的验证

    <script type="text/javascript"> $.validator.setDefaults({ //取消 忽略对hidden的验证 ignore: ...

  3. Java学习笔记之——常用快捷键(eclipse)

    * Ctrl+C 复制 * Ctrl+V 粘贴 * Ctrl+A 全选 * Ctrl+S 保存 * Ctrl+Z 撤销 * Ctrl+Y 还原 * Ctrl+X 剪切 * Ctrl+F   查找 * ...

  4. C++ enum的使用

    enum day {Sun,Mon,Tue,Wed,Thu,Fri,Sat};  默认情况下,枚举符的值从0开始,其后值总是前面一个+1.  即Sun=0,Mon=1,Tue=2,Wed=3,Thu= ...

  5. 请输入经过encode编码的URL

    美团门店映射: <?php $url = "http://manage.test.kdb.kudianbao.com/Branch/mt_mdysh1d"; $res = u ...

  6. React Render Props 模式

    概述 Render Props模式是一种非常灵活复用性非常高的模式,它可以把特定行为或功能封装成一个组件,提供给其他组件使用让其他组件拥有这样的能力,接下来我们一步一步来看React组件中如何实现这样 ...

  7. Android手势密码--设置和校验

    private void setGesturePassword() { toggleMore.setOnCheckedChangeListener(new CompoundButton.OnCheck ...

  8. Django 知识总结(一)

    Django已经学过的知识点: 1. Urls.py 路由系统: 正则 分组匹配 --> 位置参数 分组命名匹配 --> 关键字参数 分级路由 include 给路由起别名 name=&q ...

  9. 08-OpenLDAP主机控制策略

    OpenLDAP主机控制策略 阅读视图 参考 环境准备 openldap服务端配置 openldap客户端配置 客户端测试登录 故障处理 1. 参考 本文基本转载博客openldap主机访问控制(基于 ...

  10. 洗礼灵魂,修炼python(27)--异常处理(1)—>了解异常

    python学到这,其实你应该是在入门到进阶的中间阶段了,但是还没有到进阶的阶段的,这是肯定的,因为进阶得可以从实际问题中解决问题的,比如写一个自动化的爬虫程序啊,对一件事物作大数据归纳分析,开发一个 ...