13219 - BFS - Practice   

Description

There is a grid. Each cell is either an empty cell or a wall. You are on in the beginning and want to go to .

It's guaranteed that  and  are empty cells.

You could move to an adjacent empty cell (i.e an empty cell which shares a common side with the cell which you are on).

Find the minimum number of cells you need to pass by to reach . There may be no such a path for you to reach .

Input

The first line contains two positive integers  and   the size of the grid.

Then  lines folllow. The -th line contains  characters. The -th characters in the -th line  may be 0 or 1. The cell  is an empty cell if  is equal to 0. Otherwise it's a wall.

The next line contains two integers  and   the initial cell you are on.

The final line contains two integers  and   the cell you want to go.

Note that  and  must be empty cells. Also they are different(i.e  or ).

 

It's guaranteed that:

  • The 1st testcase must be identical to the Sample #1 below
  • For the first 5 testcases: 
  • For the first 7 testcases: 

Output

Output the minimum cells you must pass by for you to reach . Output -1 instead if there is no such a path.

Remember to print a newline('\n') character at the end of the line.

 

Note: there are two sample below. "# Sample Input 1/2" and "# Sample Output 1/2" are not the part of input and output.

They are just for marking that the following content corresponds to which sample.

Sample Input  Download

Sample Output  Download

Tags




Discuss