pta 7-6 冬奥会接驳车

冬奥会无人驾驶接驳车:BFS算法解决最短路径问题
本文介绍了一种使用BFS算法来规划冬奥会无人驾驶接驳车从奥运村到比赛场馆的最短路径,通过实例展示了如何在给定的地图中应用该算法。

OJ是PTA 摘自吉林大学

找最短路径一般是比较适合用BFS层层遍历 而且没有权重 不用priority_queue 所以答案明了了起来

就是BFS

艾灵是冬奥会的一名运动员,今天她准备从奥运村前往比赛场馆进行比赛,本届冬奥会科技感十足,奥组委配备了无人驾驶的接驳车。假定你是奥组委的软件工程师,请你为无人接驳车编写路径规划程序,使其载着艾灵以最短的时间到达目的地。

接驳车的运行范围可以被建模为一张由n行m列单元格组成的地图。有的单元格是空地,可以走;有的单元格处是障碍物,不能走。假定接驳车只可以朝上、下、左、右四个方向行驶,不能斜着走。每行驶过一个位置(单元格)需要1分钟。给定地图以及艾灵的起点和终点,请输出接驳车从起点行驶到终点所需的最短时间。

输入格式:

输入包含多组数据。每组数据第一行是两个整数n和m (1≤ m, n ≤100),表示地图的长和宽;接下来是n行,每行m个数字,表示整个地图。空地格子用0表示,障碍物用1表示,艾灵所在起点用3表示,目的地用4表示。

输出格式:

对于每组数据,如果接驳车能够到达目的地,输出一个整数,表示艾灵从起点到目的地所需的最短时间,如果不能到达目的地,输出“unreachable”。

输入样例:

5 5
1 0 1 1 1
1 0 4 1 0
1 0 0 1 0
0 0 0 1 0
1 0 3 0 1
5 5
3 0 1 1 1
1 0 1 1 0
1 0 1 1 0
0 0 0 1 0
1 0 1 0 4

输出样例:

3
unreachable
#include <bits/stdc++.h>
using namespace std;
struct Node {
	pair<int, int> pLocate;//坐标
	int times;//层数/次数 用于计数 最短路径
	Node(pair<int,int>,int);

};
Node::Node(pair<int,int> _pLocate,int _Times) {
	this->pLocate = _pLocate;
	this->times = _Times;//ctors
}
class solution6 {
public:
	solution6();
};
bool Visited[15][15];
int BFS(int** _Maze,int x0,int y0,int x1,int y1,int n,int m) {
	int nLength = 0;
	queue<Node> q;
	q.push(Node(pair<int,int>(x0,y0),0));
	Visited[x0][y0]=1;
	while (!q.empty()) {
		auto temp = q.front();
		q.pop();
		if (temp.pLocate.first != n - 1){
		if (_Maze[temp.pLocate.first+1][temp.pLocate.second] == 0 && Visited[temp.pLocate.first+1][temp.pLocate.second] == 0) {
			
			q.push(Node(pair<int, int>(temp.pLocate.first + 1, temp.pLocate.second), temp.times + 1));
			
			Visited[temp.pLocate.first + 1][temp.pLocate.second] =1;
		}
		if (_Maze[temp.pLocate.first + 1][temp.pLocate.second] == 4) {
			return temp.times+1;
		}
		}
		if (temp.pLocate.second != m - 1) {
		if (_Maze[temp.pLocate.first][temp.pLocate.second+1] == 0 && Visited[temp.pLocate.first][temp.pLocate.second+1] == 0) {
			
			q.push(Node(pair<int, int>(temp.pLocate.first, temp.pLocate.second + 1), temp.times + 1));

			Visited[temp.pLocate.first][temp.pLocate.second + 1] = 1;
		}
		if (_Maze[temp.pLocate.first][temp.pLocate.second + 1] == 4) {
			return temp.times+1;
		}
		}
		if (temp.pLocate.first != 0){
		if (_Maze[temp.pLocate.first-1][temp.pLocate.second] == 0 && Visited[temp.pLocate.first-1][temp.pLocate.second] == 0) {
			
			q.push(Node(pair<int,int>(temp.pLocate.first - 1, temp.pLocate.second),temp.times+1));

			Visited[temp.pLocate.first - 1][temp.pLocate.second] = 1;
		}
		if (_Maze[temp.pLocate.first - 1][temp.pLocate.second] == 4) {
			return temp.times+1;
		}
		}
		if (temp.pLocate.second != 0){
		if (_Maze[temp.pLocate.first][temp.pLocate.second-1] == 0 && Visited[temp.pLocate.first][temp.pLocate.second-1] == 0) {
			
			q.push(Node(pair<int, int>(temp.pLocate.first, temp.pLocate.second - 1), temp.times + 1));
			
			Visited[temp.pLocate.first][temp.pLocate.second - 1] = 1;
		}
		if (_Maze[temp.pLocate.first][temp.pLocate.second - 1] == 4) {
			return temp.times+1;
		}
		}
	}
	//肯定不成功
	return 0;

}
solution6::solution6() {
	vector <string> show;
	int n, m, x0, y0, x1, y1;
	//循环输入 遇到unreachable的时候停止
	while (cin>>n>>m) {
		int** maze = (int**)new int*[n];//分配动态二维数组
		for (int i = 0; i < n; i++) {
			maze[i] = (int*)new int[m];
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> maze[i][j];
				if (maze[i][j] == 3) { x0 = i; y0 = j; }
				if (maze[i][j] == 4) { x1 = i; y1 = j; }
			}
		}
		int times = BFS(maze, x0, y0, x1, y1, n, m);
		(times)? (cout << times << endl) : (cout << "unreachable" << endl);
		//if (times) {
		//	cout << times << endl;
		//}
		//else {
		//	cout << "unreachable" << endl;
		//}

	}

	//for (int i = 0; i < show.size(); i++) {
	//	cout << show.at(i) << endl;
	//}
}
int main() {
	solution6 s6;
	return 0;
}

