Skip to the code
cf.
+25 XP

#436 Fewest Walls to Knock Down

medium

A level editor checks how many walls a player must knock down to get from the top-left cell to the bottom-right cell. The level is a grid of strings where "." is open floor and "#" is a wall. Players move up, down, left, or right. Stepping onto open floor is free; stepping onto a wall means knocking it down, which costs 1.

Write fewestBreaks(level) that returns the smallest number of walls that must be knocked down. The top-left and bottom-right cells are always open.

Example 1

Input: level = [".#.",".#.",".#."]
Output: 1

The middle column is a solid wall, so at least one wall must go. Knocking down any one of them opens a path.

Example 2

Input: level = ["..",".."]
Output: 0

The floor is already open from corner to corner.

Constraints

  • 1 <= level.length, level[i].length <= 200
  • level[0][0] and the bottom-right cell are ".".
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).