迷宫

链接:https://ac.nowcoder.com/acm/contest/635/C
来源:牛客网
 

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

题目描述

给出一个n*m的迷宫,其中标记为1的为障碍,标记为0的为可以通行的地方。

迷宫的入口为左上角,出口为右下角,只能从一个位置走到这个它的上、下、左、右四个方向之一。

如果能顺利走出迷宫就输出YES,否则输出NO

输入描述:

输入一个n行m列的迷宫(0<n<=100,0<m<=100)

输出描述:

输出YES或NO

示例1

输入

复制

2 2
0 1
1 0

输出

复制

NO

示例2

输入

复制

4 6
0 1 0 0 0 0
0 0 0 1 0 0
0 0 1 0 0 1
1 1 0 0 0 0

输出

复制

YES
#include <iostream>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <queue>

using namespace std; 

typedef long long ll;

int n,m;

const int MAX = 105;

int map[MAX][MAX];
bool vis[MAX][MAX];

int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

struct node{
	
	int x;
	int y;
};

bool bfs(){
	
	queue<node> q;
	node node1;
	node1.x = 1;
	node1.y = 1;
	
	q.push(node1);
	
	vis[1][1] = true;
	
	while(!q.empty()){
		
		node temp = q.front();
		
		q.pop();
		
		if(temp.x == n && temp.y == m){
			
			return true;
		}
		
		for(int i=0;i<4;i++){
			
			int xx = temp.x + dx[i];
			int yy = temp.y + dy[i];
			
			if(xx>=1 && xx<=n && yy>=1 && yy<=m && !vis[xx][yy] && map[xx][yy]==0){
				
				node t;
				t.x = xx;
				t.y = yy;
				q.push(t);
				vis[xx][yy] = true;
			}
		}
	}
	
	return false;
}

int main(){
	
	ios::sync_with_stdio(false);
		
	while(cin>>n>>m){
		
		memset(vis,false,sizeof(vis));
		
		for(int i=1;i<=n;i++){
			
			for(int j=1;j<=m;j++){
				
				cin>>map[i][j];
			}
		}
		
		if(bfs()){
			
			cout<<"YES"<<endl;
		}
		else{
			
			cout<<"NO"<<endl;
		}
	}
	
	
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值