An Animal Contest 3 P5 - Monkey Canoe

View as PDF

Points: 12 (partial)
Time limit: 2.0s
Java 3.0s
Memory limit: 512M
Java 768M

Author:
Problem types

Tony the monkey is playing in the forest when he stumbles upon a bizarre grid with rows and columns. The grid contains cells, each consisting of either puddles or land. Intrigued by his discovery, Tony decides to cross it. He can start on any cell consisting of land in row and wants to reach row . However, Tony is deathly afraid of water and needs a way to cross the puddles!

Instead of jumping over the puddles or wearing boots, Tony decides to build a canoe! Before entering the grid, Tony can build a single canoe of length and width — he can use this canoe to cross any subgrid of puddles that can fit the entire length of the canoe. If Tony is on a land cell, he can lay down his canoe in the direction of an adjacent puddle cell, as long as the puddle can accommodate the length of the canoe. Once in the water, he can move either forward or backward in the direction he placed the canoe but cannot turn (Tony doesn't know how to steer). Once the tip of his canoe eventually hits land, Tony can get off his canoe on the cell of land he hits.

For example, if Tony is in cell and an adjacent puddle cell is cell , he is able to set his canoe on cell pointing towards decreasing and freely move the canoe parallel to the axis, as long as the canoe is not occupying a land cell.

While on land, Tony can move to any adjacent land cell. Since Tony can carry the canoe above his head and over any nearby water or vegetation, the size of his canoe is not limited by his surroundings while on land.

By using some sequence of walking and canoeing, Tony wants to eventually reach row . For each cell in row , you are to tell Tony the largest possible such that he is able to reach that cell. If can be arbitrarily large, tell him 0. If the cell is a puddle or unreachable, tell him -1.

Input Specification

The first line contains two space-separated integers and .

The next lines each contain characters representing the grid, with # representing land and . representing a puddle.

Output Specification

Output integers, each representing the largest possible for each cell in row . If can be infinitely large, output 0. If cell of row is a puddle or is unreachable, output -1.

Sample Input 1

4 5
#####
.....
##.##
#.#.#

Sample Output 1

1 -1 2 -1 1

Explanation for Sample 1

The following diagrams show a possible path that Tony can take to get from some cell on row to each of the land cells on row . Light blue cells represent puddle cells and brown cells represent land cells. A red arrow means a move with a canoe that has the same length as the arrow and a white arrow means a move on land.

In the following description, cell is the top-leftmost cell and cell is the bottom-rightmost cell.

In the rightmost diagram, Tony wants to go to the rightmost cell on row — cell . He starts at cell and places a canoe of length in the direction of the topmost red arrow which spans cell . He then paddles forward to cell allowing him to exit the canoe at cell .

Tony then places a canoe of length in the direction of the bottommost red arrow which spans cell , allowing him to exit the canoe at cell and reach his destination. The answer for this destination cell is therefore , and this can be proven to be the maximum that reaches this destination.

Sample Input 2

2 4
###.
...#

Sample Output 2

-1 -1 -1 -1

Explanation for Sample 2

Note that placing a canoe in the position and orientation of the red arrow is not allowed. Canoes can only be placed from a cell in row in the direction of the blue arrows.

Sample Input 3

2 2
##
##

Sample Output 3

0 0