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;
}