The Great Escape
Time limit1sMemory limit128 MB
Count escape routes on an n by m grid where the driver enters at the bottom-left corner heading north, only goes straight or turns right, and never revisits an intersection.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Combinatorics, Matrix
- Solved
- No attempts yet
Problem
Al Bytone, a notorious thief, is planning to rob a bank. He knows all too well that the moment he robs it, a pursuit begins. Unfortunately Al Bytone is a poor driver, and turning left gives him great trouble, so he wants an escape route on which, at every intersection, he only drives straight ahead or turns right. He also knows that once he passes through an intersection, the police move in and wait there, so he may pass through any intersection at most once. On top of that, some intersections already have police, and he must avoid those as well. (There is no police at the intersections near the bank or near the hideout.)
The streets of Byteburg form a rectangular grid. Every street runs either North-South or East-West, and every two streets of different orientation meet at exactly one intersection. The bank lies just south of the south-westernmost intersection, and Al Bytone starts his escape driving North.
Write a program that:
- reads the location of the hideout, the intersections that have police, and a positive integer ;
- computes the number of distinct escape routes from the bank to the hideout that obey the rules above;
- prints that number modulo .
Input
The first line contains three integers , , and (, ). Here is the number of East-West streets and is the number of North-South streets.
The second line contains two integers and (, ): the hideout is at the intersection of the -th North-South street and the -th East-West street. North-South streets are numbered to from West to East, and East-West streets are numbered to from North to South.
Each of the next lines contains characters, each either * or +. The character in line , column describes the intersection of the -th East-West street with the -th North-South street: * means there is police at that intersection, and + means it is free and the route may pass through it.
Al Bytone drives onto the intersection with coordinates from the South, that is, from the nonexistent intersection . Coordinates are written as (North-South street number, East-West street number).
Output
Print, in a single line, the number of valid escape routes taken modulo .