You are given a 2D grid of characters representing a board. Each cell is one of:
'W' — a walk cell. From here the player moves 1 step to any of the four orthogonal neighbours.'J' — a jump cell. From here the player can jump to any other jump cell in the grid.'.' — an obstacle cell. The player cannot move through or land on this cell.The player starts at the top-left cell (0, 0) of the grid. The goal is to determine if the player can reach the bottom-right cell (n-1, m-1) of the grid.
1 <= n, m <= 200 (size of the grid)grid[i][j] is either 'W', 'J', or '.'.Example 1:
grid = [ ["J", "W", "W", "J"], ["W", "W", "W", "J"], ["W", "J", "W", "W"], ["J", "W", "W", "J"] ]TrueExample 2:
grid = [ ["J", "W", "W"], ["W", "W", "W"], ["W", "J", "W"] ]FalseIdentify Jump Cells:
Breadth-First Search (BFS):
(0, 0). During the BFS, consider two types of moves:
Check Reachability:
(n-1, m-1) is reached during the BFS, return True. Otherwise, return False.`python from collections import deque
def canReach(grid): n, m = len(grid), len(grid[0]) directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] jump_cells = []
# Find all jump cells
for i in range(n):
for j in range(m):
if grid[i][j] == 'J':
jump_cells.append((i, j))
# BFS
queue = deque([(0, 0)])
visited = set((0, 0))
while queue:
x, y = queue.popleft()
# Check if reached the bottom-right cell
if x == n - 1 and y == m - 1:
return True
# Walk cells
for dx, dy in directions:
nx, ny = x + dx, y + dy
if 0 <= nx < n and 0 <= ny < m and grid[nx][ny] != '.' and (nx, ny) not in visited:
queue.append((nx, ny))
visited.add((nx, ny))
# Jump cells
if grid[x][y] == 'J':
for jx, jy in jump_cells:
if (jx, jy) not in visited:
queue.append((jx, jy))
visited.add((jx, jy))
return False
`
After conducting a thorough search, I found no additional information or variations of this problem on the specified platforms. The problem statement, constraints, examples, and solution approach provided above are based on the excerpt and common problem-solving strategies for grid-based reachability problems. If you have any further details or specific requirements, feel free to ask.