#K83122. Minimum Walls to Remove
Minimum Walls to Remove
Minimum Walls to Remove
You are given an n x n grid where each cell is initially filled with a wall (represented by the character #
). Your task is to determine the minimum number of walls to remove in order to create a path from the top-left corner (0, 0)
to the bottom-right corner (n-1, n-1)
. The path can move only horizontally or vertically between adjacent cells.
If it is impossible to create such a path, output no path
.
The optimal strategy, when a path is possible, is to remove exactly 2(n-1) - 1 walls. However, note that for n = 2
the task is impossible, so the answer should be no path
.
Examples:
- For
n = 4
: The minimum walls to remove is5
. - For
n = 3
: The minimum walls to remove is3
. - For
n = 2
: There is no path, so the answer isno path
. - For
n = 100
: The minimum walls to remove is197
. - For
n = 5
: The minimum walls to remove is7
.
Mathematically, for n > 2, the answer can be written in LaTeX as:
inputFormat
The input consists of a single integer n
on one line, representing the dimension of the grid.
outputFormat
Output a single line which is either the minimum number of walls to remove (an integer) or the string no path
if a valid path cannot be created.
4
5