Uva 208 - Firetruck
【随笔侃述】这题感想多过于解题的思路,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("\n"); 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:\n", ++t); 80 Traverse(1, des, 0); 81 printf("There are %d routes from the firestation to streetcorner %d.\n", n, des); 82 } 83 return 0; 84 }
![物役记](https://s2.ax1x.com/2019/05/26/VE5YD0.jpg)
更多内容请关注个人微信公众号 物役记 (微信号:materialchains)
作者:雪影蓝枫
本文版权归作者和博客园共有,欢迎转载,未经作者同意须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。