luogu#P1746. 离开中山路

离开中山路

Background

The final chapter of “A Story of Love and Sorrow III · shopping”.

Problem Description

After shopping, Aiyuchou plans to take a bus to leave Zhongshan Road. He is at x1,y1x_1,y_1, and the station is at x2,y2x_2,y_2. You are given an n×n(n1000)n \times n(n \le 1000) map, where 00 represents road and 11 represents shop (cannot pass through shops). Aiyuchou can move only vertically or horizontally along roads. To save time, he wants the shortest distance to the destination (the distance between two adjacent coordinates is 11). Can you help him?

Input Format

Line 11 contains an integer nn.

Lines 22 to n+1n+1: the entire map description (00 represents road, 11 represents shop; note there are no spaces between numbers).

Line n+2n+2: four integers x1,y1,x2,y2x_1,y_1,x_2,y_2.

Output Format

Only 11 line, the shortest distance to the destination.

3
001
101
100
1 1 3 3
4

Hint

For 20%20\% of the testdata, 1n1001\leq n \le 100.

For 100%100\% of the testdata, 1n10001\leq n \le 1000.

Translated by ChatGPT 5