迷宫(bfs)

链接:https://ac.nowcoder.com/acm/contest/332/J
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

你在一个 n 行 m 列的网格迷宫中,迷宫的每一格要么为空,要么有一个障碍。
你当前在第 r 行第 c 列(保证该格子为空)。每次移动你可以向上下左右任意一个方向移动一格,前提是不能走到障碍上,也不能超出迷宫的边界。
你向左移动的次数不能超过 x 次,向右不能超过 y 次。
问在这种情况下,对于每个格子,是否存在一种移动方案让你走到它。
输出有多少个格子存在移动方案让你走到它。

输入描述:

第一行两个正整数 n,m 。
第二行两个正整数 r,c ,保证 1≤r≤n ,1≤c≤m 。
第三行两个整数 x,y ,保证 0≤x,y≤10^9 。
接下来 n 行,每行一个长度为 m 的字符串,
第 i 行第 j 个字符表示迷宫第 i 行第 j 列的格子,
字符为`.` 表示格子为空,字符为`*` 表示格子上有一个障碍。

输出描述:

输出一个数,表示有多少个格子存在移动方案让你走到它。

示例1

输入

复制

4 5
3 2
1 2
.....
.***.
...**
*....

输出

复制

10

说明

将能走到的格子用+标记:

+++..
+***.
+++**
*+++.

示例2

输入

复制

4 4
2 2
0 1
....
..*.
....
....

输出

复制

7

说明

.++.
.+*.
.++.
.++.

备注:

对于全部数据, 1≤n,m≤1000 。

题意:当时没做出来,但是很多过的,很迷,后来发现题目理解错了,题目意思是说,只要能走到就行!!很多种方案~~任意一种能走到就OK~

题解:简单bfs,用bfs爆搜就好了~~巨水~~详细请看代码:

#include <iostream>
#include <queue>
using namespace std;
const int MAX = 1e3+10;
const int mv[4][2]={1,0,-1,0,0,1,0,-1};
struct hh{
	int x,y;
	int l,r;
};
bool vis[MAX][MAX];
char mp[MAX][MAX];
int n,m,r,c,x,y;
int sum;//用来计数
void bfs(){
	queue<hh> q;
	hh tmp;
	tmp.x=r;tmp.y=c;
	tmp.l=x;tmp.r=y;
	q.push(tmp);
	vis[r][c]=true;//标记过的,就不走了
	while(!q.empty()){
		hh tp=q.front();
		q.pop();
		sum++;//计数
		for (int i = 0; i < 4;i++){
			int xx=tp.x+mv[i][0];
			int yy=tp.y+mv[i][1];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.'&&!vis[xx][yy]){
				vis[xx][yy]=true;//别忘了标记~
				if(i==0||i==1){//上下
					tmp.x=xx;tmp.y=yy;
					tmp.l=tp.l;tmp.r=tp.r;
					q.push(tmp);
				}
				else if(i==2&&tp.r>=1){//往右
					tmp.x=xx;tmp.y=yy;
					tmp.l=tp.l;tmp.r=tp.r-1;
					q.push(tmp);
				}
				else if(i==3&&tp.l>=1){//往左
					tmp.x=xx;tmp.y=yy;
					tmp.l=tp.l-1;tmp.r=tp.r;
					q.push(tmp);
				}
			}
		}
	}
}
int main(){
	cin >> n >> m >> r >> c >> x >> y;
	for (int i = 1; i <= n;i++){
		for (int j = 1; j <= m;j++){
			cin >> mp[i][j];
		}
	}
	bfs();
	cout << sum << endl;
	return 0;
}

 

在C++中实现迷宫问题的BFS(广度优先搜索)算法可以分为以下几个步骤: 1. 定义迷宫地图,通常用二维数组表示,0代表通路,1代表墙。 2. 定义一个队列用于存储路径中的节点。 3. 确定搜索方向,比如上下左右四个方向。 4. 从起点开始,将起点加入队列,并将起点的位置标记为已访问。 5. 循环执行以下操作,直到队列为空: - 从队列中取出一个元素作为当前位置。 - 检查当前位置是否是终点,如果是,则返回路径。 - 如果不是终点,则将当前位置的所有未访问的邻居加入队列,并更新邻居的位置为已访问。 6. 如果队列为空还未找到终点,则表示没有路径。 下面是一个简单的迷宫BFS算法的C++代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <utility> // for std::pair using namespace std; // 定义方向数组,分别表示上下左右 const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 检查给定的坐标(x, y)是否在迷宫内且未被访问 bool isValid(int x, int y, int n, int m, vector<vector<int>>& maze, vector<vector<bool>>& visited) { return (x >= 0 && x < n && y >= 0 && y < m && maze[x][y] == 0 && !visited[x][y]); } // 广度优先搜索算法,返回从起点到终点的路径长度,如果无法到达则返回-1 int BFS(vector<vector<int>>& maze, pair<int, int> start, pair<int, int> end) { int n = maze.size(); int m = maze[0].size(); vector<vector<bool>> visited(n, vector<bool>(m, false)); queue<pair<int, pair<int, int>>> q; // 队列中存储位置和前一个位置的信息 q.push(make_pair(0, start)); // 将起点加入队列 visited[start.first][start.second] = true; while (!q.empty()) { pair<int, pair<int, int>> front = q.front(); q.pop(); int dist = front.first; pair<int, int> pos = front.second; // 到达终点 if (pos == end) { return dist; } // 遍历四个方向 for (int i = 0; i < 4; ++i) { int newX = pos.first + dir[i][0]; int newY = pos.second + dir[i][1]; if (isValid(newX, newY, n, m, maze, visited)) { visited[newX][newY] = true; q.push(make_pair(dist + 1, make_pair(newX, newY))); } } } return -1; // 如果无法到达终点 } int main() { vector<vector<int>> maze = { {0, 1, 0, 0, 0}, {0, 1, 0, 1, 0}, {0, 0, 0, 1, 0}, {0, 1, 1, 1, 0}, {0, 0, 0, 0, 0} }; pair<int, int> start(0, 0); // 迷宫的起点 pair<int, int> end(4, 4); // 迷宫的终点 int pathLength = BFS(maze, start, end); if (pathLength != -1) { cout << "Path found, length: " << pathLength << endl; } else { cout << "No path found." << endl; } return 0; } ``` 以上代码展示了如何使用BFS算法解决迷宫问题,并在主函数中使用了一个简单的迷宫实例。它会输出找到的路径长度,或者当没有路径时输出无法到达的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心脏dance

如果解决了您的疑惑,谢谢打赏呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值