๐Ÿ‘ฉโ€๐ŸŽ“๊ณต๋ถ€ - 2024.01.01 ์˜ค๋Š˜์˜ ๋ฐฑ์ค€ ๋ฌธ์ œํ’€์ด

์œ ๋ น๊ฐœยท2024๋…„ 1์›” 1์ผ
0

PS-์•Œ๊ณ ๋ฆฌ์ฆ˜2

๋ชฉ๋ก ๋ณด๊ธฐ
6/34
post-thumbnail

์ƒˆํ•ด๊ฐ€ ๋ฐ์•˜์Šต๋‹ˆ๋‹ค!
๋‹ค๋“ค ์ƒˆํ•ด๋ณต ๋งŽ์ด ๋ฐ›์œผ์‹œ๊ณ  ํ•˜์‹œ๋Š” ์ผ ๋‹ค ์ž˜๋˜์‹œ๊ธธ ๊ธฐ์›ํ•ฉ๋‹ˆ๋‹ค! 24๋…„์—” ์ฝ”๋”ฉ์‹ค๋ ฅ ๋งŽ์ด ์˜ค๋ฅด๊ฒŒ ํ•ด์ฃผ์„ธ์š” ใ…Žใ…Ž...

10026๋ฒˆ - ์ ๋ก์ƒ‰์•ฝ

์œ„์™€๊ฐ™์ด 3์ƒ‰ ๋ฐฐ์—ด ๊ทธ๋ฆผ์ด ์ฃผ์–ด์ง€๋ฉด ๊ตฌ์—ญ(๋™์ผํ•œ ๊ฐ ์ƒ‰๊น”์ด ๋ญ‰์ณ์žˆ๋Š” ๊ณณ)์ด ์ด ๋ช‡ ๊ฐœ์ธ์ง€ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
๋‹จ, ์ƒ‰์•ฝ์ธ ์‚ฌ๋žŒ์„ ์œ„ํ•ด์„œ R๊ณผ G๋ฅผ ๋™์ผํ•˜๊ฒŒ ์ทจ๊ธ‰ํ•˜์—ฌ ๊ตฌํ•œ ๊ฒฐ๊ณผ ๋˜ํ•œ ์ถœ๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


์„ธํŒ…

from collections import deque
import copy
dx = [-1,1,0,0]
dy = [0,0,-1,1]

๋ณด์ž๋งˆ์ž BFS๋กœ ํ’€์–ด์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.
ํ•ญ์ƒ BFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋ณธ์ ์ธ ์„ธํŒ…, ์ƒํ•˜์ขŒ์šฐ ์„ ์–ธ๊ณผ que๋ฅผ ๋ถˆ๋Ÿฌ์™€์ค๋‹ˆ๋‹ค.

N = int(input())
que = deque()
graph = []
for _ in range(N):
    graph.append(list(input()))
visited = [[0]*N for _ in range(N)]
graph_sakyak = copy.deepcopy(graph)

๊ทธ๋ฆผ์˜ ์ƒ‰ ์ •๋ณด๋ฅผ ๋ฐ›์•„์ค„ graph ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ  ํ•ด๋‹น ์ธ๋ฑ์Šค๋ฅผ ๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€ ํ‘œ๊ธฐํ•ด์ค„ visited ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ํ˜„์žฌ ์ดˆ๊ธฐ๊ฐ’ graph์™€ ๋™์ผํ•œ graph_sakyak์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ๋‚˜์ค‘์— ์ƒ‰์•ฝ์ธ ๊ทธ๋ž˜ํ”„๋ฅผ ํŒ๋ณ„ํ•ด์ฃผ๋„๋ก ํ•ฉ์‹œ๋‹ค.


์ƒ‰์•ฝ์ด ์•„๋‹ ๋•Œ

๋จผ์ € ์ƒ‰์•ฝ์ด ์•„๋‹Œ ๊ฒฝ์šฐ์˜ ๊ตฌ์—ญ์„ ๊ตฌํ•ด์ฃผ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

sum = 0
for i in range(N):
    for j in range(N):
        if not visited[i][j]:
            sum += 1
            bfs(i,j)

๋งŒ์•ฝ ํ•ด๋‹น ๊ตฌ์—ญ ๋ฐฉ๋ฌธ์„ ์•ˆํ–ˆ์œผ๋ฉด ๊ตฌ์—ญ ๋ฐฉ๋ฌธ ํšŸ์ˆ˜(sum)์„ ๋†’์ด๊ณ  ํ•ด๋‹น ๊ตฌ์—ญ์„ ์ „๋ถ€ ํƒ์ƒ‰ํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

        if graph_sakyak[i][j] == 'G':
            graph_sakyak[i][j] = 'R'

