A Digging Problem (Large)
Time limit5sMemory limit512 MB
In an R x C cave grid, dig the fewest rock cells so you can walk and fall at most F rows to reach any bottom-row cell.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, BFS, Implementation
- Solved
- No attempts yet
Problem
A fire has broken out in the cave and smoke is everywhere. You want to dig your way down to the bottom of the cave, where you can breathe. The trouble is that the cave contains air holes, and falling too far hurts you.
The cave is an grid. Every cell is either solid rock or an air hole. You start on the top left cell . Here is the cell in row from the top and column from the left.
You can move one cell at a time, left or right, and only if the destination cell is an air hole. After you move, if the cell below you is an air hole, you fall until you hit solid rock or reach the bottom of the cave. Falling more than cells hurts you, so every single fall covers at most cells. While falling you cannot move left or right.
You can also dig, which turns a cell of solid rock into an air hole. Standing on , the only cells you can dig are , down and to the right, and , down and to the left. The cell directly above the one you dig, so or , has to be an air hole. While falling you cannot dig. You can dig several cells without leaving your position, and a cell you dug stays an air hole for the rest of the descent.
Your goal is to reach a cell of row , the bottom of the cave, without getting hurt, while digging as few cells as possible.
The cave below has , and .

- Start at and move right three times to .
- Dig the rock at . Cell A in the picture becomes an air hole.
- Move right one cell. Nothing is below you, so you fall three cells and land on .
- Dig the rock at . Cell B in the picture becomes an air hole.
- Move right one cell. Nothing is below you, so you fall one cell and land on .
You reached the bottom of the cave by digging 2 cells.
Input
The first line holds the number of test cases, . test cases follow.
The first line of each test case holds three integers.
R C F
is the number of rows in the cave, is the number of columns, and is the largest distance you can fall without getting hurt. The next lines hold characters each, with no spaces. Each character is one of two things.
#for solid rock.for an air hole
The top left cell is always an air hole, and the cell below it is always solid rock.
Limits
Output
For each test case, print one line in the format
Case #X: No
or
Case #X: Yes D
where is the case number, starting from 1. Print No if you cannot reach the bottom of the cave. If the bottom can be reached, print Yes, a space, and , the minimum number of cells that need digging.