问题 B: 迷宫
时间限制: 1 Sec 内存限制: 128 MB提交: 21 解决: 18
[ 提交][ 状态][ 讨论版][命题人: quanxing]
题目描述
输入
输出
k行,每行输出对应一个输入。能办到则输出“YES”,否则输出“NO”。
样例输入
2 3 .## ..# #.. 0 0 2 2 5 ..... ###.# ..#.. ###.. ...#. 0 0 4 0
样例输出
YES
NO
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
char maze[102][102];
int x1,y1,x2,y2;
int d[4][4]={{-1,0},{1,0},{0,-1},{0,1}};
int m;
struct node
{
int x;
int y;
};
queue <node> Q;
int main()
{
int n;
cin>>n;
for(int k=1;k<=n;k++)
{
cin>>m;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
cin>>maze[i][j];
}
}
int a[102][102]={0};
cin>>x1>>y1>>x2>>y2;
//当时没有排除这个因数;报错
if(m==1&&maze[0][0]=='#')
{
cout<<"NO"<<endl;
continue;
}
a[x1][y1]=1;
node s,s1;
s.x=x1;
s.y=y1;
s1.x=x1;
s1.y=y1;
Q.push(s);
int P=0;
while(Q.size())
{
if(s1.x==x2&&s1.y==y2)
{
P=1;
break;
}
s=Q.front();
Q.pop();
for(int i=0;i<4;i++)
{
s1.x=s.x+d[i][0];
s1.y=s.y+d[i][1];
if((s1.x>=0&&s1.x<m)&&(s1.y>=0&&s1.y<m)&&(a[s1.x][s1.y]==0))
{
a[s1.x][s1.y]=1;
Q.push(s1);
}
//Q.push(s);
}
}
if(P==1)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
//int p=bfs(x1,y1);
}
}