算法笔记---求迷宫中起点到终点的最少步数

71 篇文章 0 订阅
41 篇文章 3 订阅

题目描述

给定一个n*m大小的迷宫,其中“ * ”代表不可通过的墙壁,而“ . ”代表平地,S表示起点,T代表终点。移动过程中,如果当前位置是(x,y)(下标从0开始),且每次只能前往上下左右(x,y+1)、(x,y-1)、(x-1,y)、(x+1,y)四个位置的平地,求从起点S到达终点T的最少步数。

输入数据:

5 5  // 表示5行5列
. . . . .  //迷宫信息
. * . * .
. * S * .
. * * * .
. . . T *
2 2 4 3  // 起点S的坐标与终点T的坐标

解题思路:

在本题中,由于求的是最少步数,而BFS是通过层次的顺序来遍历的,因此可以从起点S开始计数遍历的层数,那么在到达终点T时的层数就是需要求解的起点S到达终点T的最少步数。

使用struct结构体来保存当前结点所在的行和列,以及当前的层次

struct Node
{
	int x, y;
	int step;//层数
	Node(int x, int y) {
		this->x = x;
		this->y = y;
		this->step = 0;
	}
};

下面为实现代码:

#include <iostream>
using namespace std;
#include <vector>
#include <string>
#include <queue>

struct Node
{
	int x, y;
	int step;//层数
	Node(int x, int y) {
		this->x = x;
		this->y = y;
		this->step = 0;
	}
};

//当前坐标的四周
const int X[] = { 0, 0, -1, 1 };
const int Y[] = { -1, 1, 0, 0 };

//判断当前位置是否可以访问
bool judge(vector< vector<char> >& matrix, int rows, int cols, int x, int y, bool* visited) {
	//判断越界
	if (x < 0 || x >= rows || y < 0 || y >= cols)
	{
		return false;
	}
	if (matrix[x][y] == '*' || visited[x * cols + y])
	{
		return false;
	}
	return true;
}

//************************************
// Method:    bfs 求起点到终点的最短步数
// FullName:  bfs
// Access:    public 
// Returns:   int
// Qualifier:
// Parameter: vector< vector<char> > & matrix 给定矩阵
// Parameter: int rows 给定矩阵的总行数
// Parameter: int cols 给定矩阵的总列数
// Parameter: int x 当前位置所在的行
// Parameter: int y 当前位置所在的列
// Parameter: int tx 终点所在的行
// Parameter: int ty 终点所在的列
// Parameter: bool * visited 当前坐标是否已经入过队
//************************************
int bfs(vector< vector<char> >& matrix, int rows, int cols, int x, int y, int tx, int ty, bool* visited) {
	//下面为bfs的模板
	queue<Node> q;
	Node node(x, y);
	q.push(node);
	while (!q.empty())
	{
		Node top = q.front();
		q.pop();
		if (top.x == tx && top.y == ty)
		{
			return top.step;
		}
		for (int i = 0; i < 4; i++)
		{
			int newX = top.x + X[i];
			int newY = top.y + Y[i];
			if (judge(matrix,rows,cols,newX,newY,visited))//遍历四个方向
			{
				node.x = newX;
				node.y = newY;
				node.step = top.step + 1;//层数+1
				q.push(node);
				visited[cols * newX + newY] = true;
			}
		}
	}
	return -1;// 无法到达终点时返回-1
}

//将 str 转为 char 数组
vector<char> strToVector(string str) {
	vector<char> res;
	for (int i = 0; i < str.size();i++)
	{
		if (str[i] != ' ')
		{
			res.push_back(str[i]);
		}
	}
	return res;
}

//求起点到达终点的最少步数
int main() {
	vector< vector<char> > matrix;
	int rows, cols;
	cin >> rows >> cols;
	getchar();
	string str;
	for (int i = 0;i < rows;i++)
	{
		getline(cin, str);
		matrix.push_back(strToVector(str));
	}
	int x, y, tx, ty;
	cin >> x >> y >> tx >> ty;
	bool* visited = new bool[rows * cols];//表示当前结点是否已经入过队
	memset(visited, false, rows * cols);
	cout << bfs(matrix, rows, cols, x, y, tx, ty, visited) << endl;
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]和引用\[2\]的描述,我们可以得知这是一个迷宫最少步数的问题。迷宫由R行C列的格子组成,其有些格子是障碍物不能走,有些是空地可以走。我们需要从左上角走到右下角,最少需要走多少步。只能在水平方向或垂直方向走,不能斜着走。 根据引用\[2\]的输入描述,我们可以得到迷宫的行数和列数R和C。接下来是R行,每行C个字符,代表整个迷宫。空地格子用'.'表示,有障碍物的格子用'#'表示。迷宫的左上角和右下角都是'.'。 根据引用\[3\]的输入描述,我们可以得到迷宫的行数和列数n和m。接下来n行,每行一个长为m的字符串,表示整个迷宫的布局。字符'.'表示空地,'#'表示墙,'S'表示起点,'T'表示出口。 要走出迷宫最少步数,我们可以使用广度优先搜索算法来解决。从起点开始,依次遍历相邻的空地格子,将其加入队列,并记录步数。直到找到出口为止,返回最少步数。 因为题目没有给出具体的迷宫布局,所以无法给出具体的最少步数。但是根据算法的思路,我们可以根据给定的迷宫布局进行计算,得出最少步数。 #### 引用[.reference_title] - *1* *2* [【蓝桥杯】2.走出迷宫最少步数——DFS](https://blog.csdn.net/m0_49755093/article/details/122547313)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [追梦算法----走出迷宫最少步数2](https://blog.csdn.net/m0_56501550/article/details/123735127)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值