GreedyGrid
SRM 535 · 2012-01-31 · by wrong
Problem Statement
Fox Jiro and Eel Saburo are good friends. One day Jiro gives Saburo the following problem:
You are given a rectangular grid. It is H cells high and W cells wide. Each cell of the grid contains a non-negative integer which is between 0 and S, inclusive. The top-left cell of the grid always contains 0.
First you are on the top-left cell of the grid. You move in steps. In each step, you can go either down or right to the immediately adjacent cell. Your path terminates when you reach the bottom-right cell of the grid. Let K be the sum of integers contained in the cells which you visited (including the bottom-right cell). What is the maximum possible value of K?
This is a well-known classical problem solvable by dynamic programming. But Saburo doesn't know the clever solution. He found the following greedy approach to this problem:
- If he is in the rightmost column, he takes a step down. If he is in the bottommost row, he takes a step right.
- Otherwise, he goes to the cell which contains the bigger integer. If two adjacent cells have same integer, he goes to the right cell.
Jiro is interested in Saburo's greedy approach. He calls a grid p-greedy if the sum of integers visited by the greedy algorithm is equal to p.
You are given the
Constraints
- H will be between 1 and 2500, inclusive.
- W will be between 1 and 2500, inclusive.
- S will be between 0 and 100, inclusive.
2 2 1 Returns: 4
These are the 4 grids: 01 01 00 00 00 10 10 01
2 2 2 Returns: 9
These are the 9 grids: 02 02 02 01 01 00 00 01 00 00 10 20 01 11 11 20 20 02
2 2 0 Returns: 1
47 58 100 Returns: 1301
1234 2345 97 Returns: 8894
Submissions are judged against all 85 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class FoxAndGreed with a public method int count(int H, int W, int S) · 85 test cases · 2 s / 256 MB per case