当前位置:   article > 正文

力扣-图论

力扣-图论

797. 所有可能的路径

  1. class Solution:
  2. def __init__(self):
  3. self.res = []
  4. # self.path = [0]
  5. def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
  6. path = [0]
  7. # l = set()
  8. self.tracebacking( graph, 0, path)
  9. return self.res
  10. def tracebacking(self, graph, node, path):
  11. if(node == len(graph)-1):
  12. self.res.append(path[:])
  13. # print(self.res)
  14. return
  15. for index, node in enumerate(graph[node]):
  16. path.append(node)
  17. self.tracebacking( graph, node, path)
  18. path.pop()

200. 岛屿数量

  1. class Solution:
  2. def __init__(self):
  3. self.dir = [[0,1],# right
  4. [1,0],#down
  5. [-1,0],#up
  6. [0,-1]]#left
  7. def numIslands(self, grid: List[List[str]]) -> int:
  8. m = len(grid) #hang
  9. n = len(grid[0])#lie
  10. print(m, n)
  11. visited = [[False]*n for _ in range(m)]
  12. result = 0
  13. for i in range(m):
  14. for j in range(n):
  15. # print( grid[i][j] == 1))
  16. if visited[i][j] == False and grid[i][j] == "1":
  17. self.dfs(grid, visited, i, j)
  18. #self.bfs(grid, visited, i, j)
  19. result += 1
  20. # print(visited)
  21. return result
  22. def dfs(self, grid, visited, x, y):
  23. if grid[x][y] == "0" or visited[x][y] == True:
  24. return
  25. visited[x][y] = True
  26. for i in range(4):
  27. nextx = x + self.dir[i][0]
  28. nexty = y + self.dir[i][1]
  29. if nextx<0 or nextx>=len(grid) or nexty<0 or nexty>=len(grid[0]):
  30. continue
  31. self.dfs(grid, visited, nextx, nexty)
  32. def bfs(self, grid, visited, x, y):
  33. q = deque()
  34. q.append((x, y))
  35. visited[x][y] = True
  36. while q:
  37. x, y = q.popleft()
  38. # print(x,y)
  39. for i in range(4):
  40. nextx = x + self.dir[i][0]
  41. nexty = y + self.dir[i][1]
  42. if nextx<0 or nextx>=len(grid) or nexty<0 or nexty>=len(grid[0]) or visited[nextx][nexty] == True or grid[nextx][nexty] == "0":
  43. continue
  44. visited[nextx][nexty] = True
  45. q.append((nextx, nexty))

695. 岛屿的最大面积

 对岛屿数量进行修改就可以了

区别在于最大面积计算递归次数(dfs),或者入栈次数(bfs)

  1. class Solution:
  2. def __init__(self):
  3. self.dir = [[0,1],# right
  4. [1,0],#down
  5. [-1,0],#up
  6. [0,-1]]#left
  7. self.maxsize = 0
  8. self.count = 0
  9. def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
  10. m = len(grid) #hang
  11. n = len(grid[0])#lie
  12. # print(m, n)
  13. visited = [[False]*n for _ in range(m)]
  14. for i in range(m):
  15. for j in range(n):
  16. if visited[i][j] == False and grid[i][j] == 1:
  17. #bfs
  18. # self.maxsize = max(self.maxsize, self.bfs(grid, visited, i, j))
  19. #dfs
  20. self.count = 0
  21. self.dfs(grid, visited, i, j)
  22. self.maxsize = max(self.maxsize, self.count)
  23. return self.maxsize
  24. # def bfs(self, grid, visited, x, y):
  25. # q = deque()
  26. # q.append((x, y))
  27. # visited[x][y] = True
  28. # maxtem = 1
  29. # while q:
  30. # x, y = q.popleft()
  31. # for i in range(4):
  32. # nextx = x + self.dir[i][0]
  33. # nexty = y + self.dir[i][1]
  34. # if nextx<0 or nextx>=len(grid) or nexty<0 or nexty>=len(grid[0]) or visited[nextx][nexty] == True or grid[nextx][nexty] == 0:
  35. # continue
  36. # visited[nextx][nexty] = True
  37. # maxtem += 1
  38. # q.append((nextx, nexty))
  39. # return maxtem
  40. def dfs(self, grid, visited, x, y):
  41. if grid[x][y] == 0 or visited[x][y] == True:
  42. return 0
  43. visited[x][y] = True
  44. self.count += 1
  45. for i in range(4):
  46. nextx = x + self.dir[i][0]
  47. nexty = y + self.dir[i][1]
  48. if nextx<0 or nextx>=len(grid) or nexty<0 or nexty>=len(grid[0]):
  49. continue
  50. self.dfs(grid, visited, nextx, nexty)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/387016
推荐阅读
相关标签
  

闽ICP备14008679号