rookie_010 2024-03-14 00:03 采纳率: 0%
浏览 13

無法在二維數組中找到特定元素位置

grid1 = [
[11, 12, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0], 
[11, 12, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0], 
[11, 12, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0], 
[11, 12, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0], 
[11,  0, 11, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0], 
[11, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0]
];

grid2 = [
[11, 12, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0], 
[11, 12, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0], 
[11, 12, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0], 
[11, 12, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0], 
[11,  0, 11, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0], 
[11, 11,  0, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0]
];

def find_last_snake_tail(grid):
    rows = len(grid)
    cols = len(grid[0])
    snake_paths = []
    current_path = []
 
    for j in range(cols):
        if grid[0][j] != 0:
            head = grid[0][j]
            i = 0
            j_start = j
 
            while i < rows and grid[i][j_start] == head:
                current_path.append((i, j_start))
                i += 1
 
            if i == rows:
                j_start += 1
                while j < cols and grid[i-1][j_start] == head:
                    current_path.append((i-1, j_start))
                    j_start += 1
 
            snake_paths.append(current_path)
            current_path = []
 
    last_snake_tail = snake_paths[-1][-1]
    return last_snake_tail
    
print(find_last_snake_tail(grid1))
print(find_last_snake_tail(grid2))
  1. 網格初始化為6*15,填入0

  2. 將有一個 for 迴圈去迴圈另一個二維陣列並將元素與其他邏輯進行比較

  3. 11表示比較相等,12表示比較不等於

  4. 然後程式會將比較結果(11/12)插入find_last_snake_tail上的網格中

  5. 如果目前比較結果與上次插入結果相同,則會新增至相同儲存格位置的新行,直到到達第6行,並繼續向右增長。

  6. 如果目前比較結果與上次插入結果不同,則會在第1行新單元格上開始一個新根(在舊蛇根旁邊)

  7. 程式也會根據find_last_snake_tail位置來控制蛇何時向下或向右生長

  8. 比較完成後,程式會根據網格在ui中渲染出一個表格

無法從上面的這些變數(grid1,grid2)獲得預期結果

grid1 實際結果:(4, 2) grid1 期望結果:(4, 3)

grid2 實際結果:(4, 2) grid2 期望結果:(5, 3)

  • 写回答

5条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-03-14 00:04
    关注

    引自免费微信小程序:皆我百晓生

    根据您提供的描述和代码,find_last_snake_tail 函数的目的是在给定的网格中找到“蛇”的最后一个尾巴的位置。但是从实际结果与期望结果对比来看,函数似乎没有正确地处理蛇尾位置。

    grid1 中,蛇应该在第4行第3列结束(即 (4, 3)),而在当前实现中,它错误地返回了 (4, 2)

    grid2 中,蛇应该在第5行第3列结束(即 (5, 3)),但当前实现同样返回了 (4, 2)

    问题在于,在遍历过程中,当到达最后一行且元素仍相等时,您的代码并没有正确检查并继续向右移动。以下是修复后的 find_last_snake_tail 函数:

    def find_last_snake_tail(grid):
        rows = len(grid)
        cols = len(grid[0])
        snake_paths = []
        current_path = []
    
        for j in range(cols):
            if grid[0][j] != 0:
                head = grid[0][j]
                i = 0
                j_start = j
    
                # 遍历每一行,直到遇到不相等或达到行尾
                while i < rows and j_start < cols and grid[i][j_start] == head:
                    current_path.append((i, j_start))
                    i += 1
                    if i == rows:  # 如果达到行尾,则开始检查下一列
                        j_start += 1
    
                # 检查剩余列是否连续
                while j_start < cols and grid[i-1][j_start] == head:
                    current_path.append((i-1, j_start))
                    j_start += 1
    
                snake_paths.append(current_path)
                current_path = []
    
        last_snake_tail = snake_paths[-1][-1]
        return last_snake_tail
    

    这段修改后的代码在遇到行尾时会尝试检查下一列,以确保找到正确的蛇尾位置。现在,对于给定的 grid1grid2,函数应能返回期望的结果。

    评论 编辑记录

报告相同问题?

问题事件

  • 修改了问题 3月14日
  • 创建了问题 3月14日