Maze
Time limit2sMemory limit1024 MB
Given an R by C grid with black cells, find the fewest N by N stamps that turn black cells white so a white path joins start and goal.
- Level
Medium7 of 10
- Topics
- Shortest path, BFS, Matrix
- Solved
- No attempts yet
Problem
President K loves solving mazes. He found a grid of cells from which he may create a maze. The grid has horizontal rows and vertical columns. Each cell is colored either white or black. The cell in the -th row () from the top and the -th column () from the left is called Cell .
President K solves the maze under the condition that he can pass white cells but cannot pass black cells. He solves it in the following way.
- He chooses, among the white cells, the starting cell and the goal cell .
- He can move from the current cell to an adjacent white cell in one of the four directions (up, down, left, or right). He repeats this until he finds a path from the starting cell to the goal cell.
The starting cell and the goal cell are already fixed. Depending on the colors, there may be no path from the starting cell to the goal cell made of white cells only. President K has a stamp of size . He performs the following operation several times so that a path made of white cells only exists from the starting cell to the goal cell.
Operation: He chooses a square region of cells and paints every cell in it white. Concretely, he chooses integers and with and , and paints white every Cell with and .
Using the stamp makes his hands dirty, so he wants as few operations as possible. Given the colors of the cells, the stamp size, and the positions of the starting and goal cells, write a program that computes the minimum number of operations needed to create a path of white cells from the starting cell to the goal cell.
Input
The first line contains , , and .
The second line contains and .
The third line contains and .
The next lines each contain a string of length made of . and #. The -th character of is . if Cell is white and # if it is black.
Output
Print one line containing the minimum number of operations President K has to perform so that a path of white cells from the starting cell to the goal cell exists.
Constraints
- .
- .
- , , , .
- .
- () is a string of length consisting of
.or#. - Cell and Cell are white.
- , , , , , , are integers.