Hippity Hopscotch
Time limit1sMemory limit128 MB
On an n by n grid of penny stacks, starting at (0,0) and jumping up to k cells in a row or column to a strictly larger stack, find the maximum total collected.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Sorting, Matrix
- Solved
- No attempts yet
Problem
The game of hopscotch involves chalk, sidewalks, jumping, and picking things up. In our variant, money is involved as well.
The game is played on a square grid of dimension : each grid location is labelled where and . On each grid location there is a stack of between and pennies.
A contestant begins by standing at location . The contestant collects the pennies on the square where they stand, then jumps either horizontally or vertically to another square. The destination square must be within the contestant's jumping capability of locations (that is, it lies in the same row or the same column and is at most steps away), and it must hold strictly more pennies than the square the contestant is currently on.
The contestant keeps jumping and collecting until no legal move remains. Given , , and the number of pennies on each grid location, compute the maximum number of pennies the contestant can collect.
Input
- A line with two integers and (, ).
- Then lines follow, each with integers. The first line lists the pennies at locations ; the next line lists ; and so on. Each value is between and .
Output
- A single integer: the maximum number of pennies the contestant can collect.