首页 / 客观题库

80241 - 2026编程挑战赛Python提高组41

题目(材料题)

(1)求最短路径

给定一个 n×m 的二维网格,0 表示可通行,1 表示障碍物。从左上角 (0,0) 出发,只能向上下左右移动,求到达右下角 (n-1,m-1) 的最短路径长度(步数)。若无法到达,返回 -1。请补全下面程序中的 5 个空。

01 from collections import deque

02 def shortest_path(grid):

03     n, m = len(grid), len(grid[0])

04     dirs = [(1,0), (-1,0), (0,1), (0,-1)]

05     q =      ①    

06     visited = [[False]*m for _ in range(n)]

07     visited[0][0] = True

08 while  ②  :  

09     x, y, dist = q.popleft()

10         if    ③    :

11             return dist

12         for dx, dy in dirs:

13             nx, ny = x + dx, y + dy

14             if   ④   :   

15                 visited[nx][ny] = True

16                 q.append(   ⑤   )

17     return -1

||
( 单选 )

① 处应填

A deque([(0,0,0))

B deque([(0,0)])

C deque((0,0,0))

D deque([])

( 单选 )

② 处应填

A q is not None

B q

C len(q) <= 0

D not q.empty()

( 单选 )

③ 处应填

A x == n and y == m

B x == n-1 and y == m-1

C grid[x][y] == 0

D x == m-1 and y == n-144.

( 单选 )

④ 处应填

A 0 <= nx < n and 0 <= ny < m and grid[nx][ny] == 0

B 0 <= nx < n and 0 <= ny < m and not visited[nx][ny]

C 0 <= nx < n and 0 <= ny < m and grid[nx][ny] == 0 and not visited[nx][ny]

D 0 <= nx < m and 0 <= ny < n and grid[nx][ny] == 0 and not visited[nx][ny]

( 单选 )

⑤ 处应填

A (nx, ny, dist)

B (nx, ny, dist+1)

C (nx, ny, dist+1, visited[nx][ny])

D (nx, ny, 0)

意见反馈

    最多上传3张图片,格式为JPG、PNG、JPEG,单张不超过5MB

    注册

    发送验证码

    密码必须包含数字、字母和特殊字符

    找回密码

    发送验证码

    密码必须包含数字、字母和特殊字符

    运行 ID:67149

    • 测试点1:Accepted
    • 用时:0 ms
    • 内存:288 kb
    • 测试点2:Accepted
    • 用时:0 ms
    • 内存:288 kb
    输入
    203
    输出
    203

    test

    测评信息

    错误.in文件下载

    错误.out文件下载

    运行 ID:67149

    2019-01-24 15:06:36