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