우당탕탕 개발일지
[백준] 11123. 양 한마리... 양 두마리... (실버2, Python) 본문
반응형
풀이
최종 코드
import queue
def findSheep(x, y):
sheepQueue = queue.Queue()
sheepCheck[x][y] = True
sheepQueue.put([x, y])
# 앞뒤위아래
moveXArr = [1, -1, 0, 0]
moveYArr = [0, 0, -1, 1]
while sheepQueue.qsize() > 0:
node = sheepQueue.get()
for i in range(len(moveXArr)):
moveX = node[0] + moveXArr[i]
moveY = node[1] + moveYArr[i]
if moveX >= h or moveY >= w:
continue
if moveX < 0 or moveY < 0:
continue
if sheepInfo[moveX][moveY] == '#' and not sheepCheck[moveX][moveY]:
sheepCheck[moveX][moveY] = True
sheepQueue.put([moveX, moveY])
tc = int(input())
for t in range(tc):
h, w = map(int, input().split())
sheepInfo = [[] * w for _ in range(h)]
sheepCheck = [[False] * w for _ in range(h)]
result = 0
for x in range(h):
for s in input():
sheepInfo[x].append(s)
for x in range(h):
for y in range(w):
if not sheepCheck[x][y] and sheepInfo[x][y] == '#':
findSheep(x, y)
result +=1
print(result)

백준 문제
https://www.acmicpc.net/problem/11123
GitHub
https://github.com/ujin302/CodingTest_2023/blob/main/Code/BackJun/silver/s2/s2_11123.py
반응형
'코테 > 백준' 카테고리의 다른 글
| [백준] 1991. 트리 순회 (실버1, Python) (0) | 2025.09.09 |
|---|---|
| [백준] 2606. 바이러스 (실버3, Python) (1) | 2025.07.02 |
| [백준] 4963. 섬의 개수 (실버2, Python) (1) | 2025.06.26 |
| [백준] 11725. 트리의 부모 찾기 (실버2, Python) (0) | 2025.06.24 |
| [백준] 1012. 유기농 배추 (실버2, Python) (2) | 2025.06.24 |