USACO Silver 2018 February - Snow Boots
Authors: Qi Wang, Ryan Chou, Juheon Rhee, David Guo
Explanation
We view each position and boot as a state, and run a depth-first search (DFS) to explore every state while marking visited ones.
From any state, we step forward up to the boot's maximum step length onto tiles it can handle, or swap to any later boot that also works on the current tile.
When we reach the last tile, we record the current boot index, keeping the smallest value.
Consider the sample case. We start at tile with boot . Boot can step up to tiles ahead, but since tile has depth we cannot move forward.
Suppose we swap all the way to boot , which allows us to step up to tiles with depths less than or equal to . We can move to tile since its depth is . We cannot step to tile since its depth is . We move up one tile and now face a similar set of choices.
Each step or swap leads to a new state, which we mark visited so we never repeat it. Eventually this process reaches tile under several different boots. Whenever we first hit tile we record that boot's index, and throughout the DFS we keep the minimum such index as our answer.
There are total states and since and are both at most , visiting all neighboring states will be at worst , which runs in time.
Implementation
Time Complexity:
C++
#include <algorithm>#include <cstdio>#include <iostream>#include <vector>using namespace std;const int MAX_N = 250;int n;int m;
Java
import java.io.*;import java.util.*;public class snowboot {static List<Pair<Integer, Integer>> B = new ArrayList<>();static int[] D;static boolean[][] vist;static int N, M, A = 10000;public static void main(String[] args) throws IOException {InputReader in = new InputReader("snowboots.in");
Python
with open("snowboots.in") as r:n, b = map(int, r.readline().split())depth = list(map(int, r.readline().split()))max_depth = [[*map(int, r.readline().split())] for _ in range(b)]stor = [[0] * b for _ in range(n)]stor[0][0] = 1for i in range(n):cur = -1for j in range(b):
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!