๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด๐ป/[๐๐ฒ๐ญ๐ก๐จ๐ง] ๐๐ฅ๐ ๐จ๐ซ๐ข๐ญ๐ก๐ฆ
[Python] ๋ฐฑ์ค 1743 & 1303
๐คRyusun๐ค
2023. 8. 27. 01:40
# ๋ฐฑ์ค 1743
from collections import deque
import sys
n, m, k = map(int, sys.stdin.readline().split())
graph = [[0]* m for _ in range(n)]
for _ in range(k):
a, b = map(int,sys.stdin.readline().split())
graph[a-1][b-1] = 1
d_row = [0, -1, 0, 1]
d_col = [1, 0, -1, 0]
def bfs(result, i, j):
queue = deque()
queue.append([i, j])
cnt = 0
while queue:
x, y = queue.popleft()
for next in range(4):
nx = x+ d_row[next]
ny = y + d_col[next]
if nx < 0 or ny < 0 or nx > n-1 or ny > m-1:
continue
elif graph[nx][ny] == 1 :
queue.append([nx, ny])
graph[nx][ny] = 0
cnt += 1
if cnt == 0:
cnt = 1
return result.append(cnt)
result = []
for i in range(n):
for j in range(m):
if graph[i][j] == 1:
bfs(result, i, j)
print(max(result))
#๋ฐฑ์ค 1303
import sys
from collections import deque
input = sys.stdin.readline
queue = deque()
m, n = map(int, input().split())
graph= [list(map(str, input().rstrip())) for _ in range(n)]
visited = [[False]* m for _ in range(n)]
d_row = [0, -1, 0, 1]
d_col = [1, 0, -1, 0]
def bfs(i, j, us_cnt, them_cnt):
queue.append([i, j])
visited[i][j] = True
graph[i][j] = graph[i][j].lower() #๋ฐฉ๋ฌธํ๋ฉด ํด๋น ์ํ๋ฒณ์ ์๋ฌธ์๋ก ๋ฐ๊พผ๋ค.
while queue:
x, y = queue.popleft()
for d in range(4):
dx = x + d_row[d]
dy = y + d_col[d]
if dx < 0 or dy < 0 or dx > n-1 or dy > m-1:
continue
if graph[x][y] == 'w' and graph[dx][dy] == 'W' and not visited[dx][dy]:
queue.append([dx, dy])
visited[dx][dy] = True
graph[dx][dy] = 'w'
us_cnt += 1
elif graph[x][y] == 'b' and graph[dx][dy] == 'B' and not visited[dx][dy]:
queue.append([dx, dy])
visited[dx][dy] = True
graph[dx][dy] = 'b'
them_cnt += 1
return us_cnt, them_cnt
us_list = []
them_list = []
for i in range(n):
for j in range(m):
if graph[i][j] == 'W':
result = bfs(i, j, 1, 0)[0]
us_list.append(result)
elif graph[i][j] == 'B':
result = bfs(i, j, 0, 1)[1]
them_list.append(result)
us_list = sum([x ** 2 for x in us_list])
them_list = sum([y ** 2 for y in them_list])
print(us_list, them_list)