zoukankan      html  css  js  c++  java
  • UVa 1572 Self-Assembly (拓扑排序)

    题目链接:

    https://cn.vjudge.net/problem/UVA-1572

    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)。

     1 /*
     2 题意描述:
     3 给出n张正方形的牌,牌上有从东开始逆时针四条边的符号,当两张牌的边上字母相同而符号不同的时候表示两张牌可以连接,问能否通过这
     4 几张牌构成一各无限大的结构。
     5 解题思路:
     6 题意比较难以理解,理一理思路,无限大的结构,就是类似于某种分子结构中间是可以无限添加的,那么把标号看成是节点,一共有26*2个,
     7 将各个牌看做是某种关系,构成一个有向图,如果该有向图中存在一个有向环,就说明可以构成一个无限大的结构。那么问题就转化成了,
     8 判断一个有向图中是否存在有向环的问题,做一次拓扑排序,如果存在拓扑序列,则不存在有向环,如果不存在拓扑序列,则存在有向环。 
     9 拓扑排序的队列解法的思路
    10 根据有向图计算每个节点的出度和入度,将入度为0的节点依次加入队列,然后进入循环一次出队一个,并将和它有关系的节点B的入度减一,
    11 如果节点B的入度恰好减为0,就将节点B也加入队列,直到队列为空。如果出队的节点数和总数相等表示存在拓扑序列,也即不存在环。但是
    12 由于本题中边的关系特殊,没有办法根据顶点的入度为0,确定入队,因为一张牌上有四个符号,统计入度和出度的时候必须组合一下得出关
    13 系,增加入度,故采用拓扑排序的递归解法。
    14 拓扑排序的递归解法
    15 借助一个标记数组c,c[u]=0表示从来没有访问过u,c[u]=1表示已经访问过,并且已经递归访问过它的所有子孙,c[u]=-1表示正在访问,也
    16 即正在访问它的子孙,而dfs(u)正在栈帧中,尚未返回。 
    17 */ 
    18 #include<bits/stdc++.h>
    19 using namespace std;
    20 
    21 const int maxn=52+10;
    22 bool mp[maxn][maxn];
    23 int c[maxn];
    24 int cycle();
    25 int dfs(int u);
    26 
    27 int id(char a1,char b1){
    28     return (a1-'A')*2+ b1=='+'? 0 : 1;
    29 }
    30 void connect(char a1,char b1,char a2,char b2){
    31     if(a1 == '0' || b1 == '0')
    32         return;
    33     int u=id(a1,b1)^1,v=id(a2,b2);
    34     mp[u][v]=1;
    35 }
    36 
    37 int main()
    38 {
    39     int n;
    40     char list[maxn];
    41     //freopen("E:\testin.txt","r",stdin);
    42     while(scanf("%d",&n) != EOF){
    43         memset(mp,0,sizeof(bool)*maxn*maxn);
    44         for(int i=0;i<n;i++){
    45             scanf("%s",list);
    46             
    47             for(int i=0;i<4;i++){
    48                 for(int j=0;j<4;j++){
    49                     if(i != j)    connect(list[i*2],list[i*2+1],list[j*2],list[j*2+1]);
    50                 }
    51             }
    52         }
    53         
    54         if(cycle())
    55             printf("bounded
    ");
    56         else
    57             printf("unbounded
    ");
    58     }
    59     return 0;
    60 } 
    61 
    62 int cycle(){
    63     memset(c,0,sizeof(int)*maxn);
    64     for(int u=0;u<52;u++){
    65         if(c[u] == 0 && dfs(u)){    
    66             return 1;
    67         }
    68     }
    69     return 0;
    70 } 
    71 
    72 int dfs(int u){
    73     c[u]=-1;
    74     for(int v=0;v<52;v++){
    75         if(mp[u][v]){
    76             if(c[v] == -1)
    77                 return 1;//存在有向环
    78             else
    79                 if(c[v] == 0 && dfs(v))
    80                     return 1; 
    81         }
    82     }
    83     c[u]=1;
    84     return 0;
    85 }
     
     
  • 相关阅读:
    js模板引擎-art-template常用总结
    安装 PLSQL笔记
    [转载]软件测试之Web测试经典总结
    网络相关概念笔记
    HTTP请求过程
    Postman newman
    Postman interceptor
    SoupUI学习资料
    Postman 基本操作学习
    Postman 安装 & 资料
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/9337913.html
Copyright © 2011-2022 走看看