#C5285. Alice's Step Navigation
Alice's Step Navigation
Alice's Step Navigation
Alice wants to reach the end of a sequence of steps. The sequence is represented by an array of integers where a 1 indicates that a step is present and a 0 indicates its absence. Alice starts at position 0. She can move in one of two ways:
- Walk: Move from position p to p+1 if a step exists at p+1.
- Jump: Move from position p to p+2 if steps exist at both positions p+1 and p+2.
Your task is to determine whether Alice can reach the last position of the sequence (position n-1). Note that if the starting position (position 0) does not have a step, then Alice cannot begin her journey.
The conditions for a valid move are given by the following rules in \( \LaTeX \) format:
\( \text{If } A[p] = 1 \text{ and } p+1 < n, \text{ then } A[p+1] \text{ is reachable.} \)
\( \text{If } A[p] = 1, \; A[p+1] = 1 \text{ and } p+2 < n, \text{ then } A[p+2] \text{ is reachable.} \)
inputFormat
The input is read from stdin and has the following format:
T n₁ a₁ a₂ … aₙ₁ n₂ a₁ a₂ … aₙ₂ … nₜ a₁ a₂ … aₙₜ
Where:
- T is the number of test cases.
- For each test case, the first integer n represents the number of positions in the sequence.
- The next n integers represent the state of each position (either 0 or 1).
outputFormat
For each test case, output a single line with YES if Alice can reach the last position (i.e. position n-1), or NO otherwise. The output should be written to stdout.
## sample2
5
1 1 0 1 1
3
1 0 1
NO
NO
</p>