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

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

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

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

22352๋ฒˆ - ํ•ญ์ฒด ์ธ์‹

๊ทธ๋ž˜ํ”„๊ฐ€ ๋‘ ๊ฐœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค (ํ•ญ์ฒด๋ฅผ ๋†“๊ธฐ ์ „, ํ•ญ์ฒด๋ฅผ ๋†“์€ ํ›„)
์ €ํฌ๋Š” ์ด ๋‘ ๊ฐœ์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณด๊ณ  ๋†“์€ ํ•ญ์ฒด๊ฐ€ CPCU-1202 ๋ฐฑ์‹ ์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํŒ๋ณ„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

CPCU-1202๋Š” ํˆฌ์•ฝํ•˜๋ฉด ํ˜„์žฌ ์†ํ•ด์žˆ๋Š” ๊ฐ’๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์ƒํ•˜์ขŒ์šฐ์— ํ•ด๋‹น ๊ฐ’์ด ์žˆ์„ ๊ฒฝ์šฐ, ๊ทธ ์นธ์œผ๋กœ ํผ์ ธ๋‚˜๊ฐ‘๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ตœ์ข…์ ์œผ๋กœ ๋” ํผ์ ธ๋‚˜๊ฐˆ ๊ณต๊ฐ„์ด ์—†๋‹ค๋ฉด ํŠน์ •ํ•œ ๊ฐ’์œผ๋กœ ์—…๋ฐ์ดํŠธ๋ฉ๋‹ˆ๋‹ค.


์ž…๋ ฅ

N,M = map(int,input().split())
before = []
after = []
que = deque()

๋จผ์ € ์ž…๋ ฅ์„ ๋ฐ›์•„์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.
ํ•ญ์ฒด ํˆฌ์•ฝ์ „ before, ํ•ญ์ฒด ํˆฌ์•ฝ ํ›„ after ๊ทธ๋ž˜ํ”„๋กœ ์ž…๋ ฅ๋ฐ›๊ฒ ์Šต๋‹ˆ๋‹ค.

for _ in range(N):
    before.append(list(map(int,input().split())))
for _ in range(N):
    after.append(list(map(int,input().split())))

๋ฐ˜๋ณตํ•ด์„œ ์ด ๋‘ ๋ฒˆ ๋ฐ›์•„์ค๋‹ˆ๋‹ค.


๋กœ์ง

์ œ๊ฐ€ ์ƒ๊ฐํ•œ ํ•ด๋‹น ๋ฌธ์ œ์˜ ๋กœ์ง์€ ์ด๋ ‡์Šต๋‹ˆ๋‹ค.
์ผ๋‹จ ํ•ญ์ฒด๋ฅผ ํˆฌ์•ฝํ•˜๊ธฐ ์ „๊ณผ ํˆฌ์•ฝํ•œ ํ›„์˜ ๊ทธ๋ž˜ํ”„์—์„œ ๋ณ€ํ™”ํ•œ ๊ตฌ๊ฐ„์„ ์ฐพ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ์ธ๋ฑ์Šค๋ฅผ ๋„์ถœํ•ด์„œ ํˆฌ์•ฝํ•œ ํ›„์˜ ํ•ด๋‹น ์ธ๋ฑ์Šค๋ฅผ ํˆฌ์•ฝ ์ „์˜ ํ•ด๋‹น ์ธ๋ฑ์Šค์— ๋Œ€์ž…ํ•˜๊ณ 
๋‘˜์ด ๋น„๊ตํ•ด์„œ ๊ทธ๋ž˜ํ”„๊ฐ€ ์ผ์น˜ํ•˜๋ฉด CPCU-1202, ์•„๋‹ˆ๋ผ๋ฉด ๋‹ค๋ฅธ ํ•ญ์ฒด๊ฐ€ ํˆฌ์ž…๋œ ๊ฒ๋‹ˆ๋‹ค.

changed = []
loaded = False
for i in range(N):
    for j in range(M):
        if before[i][j] != after[i][j]:
           if after[i][j] not in changed:

๋”ฐ๋ผ์„œ ํˆฌ์•ฝ ์ „๊ณผ ํˆฌ์•ฝ ํ›„์˜ ๋ณ€ํ™”ํ•œ ๊ตฌ๊ฐ„์„ ์ฐพ๊ณ  ๋งŒ์•ฝ ๋ณ€ํ™”ํ•œ ๊ตฌ๊ฐ„ ํƒ์ƒ‰์„ ์•ˆ๊ฐ”์œผ๋ฉด

                loaded = True
                before_changed = before[i][j]
                changed.append(after[i][j])
                bfs(i,j)
                break

