1053 Path of Equal Weight

本文探讨了在一个加权树中寻找所有从根节点到叶节点,且路径上节点权重之和等于给定数值的问题。通过使用广度优先搜索(BFS),文章提供了一种有效的解决方案,并附带了代码实现。此外,还介绍了如何对找到的路径进行排序。

1053 Path of Equal Weight
题目链接
Given a non-empty tree with root R, and with weight W​i
​​ assigned to each tree node T​i
​​ . The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L.

Now given any weighted tree, you are supposed to find all the paths with their weights equal to a given number. For example, let’s consider the tree showed in the following figure: for each node, the upper number is the node ID which is a two-digit number, and the lower number is the weight of that node. Suppose that the given number is 24, then there exists 4 different paths which have the same given weight: {10 5 2 7}, {10 4 10}, {10 3 3 6 2} and {10 3 3 6 2}, which correspond to the red edges in the figure.

Input Specification:
Each input file contains one test case. Each case starts with a line containing 0<N≤100, the number of nodes in a tree, M (<N), the number of non-leaf nodes, and 0<S<2
​30
​​ , the given weight number. The next line contains N positive numbers where W
​i
​​ (<1000) corresponds to the tree node T
​i
​​ . Then M lines follow, each in the format:

ID K ID[1] ID[2] … ID[K]
where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID’s of its children. For the sake of simplicity, let us fix the root ID to be 00.

在这里插入图片描述
Output Specification:
For each test case, print all the paths with weight S in non-increasing order. Each path occupies a line with printed weights from the root to the leaf in order. All the numbers must be separated by a space with no extra space at the end of the line.

Note: sequence {A
​1
​​ ,A
​2
​​ ,⋯,A
​n
​​ } is said to be greater than sequence {B
​1
​​ ,B
​2
​​ ,⋯,B
​m
​​ } if there exists 1≤k<min{n,m} such that A
​i
​​ =B
​i
​​ for i=1,⋯,k, and A
​k+1
​​ >B
​k+1
​​ .

Sample Input:
20 9 24
10 2 4 3 5 10 2 18 9 7 2 2 1 3 12 1 8 6 2 2
00 4 01 02 03 04
02 1 05
04 2 06 07
03 3 11 12 13
06 1 09
07 2 08 10
16 1 15
13 3 14 16 17
17 2 18 19
Sample Output:
10 5 2 7
10 4 10
10 3 3 6 2
10 3 3 6 2
题目大意:现有一颗树,每个结点都有一个权重,求一条从根结点到叶子结点的路径,使路径上的结点的权重之和等于给定值。求出满足条件的所有路径。
分析:求路径,可使用bfs。本题给出两种参考方法。
路径排序的话,使用sort函数。
参考代码:

#include<iostream>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
struct  node{
	int w;
	vector<int>child;
};
int S;
vector<int>path, temp_path;
vector<node>tree;
vector<vector<int>>paths;
void bfs(int id, int sum, int S) {
	sum += tree[id].w;
	path.push_back(tree[id].w);
	if (sum < S) {
		for (int i = 0; i < tree[id].child.size(); i++) {
			int vec = tree[id].child[i];
			bfs(vec, sum, S);
		}
		if (!path.empty()) {
			path.pop_back();
		}
		//return;
	}
	else if (sum > S) {
		path.pop_back();
		//return;
	}
	else if (sum == S) {
		if (tree[id].child.size() == 0) {
			paths.push_back(path);
			path.pop_back();
			//return;
		}
		else {
			path.pop_back();
			//return;
		}
	}
}
bool cmp(vector<int>a, vector<int>b) {
	int la = a.size(), lb = b.size();
	for (int i = 0; i < la&&i < lb; i++) {
		if (a[i] != b[i])
			return a[i] > b[i];
	}
	if (la > lb)return true;
	else return false;
}
void BFS(int id, int vecNum,int sum) {
	temp_path[vecNum] = tree[id].w;
	sum += tree[id].w;
	if (sum > S)return;
	else if (sum == S) {
		if (tree[id].child.size() == 0) {
			vector<int>ans;
			for (int i = 0; i <= vecNum; i++) {
				ans.push_back(temp_path[i]);
			}
			paths.push_back(ans);
		}
		else {  return;	}
	}
	else if (sum < S) {
		for (int i = 0; i < tree[id].child.size(); i++) {
			BFS(tree[id].child[i], vecNum + 1, sum);
		}
	}
}

