python的,第二个点RE了,搞不懂为什么,大佬求助
查看原帖
python的,第二个点RE了,搞不懂为什么,大佬求助
1231862
lzysxfcs楼主2024/4/14 22:27
def dfs(grid, row, col):
    # 定义方向数组,包含八个方向的偏移量
    directions = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
    # 将当前位置标记为已访问
    grid[row][col] = '.'
    # 遍历八个方向
    for d in directions:
        new_row = row + d[0]
        new_col = col + d[1]

        # 检查新位置是否在网格范围内,且为水坑
        if 0 <= new_row < len(grid) and 0 <= new_col < len(grid[0]) and grid[new_row][new_col] == 'W':
            # 递归调用DFS
            dfs(grid, new_row, new_col)
def count_water_pools(grid):
    count = 0
    # 遍历整个网格
    for i in range(len(grid)):
        for j in range(len(grid[0])):
            # 如果当前位置为水坑,调用DFS进行深度搜索
            if grid[i][j] == 'W':
                dfs(grid, i, j)
                count += 1
    return count

N, M = map(int, input().split())
grid = [list(input()) for _ in range(N)]
print(count_water_pools(grid))

2024/4/14 22:27
加载中...