๋ณ€ํ™”ํ•œ ๊ตฌ๊ฐ„์—์„œ ๋ฐ”๋€Œ๊ธฐ ์ „ value๋Š” before_changed์—, ๋ฐ”๋€ ํ›„์˜ value๋Š” changed์— ๋„ฃ๊ณ  ํ•ด๋‹น ์ธ๋ฑ์Šค๋ฅผ BFS ํƒ์ƒ‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

    if loaded:
        break

loaded๋กœ True/False ์ฒดํฌ๋ฅผ ์•ˆํ•ด์ฃผ๊ฒŒ ๋˜๋ฉด ํ•œ ๋ฒˆ๋งŒ ๋Œ์•„๋„ ๋˜๋Š” BFS ํƒ์ƒ‰์„ ๋‘ ๋ฒˆ ์„ธ ๋ฒˆ ๋Œ์•„ ๊ฐ’์ด ์˜ค์—ผ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


ํƒ์ƒ‰

๊ธฐ์ค€์ ์€ before ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค.
์œ„์— ๋ง์”€๋“œ๋ ธ๋‹ค์‹œํ”ผ

after ๊ทธ๋ž˜ํ”„์˜ ๋ณ€ํ™”๋œ ๊ตฌ๊ฐ„์˜ value๋ฅผ before ๊ทธ๋ž˜ํ”„์˜ ๋ณ€ํ™”๋œ ๊ตฌ๊ฐ„์— ๋Œ€์ž…ํ•œ ๊ฐ’
<=>
after ๊ทธ๋ž˜ํ”„

์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ ์ผ์น˜ํ•ด์•ผ CPCU-1202๋ฅผ ํˆฌ์•ฝํ–ˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

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

BFS ์‚ฌ์šฉ์„ ์œ„ํ•ด ์ƒํ•˜์ขŒ์šฐ ์„ค์ •๊ณผ que๋ฅผ ๋ถˆ๋Ÿฌ์™€์ฃผ๊ณ 

def bfs(x,y):
    global before_changed
    boolean = False
    que.append([x,y])

que์— ์ถ”๊ฐ€ํ•˜๋ฉด์„œ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

    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>=M:
                continue

ํ•ญ์ƒ ํ•˜๋Š” 4๋ฐฉํ–ฅ ํƒ์ƒ‰์„ ์‹ค์‹œํ•ด์ฃผ๋˜

            if before[nx][ny] == before_changed:
                boolean = True
                before[nx][ny] = changed[0]
                que.append([nx,ny])

๋งŒ์•ฝ ํƒ์ƒ‰๊ตฌ๊ฐ„์ด ๋ณ€ํ™”๋œ ๊ตฌ๊ฐ„์ด๋ผ๋ฉด after ๊ทธ๋ž˜ํ”„์—์„œ ๋ณ€ํ™”๋œ ๊ฐ’์„ before ๊ทธ๋ž˜ํ”„์˜ ๋ณ€ํ™”๊ตฌ๊ฐ„์— ๋Œ€์ž…ํ•ด์ฃผ๋„๋ก ํ•ฉ์‹œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ๊ตฌ๊ฐ„์€ que์— ์ถ”๊ฐ€์‹œ์ผœ์„œ ์ฃผ๋ณ€ ํƒ์ƒ‰์„ ์žฌ์‹ค์‹œํ•ฉ๋‹ˆ๋‹ค.

    if boolean == False:
        before[x][y] = changed[0]

์ถ”๊ฐ€์ ์œผ๋กœ ํƒ์ƒ‰์ด ์•„์˜ˆ ์‹คํ–‰๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ํ•ญ์ฒด๊ฐ€ ํผ์งˆ ๊ณต๊ฐ„์ด ์กด์žฌํ•˜์ง€ ์•Š์•˜๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ ํ•ด๋‹น ์œ„์น˜๋งŒ ๋ณ€ํ™”๋œ ๊ฐ’์„ ๋Œ€์ž…ํ•ด์ฃผ๋„๋ก ํ•ฉ์‹œ๋‹ค.


์ถœ๋ ฅ

if before == after:
    print('YES')
else:
    print('NO')

๋งˆ์ง€๋ง‰์œผ๋กœ ๊ทธ๋ž˜ํ”„๊ฐ€ ์ผ์น˜ํ•˜๋ฉด YES, ์•„๋‹ˆ๋ผ๋ฉด NO๋ฅผ ์ถœ๋ ฅ์‹œํ‚ค๋ฉด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


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

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