• 注册
  • 刷题上岸
  • 今日 0
  • 帖子 12
  • 关注 1
  • 捐助

    请在小工具里添加二维码

    刷题上岸 刷题上岸 关注:1 内容:12

    Amazon | OA 2019 | Treasure Island

  • 查看作者
  • 打赏作者
  • 拉黑名单
    • 刷题上岸
    • 等级Max
      土豪

      You have a map that marks the location of a treasure island. Some of the map area has jagged rocks and dangerous reefs. Other areas are safe to sail in. There are other explorers trying to find the treasure. So you must figure out a shortest route to the treasure island.

      Assume the map area is a two dimensional grid, represented by a matrix of characters. You must start from the top-left corner of the map and can move one block up, down, left or right at a time. The treasure island is marked as X in a block of the matrix. X will not be at the top-left corner. Any block with dangerous rocks or reefs will be marked as D. You must not enter dangerous blocks. You cannot leave the map area. Other areas O are safe to sail in. The top-left corner is always safe. Output the minimum number of steps to get to the treasure.

      Example:

      Input:
      [['O', 'O', 'O', 'O'],
       ['D', 'O', 'D', 'O'],
       ['O', 'O', 'O', 'O'],
       ['X', 'D', 'D', 'O']]
      
      Output: 5Explanation: Route is (0, 0), (0, 1), (1, 1), (2, 1), (2, 0), (3, 0) The minimum route takes 5 steps.

      def solution(m):
          if len(m) == 0 or len(m[0]) == 0:
              return -1  # impossible
          matrix = [row[:] for row in m]
          nrow, ncol = len(matrix), len(matrix[0])
          q = deque([((0, 0), 0)])  # ((x, y), step)
          matrix[0][0] = "D"
          while q:
              (x, y), step = q.popleft()
              for dx, dy in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
                  if 0 <= x+dx < nrow and 0 <= y+dy < ncol:
                      if matrix[x+dx][y+dy] == "X":
                          return step+1
                      elif matrix[x+dx][y+dy] == "O":
                          # mark visited
                          matrix[x + dx][y + dy] = "D"
                          q.append(((x+dx, y+dy), step+1))
          return -1

      请登录之后再进行评论

      登录
    • 做任务
    • 实时动态
    • 返回顶部