FastPrepEscape the Haunted Castle

Escape the Haunted Castle

Persona logoPersona● EasyFULLTIMEOA
Learn

Problem statement

An adventurer starts at (0, 0) in a rows × columns haunted castle. Walls are impassable, and the adventurer may move one cell up, down, left, or right.

Return the minimum number of moves needed to reach escapePoint, or -1 when no path exists.

Function

escapeCastle(rows: int, columns: int, walls: int[][], escapePoint: int[]) → int

Examples

Example 1

rows = 2columns = 2walls = [[0,1]]escapePoint = [1,1]return = 2

The only route moves down and then right.

Example 2

rows = 3columns = 3walls = [[1,0],[1,1],[1,2]]escapePoint = [2,2]return = -1

A complete wall row separates the start from the exit.

Example 3

rows = 1columns = 1walls = []escapePoint = [0,0]return = 0

The adventurer starts on the escape point.

Constraints

  • 1 <= rows, columns <= 10.
  • Every wall and the escape point are distinct valid cells.
  • (0, 0) is not a wall.

More Persona problems

See Persona hiring insights
public int escapeCastle(int rows, int columns, int[][] walls, int[] escapePoint) {
    // Return the shortest escape distance.
}
rows2
columns2
walls[[0,1]]
escapePoint[1,1]
expected2
Checking account…