zoukankan      html  css  js  c++  java
  • 【UVa 208】Firetruck

    The Center City fire department collaborates with the transportation department to maintain maps
    of the city which reflects the current status of the city streets. On any given day, several streets are
    closed for repairs or construction. Firefighters need to be able to select routes from the firestations to
    fires that do not use closed streets.
    Central City is divided into non-overlapping fire districts, each containing a single firestation. When
    a fire is reported, a central dispatcher alerts the firestation of the district where the fire is located and
    gives a list of possible routes from the firestation to the fire. You must write a program that the central
    dispatcher can use to generate routes from the district firestations to the fires.
    Input
    The city has a separate map for each fire district. Streetcorners of each map are identified by positive
    integers less than 21, with the firestation always on corner #1. The input file contains several test cases
    representing different fires in different districts.
    • The first line of a test case consists of a single integer which is the number of the streetcorner
    closest to the fire.
    • The next several lines consist of pairs of positive integers separated by blanks which are the
    adjacent streetcorners of open streets. (For example, if the pair 4 7 is on a line in the file, then
    the street between streetcorners 4 and 7 is open. There are no other streetcorners between 4 and
    7 on that section of the street.)
    • The final line of each test case consists of a pair of 0’s.
    Output
    For each test case, your output must identify the case by number (‘CASE 1:’, ‘CASE 2:’, etc). It must
    list each route on a separate line, with the streetcorners written in the order in which they appear on
    the route. And it must give the total number routes from firestation to the fire. Include only routes
    which do not pass through any streetcorner more than once. (For obvious reasons, the fire
    department doesn’t want its trucks driving around in circles.)
    Output from separate cases must appear on separate lines.
    Sample Input
    6
    1 2
    1 3
    3 4
    3 5
    4 6
    5 6
    2 3
    2 4
    0 0
    4
    2 3
    3 4
    5 1
    1 6
    7 8
    8 9
    2 5
    5 7
    3 1
    1 8
    4 6
    6 9
    0 0
    Sample Output
    CASE 1:
    1 2 3 4 6
    1 2 3 5 6
    1 2 4 3 5 6
    1 2 4 6
    1 3 2 4 6
    1 3 4 6
    1 3 5 6
    There are 7 routes from the firestation to streetcorner 6.
    CASE 2:
    1 3 2 5 7 8 9 6 4
    1 3 4
    1 5 2 3 4
    1 5 7 8 9 6 4
    1 6 4
    1 6 9 8 7 5 2 3 4
    1 8 7 5 2 3 4
    1 8 9 6 4
    There are 8 routes from the firestation to streetcorner 4.

    深搜即可,无需多讲。

    要注意的是,要先判断起点是否和终点在一起(即在一个连通分量中)。否则会超时。这里用了传递闭包。网上说可以用并查集、TARJAN什么的。。。。

    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    int k, path[25], len, tot;
    bool v[25], g[25][25];
    
    void solve(int x)
    {
        v[x] = false;
        path[len++] = x;
        if (x == k)
        {
            tot++;
            for (int i = 0; i < len; ++i)
            {
                if (i) printf(" ");
                printf("%d", path[i]);
            }
            printf("
    ");
            v[x] = true;
            len--;
            return;
        }
        for (int i = 1; i <= 21; ++i)
            if (g[x][i] && v[i])
                solve(i);
        v[x] = true;
        len--;
    }
    
    bool it_can_work()
    {
        bool dis[25][25];
        memcpy(dis, g, sizeof(g));
        for (int m = 1; m <= 21; ++m)
            for (int i = 1; i <= 21; ++i)
                for (int j = 1; j <= 21; ++j)
                    if (i != m && i != j && j != m)
                        dis[i][j] = dis[i][j] || (dis[i][m] && dis[m][j]);
        return dis[1][k];
    }
    
    int main()
    {
        int kase = 0;
        while (scanf("%d", &k) == 1)
        {
            printf("CASE %d:
    ", ++kase);
            int x, y;
            memset(v, true, sizeof(v));
            memset(g, false, sizeof(g));
            while (scanf("%d%d", &x, &y) == 2 && x && y)
                g[x][y] = g[y][x] = true;
            len = tot = 0;
            if (it_can_work())solve(1);
            printf("There are %d routes from the firestation to streetcorner %d.
    ", tot, k);
        }
        return 0;
    }
  • 相关阅读:
    Leetcode练习(Python):树类:第117题:填充每个节点的下一个右侧节点指针 II:给定一个二叉树 填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。 初始状态下,所有 next 指针都被设置为 NULL。
    高可用OpenStack(Queen版)集群-5.Glance集群
    高可用OpenStack(Queen版)集群-4.keystone集群
    高可用OpenStack(Queen版)集群-3.高可用配置(pacemaker&haproxy)
    高可用OpenStack(Queen版)集群-2.基础服务
    高可用OpenStack(Queen版)集群-1. 集群环境
    GlusterFS分布式存储集群-2. 使用
    GlusterFS分布式存储集群-1. 部署
    高可用Kubernetes集群-15. 部署Kubernetes集群统一日志管理
    高可用Kubernetes集群-14. 部署Kubernetes集群性能监控平台
  • 原文地址:https://www.cnblogs.com/albert7xie/p/4855957.html
Copyright © 2011-2022 走看看