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 | 31 |
Tags
- 트리
- MST
- 자료구조
- 분할정복
- 서브쿼리
- DFS
- 다이나믹프로그래밍
- 구현
- 플로이드-워셜
- GROUP BY
- BFS
- 재귀
- join
- 브루트포스
- DP
- 다이나믹 프로그래밍
- 그래프 탐색
- 투포인터
- 다시
- 해시
- 크루스칼
- 누적합
- 에라토스테네스의 체
- 그리디
- 다익스트라
- 우선순위큐
- 시뮬레이션
- 수학
- 백트래킹
- 그래프 이론
Archives
- Today
- Total
기록하고 까먹지 말기
1303 본문
날짜 : 2023. 03. 30
사용 언어 : python
문제
코드
import sys
from collections import deque
n, m = map(int, sys.stdin.readline().split()) # 가로, 세로
graph = list()
visited = [[False] * n for _ in range(m)]
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
def bfs(a, b, team):
cnt = 1
q = deque()
q.append([a, b])
while q:
r, c = q.popleft()
for i in range(4):
x = r + dx[i]
y = c + dy[i]
if x in range(m) and y in range(n): # 범위에 포함
if not visited[x][y] and graph[x][y] == team: # 방문 안헸고, 같은 팀
cnt += 1
visited[x][y] = True # 방문처리
q.append([x, y]) # 큐에 포함
#print(team, cnt ** 2)
return cnt ** 2
for _ in range(m):
graph.append(list(sys.stdin.readline().rstrip()))
"""for i in graph:
print(i)"""
total_blue, total_white = 0, 0
for i in range(m):
for j in range(n):
if not visited[i][j]:
visited[i][j] = True
if graph[i][j] == "B":
total_blue += bfs(i, j, "B")
else:
total_white += bfs(i, j, "W")
print(total_white, total_blue)
풀이
- 각 팀을 입력받은 후 방문하지 않은 위치의 팀에 따라 bfs를 실행한 후 연관된 그룹의 인원 명수를 제곱한 값을 리턴함으로써 각 팀의 전투력을 가산한다.
- 전부 방문처리한 이후 그 값들을 출력한다.
알게된 점
-
참고 사이트
-