题目描述
一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由n * n的格点组成,每个格点只有2种状态, . 和#, 前者表示可以通行后者表示不能通行。同时当Extense处在某个格点时,他只能移动到东南西北(或者说上下左右)四个方向之一的相邻格点上,Extense想要从点A走到点B,问在不走出迷宫的情况下能不能办到。如果起点或者终点有一个不能通行(为#),则看成无法办到。
输入格式
第1行是测试数据的组数k,后面跟着k组输入。每组测试数据的第1行是一个正整数n (1 <= n <= 100),表示迷宫的规模是n * n的。接下来是一个n * n的矩阵,矩阵中的元素为 . 或者 #。再接下来一行是4个整数ha, la, hb, lb,描述A处在第ha行, 第la列,B处在第hb行, 第lb列。注意到ha, la, hb, lb全部是从0开始计数的。
输出格式
k行,每行输出对应一个输入。能办到则输出“YES”,否则输出“NO”。
样例输入
2
3
.##
..#
#..
0 0 2 2
5
.....
###.#
..#..
###..
...#.
0 0 4 0
样例输出
YES
NO
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
const int maxx = 1000050;
int n, m, k, t, now, p;
const double pi = acos(-1.0);
char e[220][220];
int step[220][220];
int ex, ey, sx, sy;
int dx[] = { 0 , 1 , -1 ,0 , 0 };
int dy[] = { 0 , 0 , 0 , 1 , -1};
struct check//用来初始化的结构体
{
int x, y;
check(int xx = 0, int yy = 0)//赋值为0
{
x = xx;
y = yy;
}
};
void bfs()
{
memset(step, 0x3f, sizeof(step));//使用0x3f是为了防止越界
queue<check> q;//设置队列
q.push(check(sx, sy));//将坐标推入队列中
step[sx][sy] = 1;//step记录步数
while (!q.empty())//队列不为空//将这一步所有的情况都遍历
{
check f = q.front();//f为队首
q.pop();//将其推出//其即将被遍历完所有情况所以已经无用了
for (int i = 0; i < 4; i++)//分别进行上下左右的遍历
{
int nx = f.x + dx[i];
int ny = f.y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < n)//判断是否越界
{
if (e[nx][ny] != '#' && step[nx][ny] == 0x3f3f3f3f)//如果这一步能走
{ //并且并没重复走
q.push(check(nx, ny));//将其继续推入队列(相当于下一步,表示还能走
step[nx][ny] = step[f.x][f.y] + 1;//在上一步的基础上+1(步数+1
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> k;
while (k--)
{
cin >> n;
for (int i = 0; i < n; i++)//存入迷宫的地图
{
cin >> e[i];
}
cin >> sx >> sy >> ex >> ey;//输入起点和终点
bfs();//进行搜索
if (step[ex][ey] == 0x3f3f3f3f)//如果终点值依旧为无穷//那就表示并没走到
{
cout << "NO" << endl;
}
else
{
cout << "YES" << endl;
}
}
return 0;
}
pair写法稍微优化
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
ll n, m, k;
int idx;
char e[550][550];
int step[550][550];
int dx[] = { 0 , 0 , 1 , -1 };
int dy[] = { -1 , 1 , 0 , 0 };
int sx, sy, ex, ey;
void bfs()
{
memset(step, 0x3f, sizeof(step));
queue<PII> q;
q.push({ sx , sy });
step[sx][sy] = 1;//初始步数为1
while (!q.empty())
{
PII p = q.front();
q.pop();
for (int i = 0; i <= 3; i++)
{
int nx = p.first + dx[i];
int ny = p.second + dy[i];
if (nx >= 0 && ny >= 0 && nx < n && ny < n)
{
if (e[nx][ny] != '#' && step[nx][ny] == 0x3f3f3f3f)
{
q.push(PII(nx, ny));
step[nx][ny] = step[p.first][p.second] + 1;
}
}
}
}
return;
}
void solve()
{
cin >> n ;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> e[i][j];
}
}
cin >> sx >> sy >> ex >> ey;
bfs();
if (step[ex][ey] == 0x3f3f3f3f)
{
cout << "NO" << endl;
}
else
{
cout << "YES" << endl;
}
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--)
{
solve();
}
return 0;
}