← CSES Better

Cow Hopscotch (Silver)

USACO · 2015 · February · silver-2

Open official problem ↗
Ask AI

Open a hint-first prompt in a new AI chat. Grok may ask you to confirm before sending.

Open Claude Desktop

dynamic-programming

The organizer’s problem statement is available at the official source link.

sample trace

Cow Hopscotch: quadratic DP

For each cell, sum the path counts of strictly upper-left cells whose color differs; the DP grid shows the sample states.

colors

1111
1321
1241
1111

path Counts

1000
0110
0122
0025

start

11

goal

44

answer

5
  1. 1

    Define a state

    dp[r][c] counts valid paths ending at cell (r,c). Start with one path at the top-left.
  2. 2

    Sum valid predecessors

    For each cell, add states from strictly upper-left cells whose color differs from this cell.
  3. 3

    Read the destination

    The DP matrix ends with 5 paths at the bottom-right, matching the sample output.
Tools
1
2
3
4
5
6
7
8
9
10
11
12
Draft saved locally
|12 lines, 156 chars

Run output

Run your code to see output.