习题7-1 uva 208(剪枝)
来源:互联网 发布:html小游戏源码 编辑:程序博客网 时间:2024/04/30 01:14
题意:按最小字典序输出a到b 的所有路径。
思路:先处理出个点到目标点b的情况(是否能到达),搜索即可。
最开始我只判了a能否到b,然后给我的是WA,然后看了半天感觉思路没什么问题,然后把所有点都处理出来,AC
实在是看不懂- -,好无语。
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <queue>#include <algorithm>typedef long long ll;using namespace std;int tmap[200][200];const int inf = 0x3f3f3f3f;int ans[200];int can[200];int n,a,b;int tmax;int vis[200];int all;void dfs(int cur,int num){ if(cur == n) { printf("1"); for(int i = 1; i < num; i++) printf(" %d",ans[i]); printf("\n"); all++; return ; } for(int i = 1; i <= tmax; i++) { if(!vis[i] && tmap[cur][i] && can[i]) { vis[i] = 1; ans[num] = i; dfs(i,num+1); vis[i] = 0; } } return ;}void get_can(int cur) //是否能到达终点{ can[cur] = 1; for(int i =1 ; i <= tmax; i++) if(tmap[cur][i] && !can[i]) get_can(i);}int main(){ int cas = 1; while(scanf("%d",&n)!=EOF) { memset(vis,0,sizeof(vis)); memset(can,0,sizeof(can)); memset(tmap,0,sizeof(tmap)); while(scanf("%d %d",&a,&b)) { if(a == 0 && b == 0) break; tmap[a][b] = tmap[b][a] = 1; if(a > tmax) tmax = a; if(b > tmax) tmax = b; } get_can(n); printf("CASE %d:\n",cas++); if(!can[n]) { printf("There are 0 routes from the firestation to streetcorner %d.\n",n); continue; } vis[1] = 1; all = 0; dfs(1,1); printf("There are %d routes from the firestation to streetcorner %d.\n",all,n); } return 0;}
0 0
- 习题7-1 uva 208(剪枝)
- 紫书习题 7-1 UVa 208 (爆搜加剪枝)
- 习题7-2 UVA - 225 Golygons (简单的暴力回溯法+剪枝)
- UVa 208 - Firetruck(剪枝)
- UVa #208 Firetruck (习题7-1)
- UVA 208 Firetruck 消防车(回溯 + 剪枝)
- Uva307 Sticks 【dfs+剪枝】【习题7-14】
- 紫书搜索 习题7-1 UVA
- UVa 1585 Score(习题3-1)
- 习题7-15:最大的数(dfs+最优性剪枝)
- UVa 208 - Firetruck,双向搜索进行剪枝
- UVa 208 - Firetruck 回溯+剪枝 数据
- uva 10160 Servicing Stations(DFS+剪枝)
- uva 307 Sticks(DFS+ 剪枝)
- [UVA 307] Sticks (DFS回溯 + 剪枝)
- uva:307 - Sticks(dfs + 剪枝)
- UVA - 10160 Servicing Stations(回溯+剪枝)
- UVA - 307 Sticks(回溯+剪枝)
- Hadoop WordCount详解(2.7.1版本)
- 关于MongoDB的几个重要的网站
- js写一个日期Ⅱ(完结)
- IOS笔记UI--main函数
- LeetCode Course Schedule BFS
- 习题7-1 uva 208(剪枝)
- 使用runtime将任意对象中所有属性转换为字典
- css3 @-webkit-keyframes 这种格式什么意思
- markdown使用手册
- 获取盘符的路径/文件/创建时间
- QQ第三方登录
- lightOJ 1005 【规律题】
- android 解决fragment中有百度高德等地图,切换出现闪一下黑屏的问题
- (NO.00001)iOS游戏SpeedBoy Lite成形记(十一)