Honey Tour
Time limit1sMemory limit256 MB
For each top entrance and bottom exit of K stacked copies of an N by M grid, find the maximum honey pots on a simple path and how many paths reach it.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Bit manipulation, Graph, Matrix
- Solved
- No attempts yet
Problem
A little bear Koma likes honey very much. One day, Koma found a storehouse full of honey. The storehouse has entrances and exits, and its layout is as complicated as a maze. Soon Koma understood the structure and drew a map of it. The map is a two-dimensional grid of rows and columns. Each square contains either an obstacle or a honey pot. The map has one special property: it consists of identical copies of the same grid, stacked one below the other.
The top side of each of the columns is an entrance, and the bottom side of each column is an exit. Koma takes a tour through the storehouse under these rules:
- Koma enters through one of the entrances and leaves through one of the exits.
- Koma can move from one square to another if the two squares share a side.
- Koma can occupy a square only if the square contains no obstacle.
- Koma can visit each square at most once.
- Koma can eat from each honey pot visited.
Koma wants to visit as many honey pots as possible. For each entrance and each exit , compute two integers: the maximum number of honey pots Koma can visit when entering through the -th entrance and leaving through the -th exit, and the number of tours that reach this maximum. The number of tours can be large, so print it modulo .
Input
The first line contains three integers , , and (, , , ).
The next lines each contain characters that describe the repeating pattern. A '.' marks a square with a honey pot, and an 'X' marks a square with an obstacle.
Output
Print two matrices, one after another. The first matrix holds the maximum number of honey pots Koma can visit. The second matrix holds the number of tours that reach that maximum, modulo . The -th number on the -th line corresponds to entering through the -th entrance and leaving through the -th exit.