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
- fe
- HTML
- error
- ์ฝ๋ฉํ ์คํธ
- KDT
- ํฌ๋กค๋ง
- heapq
- ํ์ด์ฌ
- ํ๋ก์ ํธ
- Til
- Python
- ์ฝ๋ฉ
- ๋๋ฆผ์ฝ๋ฉ
- ๊ฐ๋ฐ
- react
- ๋ ธ๋ง๋์ฝ๋
- ์ฝ๋ฉ์ ํ
- ํ ์ดํ๋ก์ ํธ
- ๊ตญ๋น์ง์
- mongodb
- ๋ชจ๊ฐ์ฝ
- ํ๋ก ํธ์๋
- ์๊ณ ๋ฆฌ์ฆ
- javascript
- node.js
- CSS
- ๊ทธ๋ฆฌ๋
- ๋ฐฑ์ค
- ํ๋ก๊ทธ๋๋จธ์ค
- JS
Archives
- Today
- Total
๐ฑ โ ๐ณ
[Baekjoon] 4963. ์ฌ์ ๊ฐ์ - python ๋ณธ๋ฌธ
728x90
https://www.acmicpc.net/problem/4963
4963๋ฒ: ์ฌ์ ๊ฐ์
์ ๋ ฅ์ ์ฌ๋ฌ ๊ฐ์ ํ ์คํธ ์ผ์ด์ค๋ก ์ด๋ฃจ์ด์ ธ ์๋ค. ๊ฐ ํ ์คํธ ์ผ์ด์ค์ ์ฒซ์งธ ์ค์๋ ์ง๋์ ๋๋น w์ ๋์ด h๊ฐ ์ฃผ์ด์ง๋ค. w์ h๋ 50๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ์์ ์ ์์ด๋ค. ๋์งธ ์ค๋ถํฐ h๊ฐ ์ค์๋ ์ง๋
www.acmicpc.net
์ฝ๋
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 |