#D571. Stray Twins
Stray Twins
Stray Twins
Takayuki and Kazuyuki are good twins, but their behavior is exactly the opposite. For example, if Takayuki goes west, Kazuyuki goes east, and if Kazuyuki goes north, Takayuki goes south. Currently the two are in a department store and are in different locations. How can two people who move in the opposite direction meet as soon as possible?
A department store is represented by a grid consisting of W horizontal x H vertical squares, and two people can move one square from north, south, east, and west per unit time. However, you cannot move outside the grid or to a square with obstacles.
As shown, the position of the grid cells is represented by the coordinates (x, y).
Create a program that inputs the grid information and the initial positions of the two people and outputs the shortest time until the two people meet. If you can't meet, or if it takes more than 100 hours to meet, print NA. The grid information is given by the numbers in rows H and columns W, and the location information of the two people is given by the coordinates.
If either Takayuki-kun or Kazuyuki-kun is out of the range of the obstacle or grid after moving, you cannot move, so the one who is out of the range of the obstacle or grid is the original place. But if you don't, you can move without returning to the original place.
When two people meet, it means that they stay in the same square after moving. Even if two people pass each other, they do not meet.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
W H tx ty kx ky d11 d21 ... dW1 d12 d22 ... dW2 :: d1H d2H ... dWH
The first line gives the department store sizes W, H (1 ≤ W, H ≤ 50). The second line gives Takayuki's initial position tx, ty, and the third line gives Kazuyuki's initial position kx, ky.
The H line that follows gives the department store information. di, j represents the type of square (i, j), 0 for movable squares and 1 for obstacles.
The number of datasets does not exceed 100.
Output
Outputs the shortest time on one line for each input dataset.
Example
Input
6 6 2 4 6 2 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 3 3 1 1 3 3 0 0 0 0 1 0 0 0 0 0 0
Output
3 NA
inputFormat
inputs the grid information and the initial positions of the two people and
outputFormat
outputs the shortest time until the two people meet. If you can't meet, or if it takes more than 100 hours to meet, print NA. The grid information is given by the numbers in rows H and columns W, and the location information of the two people is given by the coordinates.
If either Takayuki-kun or Kazuyuki-kun is out of the range of the obstacle or grid after moving, you cannot move, so the one who is out of the range of the obstacle or grid is the original place. But if you don't, you can move without returning to the original place.
When two people meet, it means that they stay in the same square after moving. Even if two people pass each other, they do not meet.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
W H tx ty kx ky d11 d21 ... dW1 d12 d22 ... dW2 :: d1H d2H ... dWH
The first line gives the department store sizes W, H (1 ≤ W, H ≤ 50). The second line gives Takayuki's initial position tx, ty, and the third line gives Kazuyuki's initial position kx, ky.
The H line that follows gives the department store information. di, j represents the type of square (i, j), 0 for movable squares and 1 for obstacles.
The number of datasets does not exceed 100.
Output
Outputs the shortest time on one line for each input dataset.
Example
Input
6 6 2 4 6 2 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 3 3 1 1 3 3 0 0 0 0 1 0 0 0 0 0 0
Output
3 NA
样例
6 6
2 4
6 2
0 0 0 0 1 0
0 1 0 0 0 0
0 1 0 0 0 0
0 0 0 1 0 0
0 0 0 0 0 1
0 0 0 0 0 0
3 3
1 1
3 3
0 0 0
0 1 0
0 0 0
0 0
3
NA
</p>