• Uva 208


    【题目链接】http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=144

    【随笔侃述】这题感想多过于解题的思路,TL不可避免,很难想象1Y过的是何方神圣,当然不敢否定有经验和敢于尝试的人会在TL几次后马上领悟灯亮后敲出AC的代码,需要预处理的原因是:尽管Case可能给你很多跟1连通的点但最终1跟目的之间能连通的路径去找不到,这得消耗很多的时间做无用功。TL的原因就可能出在此处

    【解题思路】现将所有能连通到目的点的点放入一个数组,来一个排序(题目告诉你按顺序输出而不会是special judge)然后再进行深度查找,遍历输出。

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 #define MAXN 22
     5 int road[MAXN][MAXN], path[MAXN], visit[MAXN], link[MAXN];
     6 int n, num, lastnum, huge;
     7 
     8 int cmp(const void *a, const void *b)
     9 {
    10     return *(int *)a - *(int *)b;
    11 }
    12 
    13 void previous(int cur)
    14 {
    15     int i;
    16     link[lastnum++] = cur;
    17     visit[cur] = 1;
    18     for(i=1; i<=num; ++i)
    19     {
    20         if(road[cur][i] && !visit[i])
    21         previous(i);    
    22     }
    23     return;
    24 }
    25 
    26 void Traverse(int cur, int des, int cnt)
    27 {
    28     int i, j, temp;
    29     if(cur == des)
    30     {
    31         j = 0;
    32         printf("1");
    33         for(i=0; i<cnt; ++i)
    34         {
    35             j = 1;
    36             printf(" %d", path[i]);
    37         }
    38         printf("
    ");
    39         n += j;
    40         return;
    41     }
    42     for(i=0; i<lastnum; ++i)
    43     {
    44         temp = link[i];
    45         if(road[cur][temp] && !visit[temp])
    46         {
    47             path[cnt] = temp;
    48             visit[temp] = 1;
    49             Traverse(temp, des, cnt+1);
    50             visit[temp] = 0; 
    51         }
    52     }    
    53 }
    54 
    55 int main()
    56 {
    57     #ifndef ONLINE_JUDGE
    58     freopen("input.txt", "r", stdin);
    59     #endif
    60     int i, j, des, from, to, t=0;
    61     while(scanf("%d", &des) != EOF)
    62     {
    63         memset(road, 0, sizeof(road));
    64         memset(visit, 0, sizeof(visit));
    65         num = 0;
    66         while(scanf("%d%d", &from, &to) != EOF)
    67         {
    68             if(!from && !to) break;
    69             road[from][to] = road[to][from] = 1;
    70             if(num < from) num = from;
    71             if(num < to) num = to;
    72         }
    73         n = 0;
    74         lastnum = 0;
    75         previous(des);
    76         memset(visit, 0, sizeof(visit));
    77         qsort(link, lastnum, sizeof(int), cmp);
    78         visit[1] = 1;
    79         printf("CASE %d:
    ", ++t);
    80         Traverse(1, des, 0);
    81         printf("There are %d routes from the firestation to streetcorner %d.
    ", n, des);
    82     }
    83     return 0;
    84 }
  • 相关阅读:
    docker 安装 nexus3 初始密码不再是admin123
    eclipse中Tomcat修改项目名称
    WAMP3.1.3自定义根目录
    git学习笔记
    小米和MAC触摸板手势汇总
    IDEA快捷键汇总
    servelet 实现Post接口访问
    LeetCode:Jump Game II
    LeetCode:Trapping Rain Water
    LeetCode: Container With Most Water
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/3187443.html
Copyright © 2020-2023  润新知