문제 링크
3184. 양
문제 코드
from collections import deque
row, col = list(map(int, input().split()))
board = [[0 for c in range(col)] for r in range(row)]
wolf_list = []
lamb_list = []
death_wolf = 0
death_lamb = 0
for r in range(row):
tmp_board = input()
for c in range(col):
board[r][c] = tmp_board[c]
if tmp_board[c] == 'v':
wolf_list.append([r, c])
if tmp_board[c] == 'o':
lamb_list.append([r,c])
visited = [[0 for c in range(col)] for r in range(row)]
for i in range(len(wolf_list)):
x, y = wolf_list[i]
if visited[x][y] == 1:
continue
que = deque()
que.append([x, y])
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
one_area = []
one_area.append([x, y])
visited[x][y] = 1
while len(que) > 0:
x, y = que.popleft()
for i in range(4):
next_x = x + dx[i]
next_y = y + dy[i]
if 0 <= next_x < row and 0 <= next_y < col and visited[next_x][next_y] == 0 and board[next_x][
next_y] != '#':
que.append([next_x, next_y])
one_area.append([next_x, next_y])
visited[next_x][next_y] = 1
lamb_count = 0
wolf_count = 0
for x, y in one_area:
if board[x][y] == 'o':
lamb_count += 1
if board[x][y] == 'v':
wolf_count += 1
if lamb_count > wolf_count:
death_wolf += wolf_count
else:
death_lamb += lamb_count
print(len(lamb_list) - death_lamb, len(wolf_list) - death_wolf)
문제 풀이
- 영역을 나누면 되는 문제
- 영역을 먼저 나누고, 해당 영역의 wolf와 lamb 숫자를 센다.
- lamb가 많으면 wolf가 죽고 , 반대면 lamb가 죽음
- 죽은만큼 처음 센 갯수에서 빼준다.