์ƒ‰์•ฝ์ธ ๊ฒฝ์šฐ๋Š” G๊ฐ€ R๋กœ ๋ณด์ด๊ฑฐ๋‚˜ R์ด G๋กœ ๋ณด์ด๊ธฐ ๋–„๋ฌธ์— ์“ธ๋ฐ์—†๋Š” for๋ฌธ ๋‚ญ๋น„๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด์„œ ์—ฌ๊ธฐ์„œ graph_sakyak์— R๊ณผ G๋ฅผ ๋™์ผํ•˜๊ฒŒ ๋งŒ๋“ค์–ด ์ƒ‰๋งน ํšจ๊ณผ๋ฅผ ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

def bfs(x,y):
    que.append([x,y])
    while que:
        x,y = que.popleft()
        for i in range(4):
            nx = dx[i] + x
            ny = dy[i] + y
            if nx<0 or ny<0 or nx>=N or ny>=N:
                continue 

๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ธ๋ฑ์Šค์˜ 4๋ฐฉํ–ฅ ํƒ์ƒ‰์„ ๋Œ์•„์ค๋‹ˆ๋‹ค.

            if graph[x][y] == 'R' and graph[nx][ny] == 'R' and not visited[nx][ny]:
                visited[nx][ny] = 1
                que.append([nx,ny])
            if graph[x][y] == 'G' and graph[nx][ny] == 'G' and not visited[nx][ny]:
                visited[nx][ny] = 1
                que.append([nx,ny])
            if graph[x][y] == 'B' and graph[nx][ny] == 'B' and not visited[nx][ny]:
                visited[nx][ny] = 1
                que.append([nx,ny])

๋งŒ์•ฝ ์ž๊ธฐ ์ž์‹ ์ด ํ•ด๋‹น ์ƒ‰๊น”์ด๊ณ  ๊ทธ ๋‹ค์Œ ๋‚˜์˜ฌ ๊ฒƒ๋„ ํ•ด๋‹น ์ƒ‰๊น”์ด๋ฉด ๊ทธ๊ฑด ๊ณง ๊ตฌ์—ญ์ด ํ˜•์„ฑ๋˜๋ฏ€๋กœ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ํ‘œ์‹œํ•ด์ฃผ๊ณ  ๋‹ค์Œ ๋‚˜์˜ฌ ์ธ๋ฑ์Šค๋„ ํƒ์ƒ‰์— ๋„ฃ์–ด์ฃผ๋„๋ก ํ•ฉ์‹œ๋‹ค.

์ž๊ธฐ ์ž์‹ ์ด if๋ฌธ ์กฐ๊ฑด์— ๋“ค์–ด๊ฐ„๋‹ค๋ฉด R->R ํƒ์ƒ‰๋งŒ ๊ฐ€๋Šฅํ•ด R->B ๋˜๋Š” R->G์™€ ๊ฐ™์ด ํƒ์ƒ‰ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์—†์•  ํ•ด๋‹น ๊ตฌ์—ญ๋งŒ ํƒ์ƒ‰์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.


์ƒ‰์•ฝ์ผ ๋•Œ

์ƒ‰์•ฝ์ผ ๋•Œ๋Š” ์•„๊นŒ ๋งŒ๋“ค์–ด์ค€ graph_sakyak์„ graph์— ๋ง์”Œ์›Œ์„œ ๋‹ค์‹œ bfs ํƒ์ƒ‰์„ ๋Œ๋ฆฌ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

visited = [[0]*N for _ in range(N)]
graph = graph_sakyak[:]
sum2 = 0

์ด๋Ÿฐ์”ฉ์œผ๋กœ์š”.
์ฐธ๊ณ ๋กœ graph_sakyak์„ ๋งŒ๋“ค ๋•Œ๋ž‘ graph์— ๋ง์”Œ์šธ๋•Œ๋ž‘ ๋ณต์ œ ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅธ ์ด์œ ๋Š” shallow copy๋ž‘ deep copy ๊ธฐ๋ฒ• ๋•Œ๋ฌธ์— ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ๊นŠ์€ ๋ณต์‚ฌ๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ฉด ์›๋ณธ ๋ฐฐ์—ด์— ์˜ํ–ฅ์„ ๋ฐ›์•„์„œ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์˜ค์—ผ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

for k in range(N):
    for p in range(N):
        if not visited[k][p]:
            sum2 += 1
            bfs(k,p)

๋ฐฉ๊ธˆ ๋ง์”€๋“œ๋ ธ๋“ฏ์ด graph_sakyak์„ ๋ง์”Œ์šด graph๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ BFS๋ฅผ ๋Œ๋ ค์ฃผ๋„๋ก ํ•ฉ์‹œ๋‹ค.


print(sum,sum2)

๋งˆ์ง€๋ง‰์œผ๋กœ ๊ตฌ์—ญ ํ•ฉ์‚ฐํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ์ •์ƒ์ ์œผ๋กœ ๋‹ต์„ ๋„์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


profile
ํ•œ๋ฆผ๋Œ€ํ•™๊ต ์ •๋ณด๊ณผํ•™๋Œ€ 2ํ•™๋…„ ์žฌํ•™์ค‘ / ์œก๊ตฐ ์ •๋ณด๋ณดํ˜ธ๋ณ‘ 22-2๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€