https://www.acmicpc.net/problem/1303
1303번: 전쟁 - 전투
첫째 줄에는 전쟁터의 가로 크기 N, 세로 크기 M(1 ≤ N, M ≤ 100)이 주어진다. 그 다음 두 번째 줄에서 M+1번째 줄에는 각각 (X, Y)에 있는 병사들의 옷색이 띄어쓰기 없이 주어진다. 모든 자리에는
www.acmicpc.net
https://nodingco.tistory.com/138?category=516041
[Java] 백준 1303번. 전쟁-전투 (실버1)
https://www.acmicpc.net/problem/1303 1303번: 전쟁 - 전투 첫째 줄에는 전쟁터의 가로 크기 N, 세로 크기 M(1 ≤ N, M ≤ 100)이 주어진다. 그 다음 두 번째 줄에서 M+1번째 줄에는 각각 (X, Y)에 있는 병사들..
nodingco.tistory.com
위 풀이의 Python 버전입니다. 크게 다를건 없고 Python이 코드라인이 훨씬 적습니다.
import sys
from collections import deque
nm = sys.stdin.readline().split(' ')
M = int(nm[0])
N = int(nm[1])
B, W = 0, 0
maps = []
delta = [[-1, 0],[1, 0],[0, -1],[0, 1]]
for n in range(N):
maps.append(list(sys.stdin.readline()))
for n in range(N):
for m in range(M):
maps[n][m] = 0 if maps[n][m] == 'W' else 1
for n in range(N):
for m in range(M):
if maps[n][m] in [0, 1]:
target = maps[n][m]
count = 0
queue = deque()
queue.append([n, m])
maps[n][m] = -1
while len(queue) > 0:
x, y = queue.popleft()
count += 1
for i in range(4):
nx = x + delta[i][0]
ny = y + delta[i][1]
if 0 <= nx < N and 0 <= ny < M and maps[nx][ny] == target:
queue.append([nx,ny])
maps[nx][ny] = -1
count *= count
if target == 0:
W += count
else:
B += count
print(f"{W} {B}")
'🔍 알고리즘 > 백준 Python' 카테고리의 다른 글
[Python] 백준 1026번. 보물 (실버2) (0) | 2022.06.30 |
---|---|
[Python] 백준 9084번. 동전 (골드 5) (0) | 2022.01.26 |
[Python] 백준 15810번. 풍선공장 (실버2) (0) | 2022.01.13 |
[Python] 백준 10610번. 30 (실버5) (0) | 2022.01.11 |