Wall Value
InterviewTime limit2sMemory limit512 MB
Given a grid with walls, N pieces, and a destination, find the sum of shortest distances and the total drop in that sum when each wall is removed.
- Level
Medium5 of 10
- Topics
- BFS, Graph, Implementation
- Solved
- No attempts yet
Problem
Albert enjoys a board game that can be played alone. To play, he first prepares a grid board with rows and columns and places walls on some cells. Then he puts game pieces, one each, on different empty cells (cells without a wall). Last, he picks one of the remaining empty cells as the destination for every piece. Let the destination be (row , column ). The pieces are numbered 1 to , and the cell where piece is placed is .
For example, the left figure below shows a board with , where the cells marked 'W' are walls and the cells marked '.' are empty. Let be the string of length that shows row of the board. The board in this example is . The right figure shows the cells where Albert placed three pieces and the destination, with , , and .

Each piece moves up, down, left, or right through empty cells to reach the destination. Let be the minimum distance piece must travel to reach the destination. In the example above, (move down, then right), , and . Let be the total minimum distance of all pieces. This is the score of the game. The score of this example is .
Albert wants to change the game a little. First, he keeps the initial board state and removes exactly one wall. Removing a wall can open new paths, so the score stays the same or gets lower. Call the drop in score the value of the removed wall. The example board has 5 walls, so Albert can remove one wall in 5 different ways. The 'X' marks in the figure show the removed walls. They are empty cells now, so pieces can pass through them.

For the five new boards, compute the score and subtract it from the original score of 17 to get the value of the removed wall. From left to right:
- In the leftmost game, the score is 17, the same as the original, so the wall at (1, 2) has value 0.
- In the second game, the score is also 17, and the wall at (1, 3) has value 0.
- In the third game, the score is 9, because the minimum distances of pieces 2 and 3 drop to and . So the wall at (2, 3) has value 8.
- In the fourth game, the score is 13, because the minimum distances of pieces 2 and 3 drop to and . So the wall at (3, 2) has value 4.
- In the fifth game, the score is 17, so the wall at (3, 3) has value 0.
Adding the values of all five walls gives . Call this the total wall value.
Given the board, find the original score and the total wall value.
Input
The first line contains the number of test cases .
The first line of each test case contains , , , , and , separated by spaces. The next lines each contain two integers and , separated by a space, which are the position of the -th piece. The next lines each contain a string of length with no spaces, which is row of the board . A wall is 'W' and an empty cell is '.'.
Output
For each test case, print two integers separated by a space on one line. The first integer is the original score . The second integer is the total wall value.
Constraints
- and
- For each with :
- and
- Within each test case, the piece positions and the destination are distinct. No two pieces share a cell, and no piece shares a cell with the destination.
- Every piece position and the destination are empty cells (no wall).
- Input only includes test cases where, with no walls removed, every piece can reach the destination through empty cells moving up, down, left, or right.
- There is at least 1 wall.