# Check if each start cell can reach each finish cell drivable_pairs = [] for start, start_cell in start_cells.items(): for finish, finish_cell in finish_cells.items(): # Use dynamic programming to calculate the probability of reaching finish cell # starting from start cell dp = [[{} for _ in range(C)] for _ in range(R)] def dfs(row, col): if not isValidCell(row, col, R, C): return {} if isWall(grid, row, col): return {} if isHazard(grid, row, col): return {(row, col): 0.0} if (row, col) in dp[row][col]: return dp[row][col][(row, col)] neighbors = getNeighbors(row, col, R, C) probs = {} for r, c in neighbors: if (row, col) == (start_cell[0], start_cell[1]): if (r, c) == finish_cell: probs[(r, c)] = 1.0 continue elif isHazard(grid, r, c): probs[(r, c)] = 0.0 continue sub_probs = dfs(r, c) for sub_cell, sub_prob in sub_probs.items(): if (row, col) == (start_cell[0], start_cell[1]): if sub_cell == finish_cell: probs[(r, c)] = sub_prob * 0.5 + 0.5 elif isHazard(grid, sub_cell[0], sub_cell[1]): probs[(r, c)] = sub_prob * 0.5 else: probs[(r, c)] = sub_prob * 0.5 + dfs(sub_cell[0], sub_cell[1])[(r, c)] * 0.5 else: if sub_cell == finish_cell: probs[(r, c)] = sub_prob *