#P10923. Happybob's Pencil Case (UBC001C)

Happybob's Pencil Case (UBC001C)

Background

Note: There are no typos in this problem.

Problem Description

Suppose the classroom is an n×nn \times n 01 matrix, where 0 means empty and 1 means there is a desk (an obstacle).

Now Cuazyoxi is holding Happybob's Pencil Case at (x1,y1)(x_1, y_1), and Happybob is at (x2,y2)(x_2, y_2).

We define one move as moving from the current cell to one of the four adjacent cells (up, down, left, or right). Formally, suppose a person (Cuazyoxi or Happybob) is currently at (x,y)(x, y). If one of (x+1,y)(x + 1, y), (x,y+1)(x, y + 1), (x1,y)(x - 1, y), (x,y1)(x, y - 1) exists and is not an obstacle, then he may move to that cell.

Every second, Cuazyoxi moves once or stays still first, and then Happybob moves once, twice, or stays still. After each move, both of them know the other person's position.

Both sides are very smart. Cuazyoxi wants to delay being caught by Happybob for as long as possible, while Happybob wants to catch Cuazyoxi as early as possible.

Question: How many more seconds can Cuazyoxi evade Happybob (i.e., at least how many seconds does Happybob need before he can reach the same cell as Cuazyoxi)?

Input Format

The first line contains a positive integer nn, which is the size of the classroom.

The second line contains four positive integers x1,y1,x2,y2x_1, y_1, x_2, y_2, representing the starting positions of Cuazyoxi and Happybob.

The next nn lines each contain a string SS of length nn. These nn strings form a 01 matrix.

Output Format

If Cuazyoxi will eventually be caught, output the time when he is caught. Otherwise, output inf.

3
1 1 3 3
011
011
000
2
3
1 1 3 3
011
111
110
inf

Hint

Sample Explanation

For sample 11, Cuazyoxi's optimal strategy is clearly to stay in place. After 22 seconds, Happybob can catch him.

For sample 22, no matter what happens, Happybob cannot catch Cuazyoxi.

Constraints

2n102 \le n \le 10 and 1x1,x2,y1,y2n1 \le x_1, x_2, y_1, y_2 \le n. It is guaranteed that the numbers at (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) are 0, and either x1x2x_1 \ne x_2 or y1y2y_1 \ne y_2. It is guaranteed that each of the nn input strings has length nn and contains only characters 0 and 1 (it may contain only 1 or only 0).

Translated by ChatGPT 5