Welcome to ZOJ
 Problem Sets Information Select Problem Runs Ranklist
ZOJ Problem Set - 4020
Traffic Light

Time Limit: 1 Second      Memory Limit: 131072 KB

DreamGrid City is a city with $n \times m$ intersections arranged into a grid of $n$ rows and $m$ columns. The intersection on the $i$-th row and the $j$-th column can be described as $(i, j)$, and two intersections $(i_1, j_1)$ and $(i_2, j_2)$ are connected by a road if $|i_1-i_2| + |j_1-j_2| = 1$.

At each intersection stands a traffic light. A traffic light can only be in one of the two states: 0 and 1. If the traffic light at the intersection $(i, j)$ is in state 0, one can only move from $(i, j)$ to $(i+1, j)$ or $(i-1, j)$; If the traffic light is in state 1, one can only move from $(i, j)$ to $(i, j+1)$ or $(i, j-1)$ (of course, the destination must be another intersection in the city).

BaoBao lives at the intersection $(s_i, s_j)$, and he wants to visit his best friend DreamGrid living at the intersection $(f_i, f_j)$. After his departure, in each minute the following things will happen in order:

• BaoBao moves from his current intersection to another neighboring intersection along a road. As a law-abiding citizen, BaoBao has to obey the traffic light rules when moving.
• Every traffic light changes its state. If a traffic light is in state 0, it will switch to state 1; If a traffic light is in state 1, it will switch to state 0.

As an energetic young man, BaoBao doesn't want to wait for the traffic lights, and he must move in each minute until he arrives at DreamGrid's house. Please tell BaoBao the shortest possible time he can move from $(s_i, s_j)$ to $(f_i, f_j)$ to meet his friend, or tell him that this is impossible.

#### Input

There are multiple test cases. The first line of the input contains an integer $T$, indicating the number of test cases. For each test case:

The first line contains two integers $n$ and $m$ ($1 \le n \times m \le 10^5$), indicating the size of the city.

For the following $n$ lines, the $i$-th line contains $m$ integers $t_{i,1}, t_{i,2}, \dots, t_{i,m}$ ($0 \le t_{i,j} \le 1$), where $t_{i,j}$ indicates the initial state of the traffic light at intersection $(i, j)$.

The next line contains four integers $s_i$, $s_j$, $f_i$ and $f_j$ ($1 \le s_i, f_i \le n$, $1 \le s_j, f_j \le m$), indicating the starting intersection and the destination intersection.

It's guaranteed that the sum of $n \times m$ over all test cases will not exceed $3 \times 10^5$.

#### Output

For each test case output one line containing one integer, indicating the shortest possible time (in minute) BaoBao can move from $(s_i, s_j)$ to $(f_i, f_j)$ without stopping. If it is impossible for BaoBao to arrive at DreamGrid's house, print "-1" (without quotes) instead.

#### Sample Input

4
2 3
1 1 0
0 1 0
1 3 2 1
2 3
1 0 0
1 1 0
1 3 1 2
2 2
1 0
1 0
1 1 2 2
1 2
0 1
1 1 1 1


#### Sample Output

3
5
-1
0


#### Hint

For the first sample test case, BaoBao can follow this path: $(1, 3) \to (2, 3) \to (2, 2) \to (2, 1)$.

For the second sample test case, due to the traffic light rules, BaoBao can't go from $(1, 3)$ to $(1, 2)$ directly. Instead, he should follow this path: $(1, 3) \to (2, 3) \to (2, 2) \to (2, 1) \to (1, 1) \to (1, 2)$.

For the third sample test case, it's easy to discover that BaoBao can only go back and forth between $(1, 1)$ and $(1, 2)$.

Author: WENG, Caizhi
Source: The 18th Zhejiang University Programming Contest Sponsored by TuSimple
Submit    Status