目录

USACO 2017 Feb Gold P1

USACO analysis

Mark Chen | 27 Nov 2020

Problem 1. Why did the Cow Cross the Road

Problem Description

Bessie the cow wants to move from the upper-left corner of field to the bottom-right corner of field. Each time it goes from one grid to the other, $T$ unit of time will be consumed. Each time Bessie pass through 3 grids, she will stop at the grid and begin eating. The time of eating in each grid is different and will be provided in the input.

$3 \leq N \leq 100, 0\leq T\leq 1\times 10^6$

Proposed Solution

The first thought on this problem is to solve by using Unified Cost Search (UCS). By maintain a fringe of Priority Queue that is sorted according to the time consumes to arrive at a specific position, it is promised that the first state we have met that arrived at the destination will be the state that consumes LEAST time to arrive at the destination.

Therefore, we can represent a State $S$ in this form

State newState = new State(Time, num, x, y);

And accordingly, the state transition function will be somehow like this

public ArrayList<State> StateTransition(State currState){
    int currTime = currState.getTime();
    int num = currState.getNum() + 1;
    int currX = currState.getX();
    int currY = currState.gety();
    
    ArrayList<State> nextStates = new ArrayList<>();
    
    Move[] validMove = this.getValidMove(x, y);
    for (Move move : validMove){
        int nextTime = currTime;
        int[] change = move.getChange();
        nextX = currX + change[0];
        nextY = currY + change[1];
        if (num % 3 == 0){ nextTime += this.Time[nextX][nextY] }
        nextStates.add(new State(nextTime, num, nextX, nextY));
    }
}

Time Complexity Analysis

Since the time that UCS iterate is not bounded explicitly and there does not has an explicit relationship between data scale and number of iteration, it is hard to calculate accurate time complexity. Below, we will try to estimate an upper bound.

First, there are $N^2$ vertexes in the graph, suppose each node is explored for $N$ time (which is an over-estimation), the time complexity of travel through the graph using UCS is $O(N^3)$. Since each state is push and pop from a priority queue that is maintained using binary heap, the time complexity of push & pop one state is $O(\log n)$. The overall time complexity should be less than $O(N^3 \log(n))$.

Since $3\leq N\leq 100$, the time complexity of $O(N^3 \log{n})$ is acceptable.


评论区