You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BFS算法实现报错:TypeError: 'ellipsis' object is not subscriptable

问题解决:TypeError: 'ellipsis' object is not subscriptable

核心错误原因

你代码里的visited = ...将变量赋值为Python的Ellipsis对象,该对象不支持下标访问[row][col],这是触发报错的直接原因。必须将visited初始化为与网格尺寸匹配的二维布尔数组,用于记录节点是否被访问过。

完整修正方案

除了修复visited的初始化问题,代码里还有其他几处错误需要修正,以下是分步说明:

  1. 初始化visited数组
    替换visited = ...为:

    visited = [[False for _ in range(self.width)] for _ in range(self.height)]
    

    这段代码创建一个与网格同高同宽的二维数组,所有元素初始为False,表示对应位置未被访问。

  2. 修正deque出队方法
    Python的collections.deque没有dequeue()方法,正确的左侧出队方法是popleft(),修改:

    x = x_queue.dequeue()
    y = y_queue.dequeue()
    

    为:

    x = x_queue.popleft()
    y = y_queue.popleft()
    
  3. 修正终点判断运算符
    用逻辑与and代替按位与&,逻辑与才是判断两个条件同时成立的正确方式:

    if x == x2 and y == y2:
    
  4. 修正方向计算错误
    计算new_col时应使用当前列坐标y加上方向偏移,而非行坐标x:

    new_col = y + direction_col[i]
    
  5. 修正代码缩进问题

    • 位置验证、入队等逻辑必须放在方向循环for i in range(4)内部,否则只会处理最后一个方向
    • 层数计数逻辑应放在当前节点的所有方向处理完成后(while循环内的末尾)
    • 提前返回-1的语句位置错误,应放在while循环结束后(未找到路径时返回)

修正后的完整代码

from collections import deque

def findRoute(self, x1, y1, x2, y2):
    grid = self.grid
    starting_row, starting_col = x1, y1

    x_queue, y_queue = deque(), deque()
    number_of_moves = 0
    number_of_nodes_in_current_layer = 1
    number_of_nodes_in_next_layer = 0
    end_reached = False

    # 上下左右四个方向偏移量
    direction_row = [-1, 1, 0, 0]
    direction_col = [0, 0, 1, -1]

    # 初始化visited数组:与网格同尺寸的二维布尔数组
    visited = [[False for _ in range(self.width)] for _ in range(self.height)]
    
    x_queue.append(starting_row)
    y_queue.append(starting_col)
    visited[starting_row][starting_col] = True

    while x_queue:
        x = x_queue.popleft()
        y = y_queue.popleft()

        if x == x2 and y == y2:
            end_reached = True
            break

        # 遍历四个方向
        for i in range(4):
            new_row = x + direction_row[i]
            new_col = y + direction_col[i]

            # 验证位置是否在网格范围内
            if new_row < 0 or new_col < 0 or new_row >= self.height or new_col >= self.width:
                continue
            # 跳过已访问或有障碍物的格子
            if visited[new_row][new_col] or grid[new_row][new_col]:
                continue

            x_queue.append(new_row)
            y_queue.append(new_col)
            visited[new_row][new_col] = True
            number_of_nodes_in_next_layer += 1

        # 更新当前层节点数,若当前层处理完则切换到下一层并增加步数
        number_of_nodes_in_current_layer -= 1
        if number_of_nodes_in_current_layer == 0:
            number_of_nodes_in_current_layer = number_of_nodes_in_next_layer
            number_of_nodes_in_next_layer = 0
            number_of_moves += 1

    # 根据是否到达终点返回结果
    if end_reached:
        return number_of_moves
    return -1

内容的提问来源于stack exchange,提问作者Jack Goodwin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 14:35:21