Problem:
Score : 400 points

Problem Statement

A maze is composed of a grid of H \times 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_{ij} is # and a road if S_{ij} 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\times 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 \leq H,W \leq 10^3

1 \leq C_h,D_h \leq H

1 \leq C_w,D_w \leq W

S_{ij} is # or ..

S_{C_h C_w} and S_{D_h D_w} are ..

(C_h,C_w) \neq (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}\ldots S_{1W}
\vdots
S_{H1}\ldots S_{HW}

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

4 4
1 1
4 4
..#.
..#.
.#..
.#..

Sample Output 1

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

4 4
1 4
4 1
.##.
####
####
.##.

Sample Output 2

-1

He cannot move from there.

Sample Input 3

4 4
2 2
3 3
....
....
....
....

Sample Output 3

0

No use of magic is needed.

Sample Input 4

4 5
1 2
2 5
#.###
####.
#..##
#..##

Sample Output 4

2

Solution in C++ (GCC 9.2.1) (judge verdict: Time Limit Exceeded)

#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <tuple>

using namespace std;
// import sys, math, heapq
#define MAX_V 1000
#define MAX_W 1000
#define INF pow(10, 6)

// #define stack < pair<int, int> > Q1
int dist[MAX_V][MAX_W];



void move(int V, int W, vector<string> Maps, int dist[][MAX_W], stack< tuple<int, int, int> > &stack_walk, stack< tuple<int, int, int> > &stack_warp)
{

    vector< pair<int, int> > move_walk= { make_pair(-1, 0), make_pair(1, 0), make_pair(0, -1), make_pair(0, 1) };
    /*
    pair<int, int> p = move_walk[1];
    cout << p.first << " " << p.second << endl;
    */
    while( !stack_walk.empty() )
    {
        tuple<int, int, int> pos = stack_walk.top(); stack_walk.pop();
        int distance = get<0>(pos), x = get<1>(pos), y = get<2>(pos);
        if(distance > dist[y][x]) continue;

        int flag = -1;
        int N_flag = 0;
        for(int i=0; i<4; i++)
        {
            int x_next = x+move_walk[i].first, y_next = y+move_walk[i].second;
            if(x_next < 0 || x_next > W-1 || y_next < 0 || y_next > V-1) continue;
            if(Maps[y_next][x_next] == '#')
            {
                flag = i;
                N_flag ++;
                continue;
            }
            if(dist[y_next][x_next] > dist[y][x])
            {
                dist[y_next][x_next] = dist[y][x];
                stack_walk.push( make_tuple(dist[y_next][x_next], x_next, y_next) );

            }

        }

        if(N_flag > 1)
        {
            int dy_min = max(-y, -2), dy_max = min(V-y, 3);
            int dx_min = max(-x, -2), dx_max = min(W-x, 3);
            for(int dy=dy_min; dy< dy_max; dy++)
            {
                for(int dx=dx_min; dx< dx_max; dx++)
                {
                    int x_next = x+dx, y_next = y+dy;
                    if(Maps[y_next][x_next] == '.' && dist[y_next][x_next] == INF)
                    {
                        dist[y_next][x_next] = dist[y][x]+1;
                        stack_warp.push( make_tuple(dist[y_next][x_next], x_next, y_next) );
                    }

                }
            }

        }
        else if(flag > -1)
        {
            int dy_min = max(-y, -2), dy_max = min(V-y, 3);
            int dx_min = max(-x, -2), dx_max = min(W-x, 3);
            // make_pair(-1, 0), make_pair(1, 0), make_pair(0, -1), make_pair(0, 1)
            if(flag == 3) dy_min = 1;
            else if(flag==2) dy_max = 0;
            else if(flag==1) dx_min = 1;
            else dx_max = 0;
            for(int dy=dy_min; dy< dy_max; dy++)
            {
                for(int dx=dx_min; dx< dx_max; dx++)
                {
                    int x_next = x+dx, y_next = y+dy;
                    if(Maps[y_next][x_next] == '.' && dist[y_next][x_next] == INF)
                    {
                        dist[y_next][x_next] = dist[y][x]+1;
                        stack_warp.push( make_tuple(dist[y_next][x_next], x_next, y_next) );
                    }

                }
            }

        }





    }
    return;
}



int main(void)
{
    // int dist[][MAX_W], stack1 stack_walk, stack1 stack_warp;
    int V1, W1;
    cin >> V1 >> W1; // x, y
    const int V = V1;
    const int W = W1;
    int start[2], goal[2]; // [x, y]
    cin >> start[1] >> start[0] >> goal[1] >> goal[0];

    start[0] --;
    start[1] --;
    goal[0] --;
    goal[1] --;
    // char Maps[V][W];
    // vector<string> Maps;
    vector<string> Maps;
    string str;
    for(int i=0; i<V; i++)
    {
        cin >> str;
        Maps.push_back(str);
        fill_n(dist[i], W, INF );
    }


    stack< tuple<int, int, int> > stack_walk;
    stack< tuple<int, int, int> > stack_warp;

    dist[ start[1] ][start[0]] = 0;

    stack_walk.push( make_tuple(0, start[0], start[1]) );

    while(true)
    {
        move(V, W, Maps, dist, stack_walk, stack_warp);

        if( dist[goal[1]][goal[0]] < INF )
        {
            cout << dist[goal[1]][goal[0]] << endl;
            break;
        }
        else if(stack_warp.empty())
        {
            cout << -1 << endl;
            break;
        }
        swap(stack_walk, stack_warp);

    }



    return 0;
}