uva_208-Firetruck(救火车)
2012-05-20 23:17
381 查看
暴搜直接TLE,但是进行预处理,即只搜索终点所连接到的点,一下子就只跑了8ms,神奇的概率。
搜索2:
搜索2:
#include<cstdio> #include<cstring> #include <vector> #include<algorithm> using namespace std; int path[21]; vector <int> ok; bool vis[21]; bool a[21][21]; int x, y, t; int cnt = 0; int nCase = 1; void pre_dfs(int cur) { vis[cur] = true; ok.push_back(cur); for (int i = 1; i < 21; i++) if (!vis[i] && a[cur][i]) pre_dfs(i); } void dfs( int cur, int len) { int i ; if (cur == t) { cnt++; for(i = 0; i < len-1; i++ ) printf("%d ", path[i]); printf("%d\n", path[i]); return; } for (i = 0; i < ok.size(); i++) { if (!vis[ok[i]] && a[cur][ok[i]] ) { path[len] = ok[i]; vis[ok[i]] = true; dfs(ok[i], len+1); vis[ok[i]] = false; } } } main() { // FILE *fp = fopen("in.txt", "r"); while(scanf("%d", &t) != EOF) { memset(a, false, sizeof(a)); memset(vis, false, sizeof(vis)); memset(path, 0, sizeof(path)); ok.clear(); while (scanf("%d %d", &x, &y) !=EOF && (x||y)) { a[x][y] = a[y][x] = true; } printf("CASE %d:\n", nCase++); pre_dfs(t); memset(vis, false, sizeof(vis)); vis[1] = true; path[0] = 1; cnt = 0; sort(ok.begin(), ok.end()); dfs(1, 1); printf("There are %d routes from the firestation to streetcorner %d.\n", cnt, t); } // getchar(); return 0; }
相关文章推荐
- 习题7-1 消防车(Firetruck, ACM/ICPC World Finals 1991, UVa208)
- 双向搜索(UVA 208 Firetruck)
- UVA208-Firetruck
- UVa208,Firetruck
- 208UVA救火车
- uva 208 - Firetruck
- uva 208 Firetruck
- uva 208 Firetruck (回溯)
- uva208 - Firetruck
- uva 208 - Firetruck
- uva208 - Firetruck
- UVA 208 Firetruck
- uva 208 Firetruck
- uva 208 Firetruck
- UVa 208 - Firetruck 回溯+剪枝 数据
- 【习题 7-1 UVA-208】Firetruck
- UVA 208 Firetruck(DFS)
- uva 208 Firetruck
- UVA - 208 Firetruck(floyd+DFS)
- UVA-208 Firetruck (回溯)