Python第2个点RE
查看原帖
Python第2个点RE
692476
winstonm楼主2022/3/23 20:24

求助,Python第2个点RE了,我自己用编辑器跑这个测试用例,出的错是 递归超过了最大深度,大佬们帮忙看下

n, m = map(int, input().strip().split())
farm = [[0] * m for _ in range(n)]
for i in range(n):
    farm[i][:] = input().strip()[:]
def dfs(row, col):
    if 0 <= row + 1 < n and 0 <= col < m and farm[row + 1][col] == 'W':
        farm[row + 1][col] = '.'
        dfs(row + 1, col)
    if 0 <= row + 1 < n and 0 <= col - 1 < m and farm[row + 1][col - 1] == 'W':
        farm[row + 1][col - 1] = '.'
        dfs(row + 1, col - 1)
    if 0 <= row + 1 < n and 0 <= col + 1 < m and farm[row + 1][col + 1] == 'W':
        farm[row + 1][col + 1] = '.'
        dfs(row + 1, col + 1)
    if 0 <= row - 1 < n and 0 <= col < m and farm[row - 1][col] == 'W':
        farm[row - 1][col] = '.'
        dfs(row - 1, col)
    if 0 <= row - 1 < n and 0 <= col - 1 < m and farm[row - 1][col - 1] == 'W':
        farm[row - 1][col - 1] = '.'
        dfs(row - 1, col - 1)
    if 0 <= row - 1 < n and 0 <= col + 1 < m and farm[row - 1][col + 1] == 'W':
        farm[row - 1][col + 1] = '.'
        dfs(row - 1, col + 1)
    if 0 <= row < n and 0 <= col + 1 < m and farm[row][col + 1] == 'W':
        farm[row][col + 1] = '.'
        dfs(row, col + 1)
    if 0 <= row < n and 0 <= col - 1 < m and farm[row][col - 1] == 'W':
        farm[row][col - 1] = '.'
        dfs(row, col - 1)
count = 0
for i in range(n):
    for j in range(m):
        if farm[i][j] == 'W':
            count += 1
            dfs(i, j)
print(count)


2022/3/23 20:24
加载中...