DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Shinomiya Wants to Walk Together

Back to problem

Editorial

Since upward moves are forbidden, once a path leaves a row for the next row, it never returns. Reversing its horizontal direction within a row would immediately revisit the preceding cell. Thus, all horizontal moves within one row must have the same direction.

Remembering only the last direction is insufficient. When the last move was down, the direction before it determines which horizontal move is allowed next. Use the following five states:

  • RRR: the last move was right.
  • LLL: the last move was left.
  • DDD: the last move was down, and it was not immediately preceded by a horizontal move.
  • DRD_RDR​: the last two moves were right, then down.
  • DLD_LDL​: the last two moves were left, then down.

Also treat the starting cell, before any move, as state DDD. A downward first move likewise leads to state DDD.

Let dp[i][j][s]\mathrm{dp}[i][j][s]dp[i][j][s] be the maximum distance of a valid path reaching cell (i,j)(i,j)(i,j) in state sss. Use −∞-\infty−∞ for unreachable states. Initially, , and every other state is unreachable. States at walls and outside the grid are also unreachable. Never create a new path by adding to an unreachable value.

First consider moves down from the cell above. After a horizontal move, remember its direction for one more step. After another downward move, this restriction on the horizontal direction disappears:

dp[i][j][D]=1+max⁡s∈{D,DR,DL}dp[i−1][j][s],\mathrm{dp}[i][j][D]=1+\max_{s\in\{D,D_R,D_L\}}\mathrm{dp}[i-1][j][s],dp[i][j][D]=1+ dp[i][j][DR]=1+dp[i−1][j][R],dp[i][j][DL]=1+dp[i−1][j][L].\mathrm{dp}[i][j][D_R]=1+\mathrm{dp}[i-1][j][R],\qquad \mathrm{dp}[i][j][D_L]=1+\mathrm{dp}[i-1][j][L].dp[i][j][D

Apply these equations to passable cells with i≥2i\geq2i≥2, keeping the starting cell's initial value.

A move right may follow only states RRR, DDD, and DRD_RDR​. Reversing direction from LLL would revisit a cell, while moving right from DLD_L would form a forbidden sequence of three moves. Hence,

dp[i][j][R]=1+max⁡s∈{R,D,DR}dp[i][j−1][s].\mathrm{dp}[i][j][R]=1+\max_{s\in\{R,D,D_R\}}\mathrm{dp}[i][j-1][s].dp[i][j][R]=1+

Symmetrically, a move left is given by

dp[i][j][L]=1+max⁡s∈{L,D,DL}dp[i][j+1][s].\mathrm{dp}[i][j][L]=1+\max_{s\in\{L,D,D_L\}}\mathrm{dp}[i][j+1][s].dp[i][j][L]=1+

For each row, compute all three states for downward arrivals first. Then scan from left to right to compute the RRR states, and from right to left to compute the LLL states. There are no transitions between the two horizontal states, so this order evaluates every recurrence correctly. Initialize all states of each new row as unreachable, and never update a state at a wall.

Output the maximum of the five states at the destination. If all are unreachable, output −1-1−1. The states already store distances, so no conversion from the number of visited cells is needed.

There are only a constant number of states per cell, giving a time complexity of O(N2)O(N^2)O(N2). Keeping only the preceding and current rows of states and reading the grid one row at a time gives a space complexity of O(N)O(N)O(N).

Proof

A valid path never moves up, so it enters each row at most once. If it moves in both horizontal directions within a row, its first reversal immediately returns to the preceding cell. Therefore, horizontal moves within a row have only one direction. Moving down enters an unvisited row, and continuing in one horizontal direction enters an unvisited cell. The recurrences never transition from the opposite horizontal state in the same row, so they cannot cause a repeated visit.

Before a move right, exactly the states RRR, DDD, and DRD_RDR​ are allowed. State LL would cause a repeated visit, and would produce the forbidden sequence left, down, right. Symmetrically, a move left is allowed from exactly , , and . A downward move never violates the direction restriction. It leads from to , from to , and from any other state to . Consequently, every transition in the recurrences is valid, and the last move of every valid path is represented by one of these transitions.

Solution written by GPT6

dp[1][1][D]=0\mathrm{dp}[1][1][D]=0dp[1][1][D]=0
111
s∈{D,DR​,DL​}
max
​
dp
[
i
−
1][j][s],
R
​
]
=
1+
dp[i−
1][j][R],dp[i][j][DL​]=
1+
dp[i−
1][j][L].
DL​
s
∈
{
R
,
D
,
DR​
}
max
​
dp
[
i
]
[
j
−
1][s].
s
∈
{
L
,
D
,
DL​
}
max
​
dp
[
i
]
[
j
+
1][s].
L
DLD_LDL​
LLL
DDD
DLD_LDL​
RRR
DRD_RDR​
LLL
DLD_LDL​
DDD

Downward states in a row depend only on the preceding row. An RRR state depends only on states to its left, while an LLL state depends only on states to its right. Thus, the stated evaluation order always computes predecessor states first. The initial value is correct. Taking the largest valid predecessor distance and adding the distance of one move preserves each state's definition. By induction, every state stores the maximum distance of a valid path ending in that state. Taking the maximum at the destination gives the answer; if every state is unreachable, no valid path exists.