#P10988. [蓝桥杯 2023 国 Python A] 走方格

    ID: 12393 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2023广度优先搜索 BFS蓝桥杯国赛

[蓝桥杯 2023 国 Python A] 走方格

Problem Description

Given an NN by NN grid. The cell in row ii and column jj has coordinates (i,j)(i, j) and height Hi,jH_{i,j}. Xiao Lan starts from the top-left corner at (0,0)(0, 0), and the destination is the bottom-right corner at (N1,N1)(N - 1, N - 1).

When Xiao Lan is at row rr and column cc, he can move in the following ways:

  1. If r+1<Nr + 1 < N, he can move to (r+1,c)(r + 1, c), costing 11 second.
  2. If c+1<Nc + 1 < N, he can move to (r,c+1)(r, c + 1), costing 11 second.
  3. For any integer LL, if Hr,c>Hr,c+1>>Hr,c+LH_{r,c} > H_{r,c+1} > \cdots > H_{r,c+L}, he can move to (r,c+L)(r, c + L), costing 11 second.
  4. For any integer LL, if Hr,c>Hr,c1>>Hr,cLH_{r,c} > H_{r,c-1} > \cdots > H_{r,c-L}, he can move to (r,cL)(r, c - L), costing 11 second.

Now the grid is given. How many seconds at minimum does Xiao Lan need to move from (0,0)(0, 0) to (N1,N1)(N - 1, N - 1)?

Input Format

The first line contains an integer NN, indicating the grid size.
The next NN lines each contain NN integers, representing the number in each cell.

Output Format

Output one integer representing the answer.

4
0 1 9 3
2 9 3 7
8 4 8 9
9 8 0 7

5

Hint

For 20%20\% of the test cases, 1N101 \le N \le 10.

For 50%50\% of the test cases, 1N1001 \le N \le 100.

For all test cases, 1N1000,0Hi,j1001 \le N \le 1000, 0 \le H_{i, j} \le 100.

Sample Explanation

The moving order is: $(0, 0)\rightarrow (1, 0)\rightarrow(2, 0)\rightarrow(3, 0)\rightarrow(3, 2)\rightarrow(3, 3)$. The numbers at coordinates (3,0),(3,1),(3,2)(3, 0),(3, 1),(3, 2) are 9>8>09 > 8 > 0, so he can spend 11 second to move from (3,0)(3, 0) to (3,2)(3, 2).

Translated by ChatGPT 5