1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| dx = [1, -1, 0, 0] dy = [0, 0, -1, 1] vis = [[0 for col in range(7)] for row in range(7)] res = 0 ans = 0 graph = [[0, 0, 0, 0, 1, 1, 0], [0, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 1], ]
def dfs(x, y): flag = 0 global res global ans global graph global dx global dy for i in range(0, 4): tox = x + dx[i] toy = y + dy[i] if tox < 0 or tox > 6 or toy < 0 or toy > 6: continue if vis[tox][toy] == 0 and graph[tox][toy]: vis[tox][toy] = 1 ans = ans + 1 dfs(tox, toy) return
if __name__ == '__main__': for i in range(0, 7): for j in range(0, 7): if graph[i][j] == 1: vis[i][j] = 1 ans = 1 dfs(i, j) res = max(res, ans) print(res)
|