zoukankan      html  css  js  c++  java
  • uva1572

     1 #include<cstdio>
     2 #include<string>
     3 #include<iostream>
     4 #include<cstring>
     5 using namespace std;
     6 
     7 int bian[100][100], vis[100];
     8 void cont (char a, char b, char c, char d)
     9 {
    10     if(b == '0' || d == '0')
    11         return;
    12     int l1 = (a - 'A') * 2 + (b == '+'? 0 : 1);
    13     int l2 = (c - 'A') * 2 + (d == '+'? 0 : 1);
    14     int u = l1 ^ 1;
    15     int v = l2;
    16     bian[u][v] = 1;
    17 }
    18 bool bfs(int i)
    19 {
    20     vis[i] = -1;
    21     for(int j = 0; j < 52; j++)
    22         if(bian[i][j])
    23     {
    24         if(vis[j] < 0)
    25         return true;
    26         else if( !vis[j] && bfs(j))
    27         return true;
    28     }
    29     vis[i] = 1;
    30     return false;
    31 }
    32 int main()
    33 {
    34     int n;
    35     while(scanf("%d", &n) != EOF && n)
    36     {
    37         memset(bian, 0, sizeof(bian));
    38         memset(vis, 0, sizeof(vis));
    39         int flag = 1;
    40         while(n--)
    41         {
    42             string s;
    43             cin >> s;
    44             for(int i = 0; i < 4; i++)
    45                 for(int j = 0; j < 4; j++)
    46                 if(i != j)
    47             {
    48                 cont(s[2*i], s[2*i+1], s[2*j], s[2*j+1]);
    49             }
    50         }
    51         for(int i = 0; i < 52; i++)
    52            if(!vis[i])
    53             if(bfs(i))
    54             {
    55              flag = 0;
    56              break;
    57             }
    58       if(flag)
    59         printf("bounded
    ");
    60        if(!flag)
    61         printf("unbounded
    ");
    62 
    63     }
    64 
    65 }

    这是一道关于拓扑排序的题。。。通过这道题,,更深的理解了判断是否为有向环的方法。。。感觉在构建路线时的思维实在是巧妙。。。。了解了^的用法

  • 相关阅读:
    UVa 11300 Spreading the Wealth(有钱同使)
    hihoCoder 1385 : A Simple Job(简单工作)
    hihoCoder 1383 : The Book List(书目表)
    HDU 5724 Chess(国际象棋)
    Sobytiynyy Proyekt Casino Gym
    Course recommendation Gym
    Poor Folk Gym
    How far away? (HDU
    BAPC 2016 ----Brexit (BFS + vector)
    Simpsons’ Hidden Talents(扩展KMP)
  • 原文地址:https://www.cnblogs.com/jrjxt/p/10432739.html
Copyright © 2011-2022 走看看