单身的1暗恋上了一个女生,于是想给她告白,于是就在房间里用蛋糕堆了一个心的形状。
可是天公不作美,在这个房间的某个角落里藏着一只小老鼠,小老鼠虎视眈眈的看着这些蛋糕,想等1走之后去偷吃蛋糕。
一个房间可以看成n*n的方格。小老鼠可以往上、下、左、右四个方向走。问小老鼠吃到蛋糕最少需要多少步?
Input
本题有多组测试,每组测试第一行输入三个正整数n,x,y。n代表房间的长宽,(x,y)代表老鼠洞的位置(老鼠出现在老鼠洞这个点也算一步)。接下来n行是房间的布置, ’#’代表蛋糕,’.’代表空地。其中(x,y)一定满足位于房间的边上。
(7 ≤n ≤ 99,1 ≤ x,y ≤ n)
Output
对于每组测试数据输出一个整数,占一行。
Sample Input
11 1 8
...........
...........
...#...#...
..###.###..
.#########.
.#########.
..#######..
...#####...
....###....
.....#.....
...........
9 7 9
.........
.........
..##.##..
.#######.
.#######.
..#####..
...###...
....#....
.........
Sample Output
3
4
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int inf=0x3f3f3f;
const int maxn=100+10;
struct st
{
int x,y,d;
};
char mp[maxn][maxn];
int step[4][2]= {1,0,0,1,-1,0,0,-1};
int n;
int vis[maxn][maxn];
int bfs(int x,int y)
{
vis[x][y]=1;
st s;
s.x=x;
s.y=y;
s.d=1;
queue<st>q;
q.push(s);
while(!q.empty())
{
st t=q.front();
q.pop();
if(mp[t.x][t.y]=='#')
return t.d;
for(int k=0; k<4; k++)
{
st next;
next.x=t.x+step[k][0];
next.y=t.y+step[k][1];
next.d=t.d+1;
if(next.x>=0&&next.x<n&&next.y>=0&&next.y<n&&vis[next.x][next.y]==0)
{
q.push(next);
vis[next.x][next.y]=1;
}
}
}
}
int main()
{
int x,y;
while(~scanf("%d%d%d",&n,&x,&y))
{
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
cin>>mp[i][j];
}
memset(vis,0,sizeof(vis));
cout<<bfs(--x,--y)<<endl;
}
return 0;
}