Cow Hopscotch
Time limit1sMemory limit256 MB
Count paths from the top-left to the bottom-right cell moving down and right where consecutive cells hold different values.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Prefix sum
- Solved
- No attempts yet
Problem
Farmer John's cows copied the human game of hopscotch and invented a version of their own. Animals that weigh close to a ton play it, so a round usually ends in a mess, and the cows still gather for it almost every afternoon.
The board is an grid, and every square holds one integer between and .
A cow starts on the top left square and reaches the bottom right square with a sequence of jumps. A jump from the current square to another square is allowed when all three conditions hold.
- The square you jump to holds an integer different from the integer on the current square.
- The square you jump to is at least one row below the current square.
- The square you jump to is at least one column to the right of the current square.
Count the different sequences of valid jumps that take a cow from the top left square to the bottom right square. Two sequences count as different when the lists of visited squares differ.
Input
The first line contains , , and . (, , )
Each of the next lines contains integers, all between and .
Output
Print the number of ways to get from the top left square to the bottom right square, modulo .