Solve POJ 2386 with python

Solve POJ2386 LakeCounting with python

Learning DFS (Depth-First Search). As a memorandum, I couldn't understand it from the ant book alone. You do not have to do .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)

Recommended Posts

Solve POJ 2386 with python
Solve AtCoder 167 with python
Solve Sudoku with Python
Solve Lake Counting (POJ NO.2386) with Python3
[Python] Solve equations with sympy
Solve AtCoder ABC166 with python
Solve AtCoder ABC 186 with Python
solver> Link> Solve Excel Solver with python
Solve ABC163 A ~ C with Python
Solve ABC166 A ~ D with Python
Solve AtCoder Problems Recommendation with python (20200517-0523)
Solve ABC168 A ~ C with Python
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
python starts with ()
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
I wanted to solve ABC160 with Python
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Solve AtCoder ABC168 with python (A ~ D)
I wanted to solve ABC172 with Python
Serial communication with Python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Try scraping with Python.
Solve ABC168D in Python
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Solve ABC167-D in Python
Run Python with VBA
Handling yaml with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
Let's solve simultaneous linear equations with Python sympy!
1.1 Getting Started with Python
Collecting tweets with Python
I wanted to solve NOMURA Contest 2020 with Python
Solve ABC146-C in Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI