目录

USACO 2017 Jan Gold P3

USACO analysis

Mark Chen | 25 Nov 2020

Problem 3. Cow Navigation

Link to Problem

Problem Summary

Bessie is in the barn of John. The barn has a size of $N\times N$, and some of the square cells are impassable. Bessie starts in the lower-left corner (cell 1, 1) and wants to move to the right corner (cell N, N).

In each second, Bessie can either go forward, turn left, or turn right. If one instruction let it enter an impassable square, it will skip through that instruction. At the beginning, Bessie doesn’t know if she starts out facing up or facing left. You need to give the shortest sequence of directions that will guide her to the goal regardless of which case is true. Once she reaches the goal, she will ignore further commands.

Proposed Solution

The difficult point in this problem is that we don’t know whether Bessie starts with which position and we have to make sure it can arrive at the destination. Since in each situation, the series of instructions used are the same, we can apply BFS on each situation simultaneously.

In this BFS, each state will have two “sub-state”, which represent the position and direction of Bessie when start pointing upward and pointing rightward. We can also apply dynamic programming on this problem - if a set of instruction can reach the same state with shorter length, we should use the shorter instruction series.

We will construct a table with size $N\times N \times 4 \times N \times N \times 4$. The first part $N\times N \times 4$ is the DP table for first sub-state. The second part of table is the DP-table for second sub-state.

The update of DP table and state transition of BFS will follow these rules:

  1. \[T[S_1, S_2] = \min{(T[S_1, S_2], T[S_1', S_2'] + 1)}\]
  2. \[Update(S_1, S_2) = S_1, S_2' \text{ if $S_1$ is at final state}\]
  3. \[Update(S_1, S_2) = S_1',S_2 \text{ if the update will let $S_2$ get into impassable square}\]

Time Complexity Analysis

Therefore, we will search through a graph with $O(N^4)$ nodes. (For each sub-state, there are $N^2$ nodes, though for most of the time, the sub-states has same position, the overall upper bound is $O(N^4)$). Since $0\leq N\leq 20$, the proposed solution will be fast enough.


评论区