0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

POJ 2386 をpythonで解く

Last updated at Posted at 2020-03-01

POJ2386 LakeCounting をpythonで解く

DFS(深さ優先探索)を学習中。
蟻本だけでは分からなかったので備忘録として。
.replace('.','0').replace('W','1')はしなくてもよい。

2386.py
n,m=map(int,input().split())
a=[list(map(int,list(input().replace('.','0').replace('W','1')))) for i in range(n)]

def dfs(x,y):
  a[x][y]=0
  for dx in [-1,0,1]:
    for dy in [-1,0,1]:
      nx=x+dx
      ny=y+dy
      if 0<=nx<n and 0<=ny<m and a[nx][ny]==1:
          dfs(nx,ny)

cnt=0
for i in range(n):
  for j in range(m):
    if a[i][j]==1:
      dfs(i,j)
      cnt+=1
print(cnt)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?