int main() {
	int N, M, id, k, vec;
	cin >> N >> M >> S;
	tree.resize(N);
	temp_path.resize(N);
	for (int i = 0; i < N; i++) {
		cin >> tree[i].w;
	}
	for (int i = 1; i <= M; i++) {
		cin >> id >> k;
		for (int j = 1; j <= k; j++) {
			cin >> vec;
			tree[id].child.push_back(vec);
		}
	}
	//bfs(0, 0, S);
	BFS(0, 0, 0);
	sort(paths.begin(), paths.end(), cmp);

	for (int i = 0; i < paths.size(); i++) {
		for (int j = 0; j < paths[i].size(); j++) {
			cout << paths[i][j];
			if (j == paths[i].size() - 1)cout << endl;
			else cout << " ";
		}
	}

	return 0;
}

欢迎评论!!!

机器人路径规划算法是机器人领域的重要研究内容,用于在给定环境中为机器人找到一条从起始点到目标点的最优或可行路径。以下是几种常见的机器人路径规划算法及其细节: ### A*算法 A*算法是一种启发式搜索算法,结合了Dijkstra算法的最优路径搜索特性和贪心最佳优先搜索算法的高效性。它通过维护一个开放列表和一个关闭列表,不断扩展节点直到找到目标节点。其核心公式为 $f(n) = g(n) + h(n)$,其中 $g(n)$ 是从起始节点到当前节点的实际代价,$h(n)$ 是从当前节点到目标节点的启发式估计代价。 ```python import heapq def heuristic(a, b): # 曼哈顿距离作为启发式函数 return abs(a[0] - b[0]) + abs(a[1] - b[1]) def astar(array, start, goal): neighbors = [(0, 1), (0, -1), (1, 0), (-1, 0)] close_set = set() came_from = {} gscore = {start: 0} fscore = {start: heuristic(start, goal)} open_heap = [] heapq.heappush(open_heap, (fscore[start], start)) while open_heap: current = heapq.heappop(open_heap)[1] if current == goal: data = [] while current in came_from: data.append(current) current = came_from[current] return data close_set.add(current) for i, j in neighbors: neighbor = current[0] + i, current[1] + j tentative_g_score = gscore[current] + heuristic(current, neighbor) if 0 <= neighbor[0] < array.shape[0]: if 0 <= neighbor[1] < array.shape[1]: if array[neighbor[0]][neighbor[1]] == 1: continue else: # 越界 continue else: # 越界 continue if neighbor in close_set and tentative_g_score >= gscore.get(neighbor, 0): continue if tentative_g_score < gscore.get(neighbor, 0) or neighbor not in [i[1] for i in open_heap]: came_from[neighbor] = current gscore[neighbor] = tentative_g_score fscore[neighbor] = tentative_g_score + heuristic(neighbor, goal) heapq.heappush(open_heap, (fscore[neighbor], neighbor)) return None ``` ### Dijkstra算法 Dijkstra算法是一种广度优先搜索算法,用于在加权中找到从单个源节点到所有其他节点的最短路径。它通过不断扩展距离源节点最近的节点,更新其邻居节点的距离,直到所有可达节点都被访问。 ```python import heapq def dijkstra(graph, start): distances = {node: float('inf') for node in graph} distances[start] = 0 priority_queue = [(0, start)] while priority_queue: current_distance, current_node = heapq.heappop(priority_queue) if current_distance > distances[current_node]: continue for neighbor, weight in graph[current_node].items(): distance = current_distance + weight if distance < distances[neighbor]: distances[neighbor] = distance heapq.heappush(priority_queue, (distance, neighbor)) return distances ``` ### 人工势场法 人工势场法将机器人在环境中的运动看作是在虚拟力场中的运动。目标点对机器人产生引力,障碍物对机器人产生斥力,机器人在合力的作用下向目标点移动。该方法计算简单,实时性好,但容易陷入局部极小值。 ### 遗传算法 遗传算法是一种基于生物进化原理的优化算法。它通过模拟自然选择和遗传机制,对路径进行编码,生成初始种群,然后通过选择、交叉和变异等操作不断进化种群,直到找到最优路径。 ### RRT(快速随机树)算法 RRT算法是一种基于采样的路径规划算法,通过随机采样在状态空间中快速扩展一棵树,直到树的某个节点接近目标点。该算法适用于高维空间和复杂环境,但生成的路径通常不是最优的。 ```python import numpy as np import matplotlib.pyplot as plt class RRT: def __init__(self, start, goal, obstacle_list, rand_area, expand_dis=3.0, path_resolution=0.5, goal_sample_rate=5, max_iter=500): self.start = Node(start[0], start[1]) self.goal = Node(goal[0], goal[1]) self.obstacle_list = obstacle_list self.min_rand = rand_area[0] self.max_rand = rand_area[1] self.expand_dis = expand_dis self.path_resolution = path_resolution self.goal_sample_rate = goal_sample_rate self.max_iter = max_iter self.node_list = [] def planning(self, animation=True): self.node_list = [self.start] for i in range(self.max_iter): rnd_node = self.get_random_node() nearest_node = self.get_nearest_node(self.node_list, rnd_node) new_node = self.steer(nearest_node, rnd_node, self.expand_dis) if self.check_collision(new_node, self.obstacle_list): self.node_list.append(new_node) if animation and i % 5 == 0: self.draw_graph(rnd_node) if self.calc_dist_to_goal(self.node_list[-1].x, self.node_list[-1].y) <= self.expand_dis: final_node = self.steer(self.node_list[-1], self.goal, self.expand_dis) if self.check_collision(final_node, self.obstacle_list): return self.generate_final_course(len(self.node_list) - 1) return None def get_random_node(self): if np.random.randint(0, 100) > self.goal_sample_rate: rnd = Node(np.random.uniform(self.min_rand, self.max_rand), np.random.uniform(self.min_rand, self.max_rand)) else: # goal point sampling rnd = Node(self.goal.x, self.goal.y) return rnd def get_nearest_node(self, node_list, rnd_node): dlist = [(node.x - rnd_node.x) ** 2 + (node.y - rnd_node.y) ** 2 for node in node_list] minind = dlist.index(min(dlist)) return node_list[minind] def steer(self, from_node, to_node, extend_length=float("inf")): new_node = Node(from_node.x, from_node.y) d, theta = self.calc_distance_and_angle(new_node, to_node) new_node.path_x = [new_node.x] new_node.path_y = [new_node.y] if extend_length > d: new_node.x = to_node.x new_node.y = to_node.y else: new_node.x = from_node.x + extend_length * np.cos(theta) new_node.y = from_node.y + extend_length * np.sin(theta) n_expand = int(np.floor(extend_length / self.path_resolution)) for _ in range(n_expand): new_node.path_x.append(new_node.path_x[-1] + self.path_resolution * np.cos(theta)) new_node.path_y.append(new_node.path_y[-1] + self.path_resolution * np.sin(theta)) new_node.path_x.append(new_node.x) new_node.path_y.append(new_node.y) new_node.parent = from_node return new_node def check_collision(self, node, obstacle_list): for (ox, oy, size) in obstacle_list: dx_list = [ox - x for x in node.path_x] dy_list = [oy - y for y in node.path_y] d_list = [dx * dx + dy * dy for (dx, dy) in zip(dx_list, dy_list)] if min(d_list) <= size ** 2: return False # collision return True # safe def calc_dist_to_goal(self, x, y): dx = x - self.goal.x dy = y - self.goal.y return np.sqrt(dx ** 2 + dy ** 2) def generate_final_course(self, goal_ind): path = [[self.goal.x, self.goal.y]] node = self.node_list[goal_ind] while node.parent is not None: path.append([node.x, node.y]) node = node.parent path.append([node.x, node.y]) return path def draw_graph(self, rnd=None): plt.clf() # for stopping simulation with the esc key. plt.gcf().canvas.mpl_connect( 'key_release_event', lambda event: [exit(0) if event.key == 'escape' else None]) if rnd is not None: plt.plot(rnd.x, rnd.y, "^k") for node in self.node_list: if node.parent is not None: plt.plot(node.path_x, node.path_y, "-g") for (ox, oy, size) in self.obstacle_list: self.plot_circle(ox, oy, size) plt.plot(self.start.x, self.start.y, "xr") plt.plot(self.goal.x, self.goal.y, "xr") plt.axis("equal") plt.axis([-2, 15, -2, 15]) plt.grid(True) plt.pause(0.01) @staticmethod def plot_circle(x, y, size, color="-b"): # pragma: no cover deg = list(range(0, 360, 5)) deg.append(0) xl = [x + size * np.cos(np.deg2rad(d)) for d in deg] yl = [y + size * np.sin(np.deg2rad(d)) for d in deg] plt.plot(xl, yl, color) @staticmethod def calc_distance_and_angle(from_node, to_node): dx = to_node.x - from_node.x dy = to_node.y - from_node.y d = np.sqrt(dx ** 2 + dy ** 2) theta = np.arctan2(dy, dx) return d, theta class Node: def __init__(self, x, y): self.x = x self.y = y self.path_x = [] self.path_y = [] self.parent = None ``` ### 相关问题 1. 这些路径规划算法在不同环境下的性能如何比较? 2. 如何改进A*算法以避免陷入局部最优解? 3. 遗传算法在路径规划中的收敛速度如何提高? 4. RRT算法生成的路径如何进行优化? 5. 人工势场法的局部极小值问题有哪些解决方法?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值