### PTA 题目 7-5 和 7-6 的分析 #### 关于题目背景 PTA 平台上的编程练习通常涉及算法设计、数据结构应用以及逻辑推理能力。对于题目编号为 **7-5** 和 **7-6** 的两道题,其核心主题围绕着模拟现实场景中的为模式——具体来说,“撒狗粮”的情境被抽象成了一种程序化的表达方式。 以下是针对这两道题目的可能实现方法及其解析: --- #### 题目 7-5 解析与实现 这道题目主要考察的是字符串处理能力和条件判断技巧。假设输入是一系情侣之间的互动记录,目标是从这些记录中筛选出特定的为并统计频率。 ##### 实现思路 通过读取每条记录,提取关键字来匹配预定义的“撒狗粮”标准,并计算符合条件的数量。 ```python def count_dog_food(records, keywords): """ 统计给定记录表中有多少次满足关键词集合的标准 :param records: list[str], 输入的情侣互动记录 :param keywords: set[str], 定义为“撒狗粮”的关键词集合 :return: int, 符合条件的次数 """ count = 0 for record in records: words = record.split() # 将单条记录拆分为单词表 if any(word in keywords for word in words): # 如果任意单词属于关键词集,则计入一次 count += 1 return count # 示例调用 records_example = [ "Alice gave Bob a heart-shaped chocolate", "Bob and Alice went to the park together", "Charlie bought himself a new book" ] keywords_set = {"heart", "together"} # 假设这两个词代表撒狗粮为 result = count_dog_food(records_example, keywords_set) print(f"Total dog food actions: {result}") # 输出结果应为2 ``` 上述代码片段展示了如何利用简单的字符串操作完成任务[^1]。 --- #### 题目 7-6 解析与实现 此部分进一步扩展了前一题的功能需求,在原有基础上增加了时间维度考量或者更复杂的交互关系建模。例如,不仅关注某一天内的活动频次,还希望了解长期趋势变化;又或者是引入社交网络概念,评估不同个体之间的影响程度差异等。 ##### 数据结构选择建议 为了高效存储和查询大量用户间的关系链路信息,推荐采用图(Graph)形式表示整个系统状态。其中节点(Node)对应参与者(即人物角色),边(Edge)则用来描述他们彼此间的联系强度或事件发生概率。 下面给出基于邻接表(adjacency list)构建图模型的一个例子: ```python from collections import defaultdict class SocialGraph: def __init__(self): self.graph = defaultdict(list) def add_edge(self, u, v): """向无向图添加一条连接""" self.graph[u].append(v) self.graph[v].append(u) def bfs_traversal(self, start_node): """执广度优先遍历(BFS), 返回访问顺序""" visited = set() queue = [start_node] traversal_order = [] while queue: current = queue.pop(0) if current not in visited: visited.add(current) traversal_order.append(current) neighbors = sorted([node for node in self.graph[current] if node not in visited]) queue.extend(neighbors) return traversal_order # 创建实例对象 sg = SocialGraph() # 添加几组样本关联 pairs_of_friends = [("Alice", "Bob"), ("Bob", "Eve"), ("Alice", "Eve")] for pair in pairs_of_friends: sg.add_edge(*pair) # 展示从“Alice”出发可达的所有朋友名单 path_result = sg.bfs_traversal("Alice") print("Friends reachable via BFS:", path_result) ``` 这里采用了Breadth First Search (BFS)[^2] 来探索由初始顶点所能触及到的所有其他成员路径长度最短的情况。 --- ### 总结说明 以上分别就两个层次的任务提出了相应的解决方案框架。前者侧重基础功能达成,后者尝试挖掘潜在深层次规律特性。当然实际编码过程中还需要注意边界情况处理以及其他细节优化方面的工作。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值