Cow Travelling
InterviewTime limit1sMemory limit128 MB
Count the number of walks of exactly T steps on a grid from a start cell to a target cell, where each step moves to a vertically or horizontally adjacent open cell.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Matrix, Implementation, Brute force
- Solved
- No attempts yet
Problem
Searching for the very best grass, the cows are travelling about the pasture, which is represented as a grid with rows and columns (, ). A keen observer, the farmer, recorded cow Bessie's position as at a certain time and then as exactly () seconds later. He is not sure whether she passed through before seconds, but he knows she is there at time .
Every second, a cow must move from its current cell to a vertically or horizontally adjacent cell (the cows never rest). The pasture also contains trees, and no cow can travel through a tree.
Given the pasture map, where '.' marks open pasture and '*' marks a tree, compute the number of distinct ways to travel from to in exactly seconds.
Input
- Line 1: Three space-separated integers , , and
- Lines 2..: Each line describes one row of the pasture with exactly characters, each of which is '.' or '*'
- Line : Four space-separated integers , , , and
Output
Output the single integer described above on one line.
Hint
For example, if the pasture is 4 rows by 5 columns and the cow travels from (row 1, column 3) to (row 1, column 5) in exactly 6 seconds, there is exactly one such way, since the only route travels around the two trees.