链接: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;
}