1112.迷宫

一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由 n∗nn∗n 的格点组成,每个格点只有2种状态,.#,前者表示可以通行后者表示不能通行。

同时当Extense处在某个格点时,他只能移动到东南西北(或者说上下左右)四个方向之一的相邻格点上,Extense想要从点A走到点B,问在不走出迷宫的情况下能不能办到。

如果起点或者终点有一个不能通行(为#),则看成无法办到。

注意:A、B不一定是两个不同的点。

输入格式

第1行是测试数据的组数 kk,后面跟着 kk 组输入。

每组测试数据的第1行是一个正整数 nn,表示迷宫的规模是 n∗nn∗n 的。

接下来是一个 n∗nn∗n 的矩阵,矩阵中的元素为.或者#

再接下来一行是 4 个整数 ha,la,hb,lbha,la,hb,lb,描述 AA 处在第 haha 行, 第 lala 列,BB 处在第 hbhb 行, 第 lblb 列。

注意到 ha,la,hb,lbha,la,hb,lb 全部是从 0 开始计数的。

输出格式

k行,每行输出对应一个输入。

能办到则输出“YES”,否则输出“NO”。

数据范围

1≤n≤1001≤n≤100

输入样例:

2
3
.##
..#
#..
0 0 2 2
5
.....
###.#
..#..
###..
...#.
0 0 4 0

输出样例:

YES
NO
#include <cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int N=105;
const int dx[]={0,0,-1,1};
const int dy[]={1,-1,0,0};
char ch[N][N];
bool pd[N][N];
int k,n,ha,la,hb,lb,cnt;
bool dfs(int &x,int &y){
    if(x==hb&&y==lb)return true;
    pd[x][y]=true;
    for(int i=0;i<4;i++){
        int xi=x+dx[i],yi=y+dy[i];
        if(xi<0||xi>=n||yi<0||yi>=n)continue;
        if(pd[xi][yi])continue;
        if(ch[xi][yi]=='#')continue;
        if(dfs(xi,yi))return true;
    }
    return false;
}
int main(){
    cin>>k;
    while(k--){
        cin>>n;
        memset(pd,false,sizeof(pd));
        for(int i=0;i<n;i++) cin>>ch[i];
        cin>>ha>>la>>hb>>lb;
        if(ch[ha][la]=='#'||ch[hb][lb]=='#'){
            puts("NO");continue;
        }else{
            if(dfs(ha,la))puts("YES");
            else puts("NO");
        }
    }
}
发布了32 篇原创文章 · 获赞 7 · 访问量 794

猜你喜欢

转载自blog.csdn.net/Young_Naive/article/details/104172605
今日推荐