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. 牛刀小试MySQL--innodb_flush_log_at_trx_commit小结

    参数名:innodb_flush_log_at_trx_commit 参数值: 0 事务提交的时候,不会去刷日志缓存区,也不会立马写入至日志文件中.这种设置最危险.如果数据库挂了且运气不好,数据库的最 ...

  2. 数据分析之numpy模块

    numpy(numerical python)是python语言的一个扩展程序库,支持大量的维度数组和矩阵运算,此外也针对数组提供大量的数学函数库. 一.创建数组 1 使用array()创建 impo ...

  3. angularjs学习第四天笔记(第一篇:简单的表单验证)

    您好,我是一名后端开发工程师,由于工作需要,现在系统的从0开始学习前端js框架之angular,每天把学习的一些心得分享出来,如果有什么说的不对的地方,请多多指正,多多包涵我这个前端菜鸟,欢迎大家的点 ...

  4. Net 如何计算一段代码的效率

    在.Net 4.0以后的版本,提供了一个类,该类在 System.Diagnostics命名空间下,使用该类就可以计算出执行结果相同的两端代码的效率,在代码优化上是很实用的. 泛型效率是高是低呢??我 ...

  5. Java框架之Struts2(五)

    本文主要介绍Struts2 文件上传.Struts2 多文件上传.文件下载.上传文件的过滤.输入校验.输入校验的流程. 一.Struts2 文件上传 步骤: 1) 页面 <form action ...

  6. Java基础——Oracle(六)

    一.数据字典和动态性能视图 数据字典: oracle中的重要组成部分,提供了数据库的一些系统信息,记录了数据库的系统信息,它是只读表和视图的集合,数据字典的所有者为 sys 用户.用户只能在数据字典上 ...

  7. C Looooops(poj2115+扩展欧几里德)

    C Looooops Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Pr ...

  8. 【20】策略者模式(Strategy Pattern)

    一.引言 本文要介绍的策略模式也就是对策略进行抽象,策略的意思就是方法,所以也就是对方法的抽象,下面具体分享下我对策略模式的理解. 二.策略者模式介绍 2.1 策略模式的定义 在现实生活中,策略模式的 ...

  9. SSM(Spring+SpringMvc+Mybatis)整合笔记

    1.使用开发工具 jdk1.8 eclipse Tomcat7.0 MySql 2.创建数据库和表,由于重点是整合,所以数据库就随意加几条数据. 3.创建动态Web项目(推荐使用Maven可以用配置来 ...

  10. 读《阿里Java开发手册》总结(1)

    一·命名约定 类名使用大驼峰式命名(领域模式相关命名除外:如DAO\VO\DO等). 常量必须全部大写,单词中间用“_”隔开(如MAX_COUNT). 抽象类命名使用Abstract或Base开头.异 ...