Building a Swimming Pool
Time limit2.5sMemory limit128 MB
Compute the minimum cost to convert a grid into grass/hole cells, forcing the border to be grass and charging per boundary edge between grass and hole.
- Level
Easy3 of 10
- Topics
- Array, Greedy, Implementation
- Solved
- No attempts yet
Problem
Sanggeun is building a swimming pool in Jeongin's front yard.
The pool site is columns wide and rows tall, divided into square cells. A pool consists of or more hole cells, which will later be filled with water.
Before construction, each cell is either a hole (.) or grass (#). Turning the site into a pool must follow these rules:
- Leaving a cell unchanged costs nothing.
- Digging a hole in a grass cell costs .
- Filling a hole cell and planting grass costs .
- Every edge on the pool's boundary — that is, every edge where a grass cell meets a hole cell — must be sealed so that water cannot leak, at a cost of per edge.
- In the finished site, every cell in the outermost row and outermost column must be grass.
Given the initial state of the site, write a program that computes the minimum cost to finish the pool.
Input
The first line contains the number of test cases . ()
For each test case, the first line contains the site dimensions and , separated by a space. () The second line contains three integers , , and . () The next lines describe the initial state of the site; each line consists of characters, where # denotes grass and . denotes a hole.
Output
For each test case, print the minimum cost to finish the pool on its own line.