Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- KDT
- ์ฝ๋ฉ
- react
- ๊ฐ๋ฐ
- HTML
- heapq
- ๊ตญ๋น์ง์
- Til
- ํ๋ก ํธ์๋
- mongodb
- ๋๋ฆผ์ฝ๋ฉ
- ๋ชจ๊ฐ์ฝ
- fe
- ํฌ๋กค๋ง
- javascript
- ํ ์ดํ๋ก์ ํธ
- ํ์ด์ฌ
- ๋ฐฑ์ค
- ์๊ณ ๋ฆฌ์ฆ
- ์ฝ๋ฉ์ ํ
- ์ฝ๋ฉํ ์คํธ
- node.js
- ํ๋ก๊ทธ๋๋จธ์ค
- ํ๋ก์ ํธ
- JS
- Python
- ๋ ธ๋ง๋์ฝ๋
- CSS
- error
- ๊ทธ๋ฆฌ๋
Archives
- Today
- Total
๐ฑ → ๐ณ
[Baekjoon] 4963. ์ฌ์ ๊ฐ์ - python ๋ณธ๋ฌธ
728x90
https://www.acmicpc.net/problem/4963
์ฝ๋
from collections import deque
dx = [1,0,0,-1,1,1,-1,-1]
dy = [0,1,-1,0,1,-1,-1,1]
def bfs(a,b):
q = deque()
q.append((a,b))
graph[a][b] = 0
while q:
x,y = q.popleft()
for i in range(8):
nx = x + dx[i]
ny = y + dy[i]
if nx < 0 or nx >= h or ny < 0 or ny >= w:
continue
if graph[nx][ny] == 1:
graph[nx][ny] = 0
q.append((nx,ny))
return
while True:
w,h = map(int, input().split())
if w == 0 and h == 0:
break
graph = []
for _ in range(h):
graph.append(list(map(int, input().split())))
cnt = 0
for i in range(h):
for j in range(w):
if graph[i][j] == 1:
bfs(i,j)
cnt += 1
print(cnt)
์ฒ์์ ๊ธฐ๋ณธ bfs๋ผ๊ณ ์๊ฐํ๊ณ ํ์๋๋ฐ
๋ฌธ์ ๋ฅผ ๋ค์ ์ฝ์ด๋ณด๋ “๋๊ฐ์ ”๋ ๊ฑธ์ด๊ฐ ์ ์๋ ์ฌ๊ฐํ์ด์์
dx = [1,0,0,-1,1,1,-1,-1] dy = [0,1,-1,0,1,-1,-1,1]
์ด๋ ๊ฒ ๋ฐ๊ฟ์ค์ผ๋ก์ ๋ฌธ์ ํด๊ฒฐ ~>>
728x90
'Algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ - python (1) | 2023.10.30 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ์ ํ๋ฒํธ ๋ชฉ๋ก - python (0) | 2023.10.30 |
[Baekjoon] 1926. ๊ทธ๋ฆผ - python (0) | 2023.10.03 |
[Baekjoon] 7490. 0 ๋ง๋ค๊ธฐ - python (1) | 2023.10.02 |
[Baekjoon] 14281. ๋ณผ๋ก ์์ด - python (0) | 2023.10.02 |