Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 418168e

Browse files
committed
update Python solutions for kama102 and kama104
Provide BFS solution
1 parent f16c640 commit 418168e

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

‎problems/kamacoder/0102.沉没孤岛.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,63 @@ int main() {
140140

141141
### Python
142142

143+
#### 广搜版
144+
```Python
145+
from collections import deque
146+
147+
n, m = list(map(int, input().split()))
148+
g = []
149+
for _ in range(n):
150+
row = list(map(int,input().split()))
151+
g.append(row)
152+
153+
directions = [(1,0),(-1,0),(0,1),(0,-1)]
154+
count = 0
155+
156+
def bfs(r,c,mode):
157+
global count
158+
q = deque()
159+
q.append((r,c))
160+
count += 1
161+
162+
while q:
163+
r, c = q.popleft()
164+
if mode:
165+
g[r][c] = 2
166+
167+
for di in directions:
168+
next_r = r + di[0]
169+
next_c = c + di[1]
170+
if next_c < 0 or next_c >= m or next_r < 0 or next_r >= n:
171+
continue
172+
if g[next_r][next_c] == 1:
173+
q.append((next_r,next_c))
174+
if mode:
175+
g[r][c] = 2
176+
177+
count += 1
178+
179+
180+
for i in range(n):
181+
if g[i][0] == 1: bfs(i,0,True)
182+
if g[i][m-1] == 1: bfs(i, m-1,True)
183+
184+
for j in range(m):
185+
if g[0][j] == 1: bfs(0,j,1)
186+
if g[n-1][j] == 1: bfs(n-1,j,1)
187+
188+
for i in range(n):
189+
for j in range(m):
190+
if g[i][j] == 2:
191+
g[i][j] = 1
192+
else:
193+
g[i][j] = 0
194+
195+
for row in g:
196+
print(" ".join(map(str, row)))
197+
```
198+
199+
143200
### Go
144201

145202
### Rust

‎problems/kamacoder/0104.建造最大岛屿.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,93 @@ public class Main {
366366

367367
### Python
368368

369+
370+
#### BFS
371+
```Python
372+
from typing import List
373+
from collections import defaultdict
374+
375+
class Solution:
376+
def __init__(self):
377+
self.direction = [(1,0),(-1,0),(0,1),(0,-1)]
378+
self.res = 0
379+
self.count = 0
380+
self.idx = 1
381+
self.count_area = defaultdict(int)
382+
383+
def max_area_island(self, grid: List[List[int]]) -> int:
384+
if not grid or len(grid) == 0 or len(grid[0]) == 0:
385+
return 0
386+
387+
for i in range(len(grid)):
388+
for j in range(len(grid[0])):
389+
if grid[i][j] == 1:
390+
self.count = 0
391+
self.idx += 1
392+
self.dfs(grid,i,j)
393+
# print(grid)
394+
self.check_area(grid)
395+
# print(self.count_area)
396+
397+
if self.check_largest_connect_island(grid=grid):
398+
return self.res + 1
399+
return max(self.count_area.values())
400+
401+
def dfs(self,grid,row,col):
402+
grid[row][col] = self.idx
403+
self.count += 1
404+
for dr,dc in self.direction:
405+
_row = dr + row
406+
_col = dc + col
407+
if 0<=_row<len(grid) and 0<=_col<len(grid[0]) and grid[_row][_col] == 1:
408+
self.dfs(grid,_row,_col)
409+
return
410+
411+
def check_area(self,grid):
412+
m, n = len(grid), len(grid[0])
413+
for row in range(m):
414+
for col in range(n):
415+
self.count_area[grid[row][col]] = self.count_area.get(grid[row][col],0) + 1
416+
return
417+
418+
def check_largest_connect_island(self,grid):
419+
m, n = len(grid), len(grid[0])
420+
has_connect = False
421+
for row in range(m):
422+
for col in range(n):
423+
if grid[row][col] == 0:
424+
has_connect = True
425+
area = 0
426+
visited = set()
427+
for dr, dc in self.direction:
428+
_row = row + dr
429+
_col = col + dc
430+
if 0<=_row<len(grid) and 0<=_col<len(grid[0]) and grid[_row][_col] != 0 and grid[_row][_col] not in visited:
431+
visited.add(grid[_row][_col])
432+
area += self.count_area[grid[_row][_col]]
433+
self.res = max(self.res, area)
434+
435+
return has_connect
436+
437+
438+
439+
440+
def main():
441+
m, n = map(int, input().split())
442+
grid = []
443+
444+
for i in range(m):
445+
grid.append(list(map(int,input().split())))
446+
447+
448+
sol = Solution()
449+
print(sol.max_area_island(grid))
450+
451+
if __name__ == '__main__':
452+
main()
453+
```
454+
455+
369456
```Python
370457
import collections
371458

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /