Maze Repair
InterviewTime limit2sMemory limit1024 MB
Each cell points to a neighbor or outside the grid. Find the minimum cost of jump pads so that every starting cell escapes, which means hitting every cycle.
- Level
Medium4 of 10
- Topics
- Graph, Implementation
- Solved
- No attempts yet
Problem
A maze is an grid with rows and columns. Each cell shows one of 'up', 'down', 'left', or 'right'. Anyone who arrives at a cell moves one cell in the direction shown there. Repeating this, a person escapes once they leave the maze.

The blue path in the figure is an escape. With bad luck, though, a person can follow the red path in the figure and never leave. To prevent this, Hyungjin decides to repair the maze.

Hyungjin can install as many jump pads as he wants. Anyone who arrives at a cell with a jump pad leaves the maze immediately by jumping out.
Installing a jump pad costs a different amount depending on the cell. Installing a jump pad in the cell at row , column costs .
Find the minimum total cost of installing jump pads so that a person starting from any cell in the maze can escape.
Input
The first line contains and separated by a space.
The next lines each contain a string of length . The string uses only the uppercase letters U, D, L, and R, meaning up, down, left, and right.
The next lines each contain integers , and so on, separated by spaces. These give the cost of installing a jump pad in each cell.
Output
Print the minimum total cost of installing jump pads so that a person starting from any cell can escape the maze.