2018年东北农业大学春季校赛 D - why的迷宫

链接: https://www.nowcoder.com/acm/contest/93/D
来源:牛客网

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

题目描述

给你一个n*m的迷宫,这个迷宫中有以下几个标识:

s代表起点

t代表终点

x代表障碍物

.代表空地

现在你们涵哥想知道能不能从起点走到终点不碰到障碍物(只能上下左右进行移动,并且不能移动到已经移动过的点)。

输入描述:

输入第一行一个整数T(1<=T<=10)
接下来有T组测试数据,对于每一组测试数据,第一行输入2个数n和m(1<=n,m<=500)
接下来n行,每行m个字符代表这个迷宫,每个字符都是上面4个中的一种
数据保证只有一个起点和一个终点

输出描述:

对于每一组测试数据,如果可以的话输出YES,不可以的话输出NO

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>

using namespace std;
struct zuobiao{
    int x,y;
}now,fresh;

char a[501][501];
int vis[501][501];
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
int n,m,sx,sy,tx,ty;


void bfs(int x,int y){
    memset(vis,0,sizeof(vis));
    queue<zuobiao> s;
    now.x=x;
    now.y=y;
    vis[x][y]=1;
    s.push(now);
    while(!s.empty()){
        now=s.front();
        s.pop();
        for(int i=0;i<4;i++){
            fresh.x=now.x+fx[i];
            fresh.y=now.y+fy[i];
            while(true){
                if(fresh.x>=0&&fresh.x<n&&fresh.y>=0&&fresh.y<m&&a[fresh.x][fresh.y]!='x'){
                    if(fresh.x==tx&&fresh.y==ty){
                        printf("YES\n");
                        return ;
                    }
                    if(vis[fresh.x][fresh.y]==0){
                        vis[fresh.x][fresh.y]=1;
                        s.push(fresh);
                    }
                    fresh.x+=fx[i];
                    fresh.y+=fy[i];
                }
                else break;
            }
        }
    }
    printf("NO\n");
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        memset(a,0,sizeof(a));
        scanf("%d%d",&n,&m);
        getchar();
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                scanf("%c",&a[i][j]);
                if(a[i][j]=='s'){sx=i;sy=j;}
                if(a[i][j]=='t'){tx=i;ty=j;}
            }
            getchar();
        }

        bfs(sx,sy);
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值