mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 15:09:40 +08:00
update Python solutions for kama102 and kama104
Provide BFS solution
This commit is contained in:
@ -140,6 +140,63 @@ int main() {
|
||||
|
||||
### Python
|
||||
|
||||
#### 广搜版
|
||||
```Python
|
||||
from collections import deque
|
||||
|
||||
n, m = list(map(int, input().split()))
|
||||
g = []
|
||||
for _ in range(n):
|
||||
row = list(map(int,input().split()))
|
||||
g.append(row)
|
||||
|
||||
directions = [(1,0),(-1,0),(0,1),(0,-1)]
|
||||
count = 0
|
||||
|
||||
def bfs(r,c,mode):
|
||||
global count
|
||||
q = deque()
|
||||
q.append((r,c))
|
||||
count += 1
|
||||
|
||||
while q:
|
||||
r, c = q.popleft()
|
||||
if mode:
|
||||
g[r][c] = 2
|
||||
|
||||
for di in directions:
|
||||
next_r = r + di[0]
|
||||
next_c = c + di[1]
|
||||
if next_c < 0 or next_c >= m or next_r < 0 or next_r >= n:
|
||||
continue
|
||||
if g[next_r][next_c] == 1:
|
||||
q.append((next_r,next_c))
|
||||
if mode:
|
||||
g[r][c] = 2
|
||||
|
||||
count += 1
|
||||
|
||||
|
||||
for i in range(n):
|
||||
if g[i][0] == 1: bfs(i,0,True)
|
||||
if g[i][m-1] == 1: bfs(i, m-1,True)
|
||||
|
||||
for j in range(m):
|
||||
if g[0][j] == 1: bfs(0,j,1)
|
||||
if g[n-1][j] == 1: bfs(n-1,j,1)
|
||||
|
||||
for i in range(n):
|
||||
for j in range(m):
|
||||
if g[i][j] == 2:
|
||||
g[i][j] = 1
|
||||
else:
|
||||
g[i][j] = 0
|
||||
|
||||
for row in g:
|
||||
print(" ".join(map(str, row)))
|
||||
```
|
||||
|
||||
|
||||
### Go
|
||||
|
||||
### Rust
|
||||
|
@ -366,6 +366,93 @@ public class Main {
|
||||
|
||||
### Python
|
||||
|
||||
|
||||
#### BFS
|
||||
```Python
|
||||
from typing import List
|
||||
from collections import defaultdict
|
||||
|
||||
class Solution:
|
||||
def __init__(self):
|
||||
self.direction = [(1,0),(-1,0),(0,1),(0,-1)]
|
||||
self.res = 0
|
||||
self.count = 0
|
||||
self.idx = 1
|
||||
self.count_area = defaultdict(int)
|
||||
|
||||
def max_area_island(self, grid: List[List[int]]) -> int:
|
||||
if not grid or len(grid) == 0 or len(grid[0]) == 0:
|
||||
return 0
|
||||
|
||||
for i in range(len(grid)):
|
||||
for j in range(len(grid[0])):
|
||||
if grid[i][j] == 1:
|
||||
self.count = 0
|
||||
self.idx += 1
|
||||
self.dfs(grid,i,j)
|
||||
# print(grid)
|
||||
self.check_area(grid)
|
||||
# print(self.count_area)
|
||||
|
||||
if self.check_largest_connect_island(grid=grid):
|
||||
return self.res + 1
|
||||
return max(self.count_area.values())
|
||||
|
||||
def dfs(self,grid,row,col):
|
||||
grid[row][col] = self.idx
|
||||
self.count += 1
|
||||
for dr,dc in self.direction:
|
||||
_row = dr + row
|
||||
_col = dc + col
|
||||
if 0<=_row<len(grid) and 0<=_col<len(grid[0]) and grid[_row][_col] == 1:
|
||||
self.dfs(grid,_row,_col)
|
||||
return
|
||||
|
||||
def check_area(self,grid):
|
||||
m, n = len(grid), len(grid[0])
|
||||
for row in range(m):
|
||||
for col in range(n):
|
||||
self.count_area[grid[row][col]] = self.count_area.get(grid[row][col],0) + 1
|
||||
return
|
||||
|
||||
def check_largest_connect_island(self,grid):
|
||||
m, n = len(grid), len(grid[0])
|
||||
has_connect = False
|
||||
for row in range(m):
|
||||
for col in range(n):
|
||||
if grid[row][col] == 0:
|
||||
has_connect = True
|
||||
area = 0
|
||||
visited = set()
|
||||
for dr, dc in self.direction:
|
||||
_row = row + dr
|
||||
_col = col + dc
|
||||
if 0<=_row<len(grid) and 0<=_col<len(grid[0]) and grid[_row][_col] != 0 and grid[_row][_col] not in visited:
|
||||
visited.add(grid[_row][_col])
|
||||
area += self.count_area[grid[_row][_col]]
|
||||
self.res = max(self.res, area)
|
||||
|
||||
return has_connect
|
||||
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
m, n = map(int, input().split())
|
||||
grid = []
|
||||
|
||||
for i in range(m):
|
||||
grid.append(list(map(int,input().split())))
|
||||
|
||||
|
||||
sol = Solution()
|
||||
print(sol.max_area_island(grid))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
```
|
||||
|
||||
|
||||
```Python
|
||||
import collections
|
||||
|
||||
|
Reference in New Issue
Block a user