标签:sim 使用 思路 div dfs == https class -o
1 class Solution: 2 def dfs(self,grid,m,n,i,j): 3 grid[i][j] = 0 4 res = 1 5 for x in range(m): 6 if grid[x][j] == 1: 7 res += self.dfs(grid,m,n,x,j) 8 for y in range(n): 9 if grid[i][y] == 1: 10 res += self.dfs(grid,m,n,i,y) 11 return res 12 13 def countServers(self, grid: ‘List[List[int]]‘) -> int: 14 m = len(grid)#row 15 n = len(grid[0])#column 16 count = 0 17 direct = [[-1,0],[1,0],[0,-1],[0,1]] 18 for i in range(m): 19 for j in range(n): 20 if grid[i][j] == 1: 21 temp = self.dfs(grid,m,n,i,j) 22 if temp > 1: 23 count += temp 24 return count
这道题目使用DFS处理,但是dfs内部的逻辑不太好想,比赛的时候开始有思路,调试了几次都不对,脑子就变空白了,一直没有做出来。
标签:sim 使用 思路 div dfs == https class -o
原文地址:https://www.cnblogs.com/asenyang/p/11921938.html