wyh的迷宫 简单BFS

wyh的迷宫

题目
给你一个n*m的迷宫,这个迷宫中有以下几个标识:
s代表起点
t代表终点
x代表障碍物
.代表空地
现在你们涵哥想知道能不能从起点走到终点不碰到障碍物(只能上下左右进行移动,并且不能移动到已经移动过的点)。

1
3 5
s…x
x…x
…tx
YES

思路+code

简单的板子运用

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define INF 0x3f3f3f3f
#define PII pair<int, int>
#define rep(i, l, r) for (int i = l; i < r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define rep2(i, l, r) for (int i = l; i * i <= r; i++)
#define rep3(i, l, r) for (LL i = l; i * i * i <= r; i++)
#define Min(a, b) a > b ? b : a
#define Max(a, b) a > b ? a : b
#define endl '\n'
#define debug "-----"
using namespace std;
typedef long long LL;
const LL mod = 1e9;
const int N = 1e6 + 10, M = 1050;
LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }

int n, m;
char mp[M][M];
bool st[M][M];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

bool bfs( int sx , int sy ){
	PII q[N]; int tt=0,hh=0;
	q[hh] = {sx,sy};
	st[sx][sy] = 1;
	while( hh <= tt ){
		PII t = q[hh++];
		rep( i , 0 , 4 ){
			int xt = t.first+dx[i];
			int yt = t.second+dy[i];
			if( xt<0||xt>=n||yt<0||yt>=m ) continue;
			if( st[xt][yt] ) continue;
			if( mp[xt][yt] == 't' ) return true;
			if( mp[xt][yt] != '.' ) continue;
			q[++tt] = {xt,yt};
			st[xt][yt] = 1;
		}
	}
	return false;
}

int main()
{
	IOS;
	int t;
	cin >> t;
	while(t--){
		int x,y;
		memset( st , 0 , sizeof st );
		cin >> n >> m;
		rep( i , 0 , n )
			rep( j , 0 , m ){
				cin >> mp[i][j];
				if (mp[i][j] == 's') x = i , y = j;
			}
		bfs( x , y ) == true ? cout << "YES" << endl : cout << "NO" << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值