UVa 208 消防车(dfs+剪枝)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=144

题意:给出一个n个结点的无向图以及某个结点k,按照字典序从小到大顺序输出从1到结点k的所有路径。

思路:如果直接矩阵深搜的话是会超时的,所以我们可以从终点出发,将与终点相连的连通块保存起来,这样dfs深搜时可以剪枝掉一些到达不了的点。只要解决了这个,dfs就是小问题。

这道题还有点坑的就是输出格式和它所给的格式不一样,注意一下。

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4
 5 const  int maxn = 22;
 6
 7 int n, step, route;
 8 int map[maxn][maxn];
 9 int path[maxn];
10 int vis[maxn];
11 int trunk[maxn];
12
13 void init(int cur)   //从终点开始遍历,保存与终点相连的连通块
14 {
15     trunk[cur] = 1;
16     for (int i = 1; i < maxn; i++)
17     {
18         if (map[cur][i] && !trunk[i])
19             init(i);
20     }
21 }
22
23 void dfs(int cur, int step)
24 {
25     if (cur == n)
26     {
27         cout << "1";
28         for (int i = 1; i < step; i++)
29             cout << " " << path[i];
30         cout << endl;
31         memset(path, 0, sizeof(0));
32         route++;
33     }
34     for (int i = 0; i < maxn; i++)
35     {
36         if (map[cur][i] && !vis[i] && trunk[i])
37         {
38             vis[i] = 1;
39             path[step] = i;
40             dfs(i, step + 1);
41             vis[i] = 0;
42         }
43     }
44     return;
45 }
46
47 int main()
48 {
49     //freopen("D:\\txt.txt", "r", stdin);
50     int a, b, kase = 0;
51     while (cin >> n && n)
52     {
53         memset(vis, 0, sizeof(vis));
54         memset(map, 0, sizeof(map));
55         memset(path, 0, sizeof(path));
56         memset(trunk, 0, sizeof(trunk));
57         while (cin >> a >> b)
58         {
59             if (!a && !b)  break;
60             map[a][b] = map[b][a] = 1;
61         }
62         vis[1] = 1;
63         step = 1;
64         route = 0; //记录路径数量
65         init(n); //计算保存连通块
66         cout << "CASE " << ++kase << ":" << endl;
67         dfs(1, 1);
68         cout << "There are " << route << " routes from the firestation to streetcorner " << n << "." << endl;
69
70     }
71     return 0;
72 }
时间: 2024-08-02 23:06:04

UVa 208 消防车(dfs+剪枝)的相关文章

UVA 10318 - Security Panel dfs 剪枝

UVA 10318 - Security Panel dfs 剪枝 ACM 题目地址:UVA 10318 - Security Panel 题意: 这题跟点灯的题目很像,点灯游戏选择一盏灯时会让它以及四周的灯改变状态. 但是我们有特殊的开开关技巧,它给出了改变状态的位置,而不是四周都改变. 问你从全部关着变成全部开着的最小开关步骤. 分析: 很明显,在一个位置上点两次或更多次是没有必要的,所以一个位置只有选择与不选择,用dfs即可,但如果暴力所有可能,复杂度是2^25,会超时,所以要剪枝. 由于

uva 219 - Department of Redundancy Department(dfs+剪枝)

题目链接:uva 219 - Department of Redundancy Department 题目大意:给定一些关系,问哪一些关系是可以被替代的,如果可以被替代,给出替代的方案,一种即可. 解题思路:因为总共也就26个字母,所以用二进制数表示状态.剪枝,每次将所有可选关系均考虑进去都无法满足则是false. #include <cstdio> #include <cstring> #include <algorithm> using namespace std;

ZOJ 1008 Gnome Tetravex (DFS + 剪枝)

Gnome Tetravex 题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 题意:有N*N个方格,每个方格分为上下左右四个部分,每个部分填数字.现在要求重排方块,使得每两个有边相连的方块对应的数字相同. 思路:就是一个简单的搜索,我想了个剪枝,将上下左右四个方向上每个数字对应的是哪几个方块记录下来,但是这个剪枝并没有起很大的作用,还是T了.后来才发现,如果有很多个方块是相同的,会重复搜索,所以需要将相同的方块一起处

Cubes(DFS+剪枝)

题意:给一个数N,求N最少由多少个数的立方构成,并输出这些数. 做法:DFS + 剪枝,剪枝的边界很很很重要! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <stdio.h> int cub[400]; int ans[300]; int tp[300]; int n; int sum = 0x3f3f3f3f; //个数 void dfs(int le

hdu 4109 dfs+剪枝优化

求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己增加的)出发,0到1~n个节点之间的距离为1,mt[i]表示从0点到第i个节点目前所得的最长路径 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> using namespace std; const

POJ 1564 Sum It Up (DFS+剪枝)

 Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5820   Accepted: 2970 Description Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4

EOJ1981 || POJ1011 经典dfs+剪枝+奇怪的数据

题目:EOJ1981 || POJ1011   经典dfs+剪枝+奇怪的数据 Description George took sticks of the same length and cut them randomly until all partsbecame at most 50 units long. Now he wants to return sticks to the originalstate, but he forgot how many sticks he had origi

HDOJ 5113 Black And White DFS+剪枝

DFS+剪枝... 在每次DFS前,当前棋盘的格子数量的一半小于一种颜色的数量时就剪掉 Black And White Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 194    Accepted Submission(s): 50 Special Judge Problem Description In mathematics,

HDU 1010 Tempter of the Bone dfs+剪枝

给你一个迷宫一个起点和一个终点,问你能否走T步刚好到达终点,不能重复走,并且只有4个方向 显然这是一个dfs,虽然N最大只有7,但是裸的dfs复杂度还是太高了,因此要进行一些剪枝 1.如果T比图上所有的可走点还要大,肯定是不可行的.这个可以避免dfs整张图. 2.奇偶剪枝,有性质当前点(x,y)到目标点(tx,ty)的所有路径的长度的奇偶性一定和|x-tx|+|y-ty|一样. #include <cstdio> #include <iostream> #include <c