USACO Silver 2018 February - Snow Boots

Authors: Qi Wang, Ryan Chou, Juheon Rhee, David Guo

Table of Contents

ExplanationImplementation

Official Analysis (C++)

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 11 with boot 11. Boot 11 can step up to 55 tiles ahead, but since tile 22 has depth 3>03\gt0 we cannot move forward.

Suppose we swap all the way to boot 33, which allows us to step up to 22 tiles with depths less than or equal to 66. We can move to tile 22 since its depth is 363\le6. We cannot step to tile 33 since its depth is 8>68\gt6. 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 88 under several different boots. Whenever we first hit tile 88 we record that boot's index, and throughout the DFS we keep the minimum such index as our answer.

There are O(NB)\mathcal{O}(NB) total states and since NN and BB are both at most 250250, visiting all O(N+B)\mathcal{O}(N + B) neighboring states will be at worst O(N2B+NB2)\mathcal{O}(N^2B + NB^2), which runs in time.

Implementation

Time Complexity: O(N2B+NB2)\mathcal{O}(N^2B + NB^2)

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] = 1
for i in range(n):
cur = -1
for 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!