No.2639 Longest Increasing Walk
Problem Statement
There is a grid with $H$ rows and $W$ columns. The cell in the $i$-th row from the top and the $j$-th column from the left is denoted by $(i,j)$. The value $A_{i,j}$ is written in cell $(i,j)$.
A sequence of cells $((i_1,j_1),(i_2,j_2),\dots ,(i_L,j_L))$ of length $L$ is called an Increasing Walk if all of the following conditions hold.
- For $k=1,2,\dots ,L-1$, cells $(i_k,j_k),(i_{k+1},j_{k+1})$ are adjacent by an edge. That is, $\lvert i_k-i_{k+1}\rvert + \lvert j_k-j_{k+1}\rvert =1$ holds.
- For $k=1,2,\dots ,L-1$, the value written in cell $(i_{k+1},j_{k+1})$ is strictly greater than the value written in cell $(i_k,j_k)$. That is, $A_{i_k,j_k}\lt A_{i_{k+1},j_{k+1}}$ holds.
Find the maximum possible length of an Increasing Walk. (It can be shown that the maximum exists.)
Constraints
- All input values are integers.
- $1\le H,W\le 500$
- $1\le A_{i,j}\le 10^9\ (1\le i\le H, 1\le j\le W)$
Input
$H$ $W$
$A_{1,1}$ $A_{1,2}$ $\dots$ $A_{1,W}$
$\vdots$
$A_{H,1}$ $A_{H,2}$ $\dots$ $A_{H,W}$
Output
Output the maximum possible length of an Increasing Walk.
Sample
Sample 1
Input
2 3 1 2 4 3 3 4
Output
4
The sequence of cells $((1,1),(1,2),(2,2),(2,3))$ is an Increasing Walk of length $4$.
Sample 2
Input
2 2 1 1 1 1
Output
1
Sample 3
Input
4 4 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3
Output
4
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
noya2