Problem Statement
A maze is composed of a grid of
H
×
W
squares -
H
vertical,
W
horizontal.
The square at the
i
-th row from the top and the
j
-th column from the left -
(
i
,
j
)
- is a wall if
S
i
j
is # and a road if
S
i
j
is …
There is a magician in
(
C
h
,
C
w
)
. He can do the following two kinds of moves:
Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.
Move B: Use magic to warp himself to a road square in the
5
×
5
area centered at the square he is currently in.
In either case, he cannot go out of the maze.
At least how many times does he need to use the magic to reach
(
D
h
,
D
w
)
?
Constraints
1
≤
H
,
W
≤
10
3
1
≤
C
h
,
D
h
≤
H
1
≤
C
w
,
D
w
≤
W
S
i
j
is # or …
S
C
h
C
w
and
S
D
h
D
w
are …
(
C
h
,
C
w
)
≠
(
D
h
,
D
w
)
Input
Input is given from Standard Input in the following format:
H
W
C
h
C
w
D
h
D
w
S
11
…
S
1
W
⋮
S
H
1
…
S
H
W
Output
Print the minimum number of times the magician needs to use the magic. If he cannot reach
(
D
h
,
D
w
)
, print -1 instead.
Sample Input 1
Copy
4 4
1 1
4 4
…#.
…#.
.#…
.#…
Sample Output 1
Copy
1
For example, by walking to
(
2
,
2
)
and then using the magic to travel to
(
4
,
4
)
, just one use of magic is enough.
Note that he cannot walk diagonally.
Sample Input 2
Copy
4 4
1 4
4 1
.##.
.##.
Sample Output 2
Copy
-1
He cannot move from there.
Sample Input 3
Copy
4 4
2 2
3 3
…
…
…
…
Sample Output 3
Copy
0
No use of magic is needed.
Sample Input 4
Copy
4 5
1 2
2 5
#.###
####.
#…##
#…##
Sample Output 4
Copy
2
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int n,m,xs,ys,xt,yt,ans[10005][10005];
int yi[10]={0,0,0,1,-1};
int xi[10]={0,1,-1,0,0};
int ym[30]={-2,-2,-2,-2,-2,-1,0,1,2,2,2,2,2,1,0,-1,1,-1,-1,1};
int xm[30]={-2,-1,0,1,2,2,2,2,2,1,0,-1,-2,-2,-2,-2,1,1,-1,-1};
char a[1005][1005];
struct node{int x,y,w;node(){}node(int X,int Y,int W){x=X;y=Y;w=W;}
bool operator<(const node o)const{return w>o.w;}};
inline bool chcek(int xx,int yy,int ww)
{
if(a[xx][yy]=='#'||xx<1||xx>n||yy<1||yy>m||ans[xx][yy]<=ww)
return 0;
return 1;
}
inline void bfs()
{
priority_queue<node> q;
node now;
q.push(node(xs,ys,0));
while(q.size())
{
now=q.top();
q.pop();
if(ans[now.x][now.y]<now.w)
continue;
ans[now.x][now.y]=now.w;
for(int i=1;i<=4;i++)
if(chcek(now.x+xi[i],now.y+yi[i],now.w))
q.push(node(now.x+xi[i],now.y+yi[i],now.w));
for(int i=1;i<=20;i++)
if(chcek(now.x+xm[i],now.y+ym[i],now.w+1))
q.push(node(now.x+xm[i],now.y+ym[i],now.w+1));
}
}
inline int read()
{
int x=0;char ch=getchar();
while(ch<'0'||ch>'9')
ch=getchar();
while(ch>='0'&&ch<='9')
{
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x;
}
inline char read1()
{
char x=getchar();
while(x!='.'&&x!='#')
x=getchar();
return x;
}
int main()
{
n=read();m=read();
xs=read();ys=read();
xt=read();yt=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
a[i][j]=read1();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
ans[i][j]=0x3f3f3f3f;
ans[xs][ys]=0;
bfs();
if(ans[xt][yt]==0x3f3f3f3f)
printf("-1");
else
printf("%d",ans[xt][yt]);
}