[BOJ][๐ŸŸก3][๋ฐฑ์ค€#02479] ๊ฒฝ๋กœ ์ฐพ๊ธฐ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #2479


๋ฌธ์ œ

๊ธธ์ด๊ฐ€ ๊ฐ™์€ ๋‘ ๊ฐœ์˜ ์ด์ง„์ˆ˜ ์ฝ”๋“œ A์™€ B๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž. ์ด ๋‘ ์ฝ”๋“œ ์‚ฌ์ด์˜ ํ•ด๋ฐ ๊ฑฐ๋ฆฌ๋Š” A์™€ B์˜ ๊ฐ ๋น„ํŠธ๋ฅผ ์™ผ์ชฝ๋ถ€ํ„ฐ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ฐจ๋ก€๋Œ€๋กœ ๋น„๊ตํ•  ๋•Œ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ€์ง„ ๋น„ํŠธ์˜ ์ˆ˜์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, A=010010, B=011011 ์ด๋ผ๊ณ  ํ•˜๋ฉด, ์„ธ ๋ฒˆ์งธ ๋น„ํŠธ์™€ ์—ฌ์„ฏ ๋ฒˆ์งธ ๋น„ํŠธ๋งŒ ์„œ๋กœ ๋‹ค๋ฅด๋ฏ€๋กœ ์ด ๋‘ ์ฝ”๋“œ ์‚ฌ์ด์˜ ํ•ด๋ฐ ๊ฑฐ๋ฆฌ๋Š” 2์ด๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด N๊ฐœ์˜ ์ด์ง„ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ๊ฐ ์ฝ”๋“œ์˜ ๊ธธ์ด๋Š” K๋กœ ๊ฐ™๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ธธ์ด๊ฐ€ 3์ธ 5๊ฐœ์˜ ์ด์ง„ ์ฝ”๋“œ๋“ค์ด ์žˆ๋‹ค.

A=000 B=111 C=010 D=110 E=001

๋‘ ์ฝ”๋“œ A์™€ B์‚ฌ์ด์˜ ํ•ด๋ฐ ๊ฑฐ๋ฆฌ๋ฅผ H(A,B)๋กœ ํ‘œํ˜„ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด, H(A,B)=3, H(A,C)=1, H(A,D)=2, H(A,E)=1 ์ด๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด์ง„ ์ฝ”๋“œ๋“ค์— ๋Œ€ํ•ด ํ•ด๋ฐ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๊ณ ์ž ํ•œ๋‹ค. ํ•ด๋ฐ ๊ฒฝ๋กœ๋Š” ๋ชจ๋“  ์ธ์ ‘ํ•œ ๋‘ ์ฝ”๋“œ์‚ฌ์ด์˜ ํ•ด๋ฐ ๊ฑฐ๋ฆฌ๊ฐ€ 1์ธ ๊ฒฝ๋กœ์ด๋‹ค. ์œ„์˜ ์˜ˆ์—์„œ (A,C,D)๋Š” ์ฝ”๋“œ A์™€ C์˜ ํ•ด๋ฐ ๊ฑฐ๋ฆฌ๊ฐ€ 1์ด๊ณ , ์ฝ”๋“œ C์™€ D์˜ ํ•ด๋ฐ ๊ฑฐ๋ฆฌ๊ฐ€ 1์ด๋ฏ€๋กœ ํ•ด๋ฐ ๊ฒฝ๋กœ์ด๋‹ค. (A,E,B)๋Š” ์ฝ”๋“œ A์™€ E์˜ ํ•ด๋ฐ ๊ฑฐ๋ฆฌ๋Š” 1์ด์ง€๋งŒ, ์ฝ”๋“œ E์™€ B์˜ ํ•ด๋ฐ ๊ฑฐ๋ฆฌ๊ฐ€ 2์ด๋ฏ€๋กœ ํ•ด๋ฐ ๊ฒฝ๋กœ๊ฐ€ ์•„๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋Š” ์ฃผ์–ด์ง„ ๋‘ ์ฝ”๋“œ ์‚ฌ์ด์— ๊ฐ€์žฅ ์งง์€ ํ•ด๋ฐ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” ๋‘ ๊ฐœ์˜ ์ •์ˆ˜ N๊ณผ K๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค(3 โ‰ค N โ‰ค 1,000, 2 โ‰ค K โ‰ค 30). ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ์ค„๋งˆ๋‹ค ๊ธธ์ด๊ฐ€ K์ธ ์ด์ง„ ์ฝ”๋“œ๊ฐ€ ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ํ•˜๋‚˜์˜ ์ฝ”๋“œ๋Š” ๋นˆ์นธ์ด ์—†์ด ์ฃผ์–ด์ง„๋‹ค. ์ฝ”๋“œ๋“ค์€ ์ฃผ์–ด์ง„ ์ˆœ์„œ๋Œ€๋กœ 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ๋ฒˆํ˜ธ๋กœ ๊ตฌ๋ถ„๋œ๋‹ค. ์ฝ”๋“œ๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์—†๋‹ค. ๊ทธ ๋‹ค์Œ ์ค„์—๋Š” ํ•ด๋ฐ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๊ณ ์ž ํ•˜๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ๊ฐœ์˜ ์ฝ”๋“œ A์™€ B๊ฐ€ ๊ฐ๊ฐ์˜ ์ฝ”๋“œ๋ฒˆํ˜ธ๋กœ ์ฃผ์–ด์ง„๋‹ค.


์ถœ๋ ฅ

์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ๋‘ ์ฝ”๋“œ ์‚ฌ์ด์— ํ•ด๋ฐ ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜๋ฉด ๊ทธ ๊ฒฝ๋กœ ์ค‘ ๊ฐ€์žฅ ์งง์€ ๊ฒฝ๋กœ๋ฅผ ์ฝ”๋“œ A๋ถ€ํ„ฐ ์ฝ”๋“œ B๊นŒ์ง€ ๊ฒฝ๋กœ์ƒ์˜ ์ฝ”๋“œ ๋ฒˆํ˜ธ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. ์ฝ”๋“œ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•  ๊ฒฝ์šฐ์—๋Š” ์ฝ”๋“œ ๋ฒˆํ˜ธ ์‚ฌ์ด์— ํ•˜๋‚˜์˜ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ๋‹ต์ด ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์œผ๋ฉด ๊ทธ ์ค‘์— ํ•˜๋‚˜๋งŒ ์ถœ๋ ฅํ•˜๊ณ , ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

5 3
000
111
010
110
001
1 2


์ถœ๋ ฅ

1 3 4 2


My Sol

import sys
input = sys.stdin.readline
from collections import deque

T, L = map(int, input().split())
arr = []
for _ in range(T):
    arr.append(int(input()[::-1], 2))

mat = []
for i in range(T):
    num = 0
    for j in range(T):
        cnt = 0
        for k in range(L):
            if (arr[i]^arr[j]) & (1<<k):
                cnt += 1
                if cnt==2:
                    cnt = 0
                    break
        num += (1<<j)*cnt
    mat.append(num)

for i in range(L):
    mat[i] &= ~(1<<i)

S, E = map(int, input().split())
S, E = S-1, E-1

paths = deque()
paths.append([S])
Q = deque()
Q.append(S)
v = 0
while not v & (1<<E) and Q:
    s = Q.popleft()
    path = paths.popleft()
    for i in range(T):
        if v&(1<<i): continue
        if not mat[s]&(1<<i): continue
        v ^= (1<<i)
        if i==E:
            path += [i]
            for n in path:
                print(n+1, end=' ')
            quit()
        Q.append(i)
        paths.append(path+[i])
print(-1)

์‹œ๊ฐ„๊ณผ ์šฉ๋Ÿ‰์„ ๊ทน์ ์œผ๋กœ ์ค„์ด๊ธฐ ์œ„ํ•ด visited, mat 2์ฐจ์› ๋ฐฐ์—ด ๋“ฑ ์ฒดํฌ์šฉ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ€์šฉํ•œ ๋ชจ๋“  ๊ฒƒ์„ ๋น„ํŠธํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•˜์˜€๋‹ค.

์ตœ์ดˆ 1๊ณผ0์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ž…๋ ฅ ํ…์ŠคํŠธ๋ฅผ ๋ฐ˜์ „ํ•˜์—ฌ int(,2)๋ฅผ ์ด์šฉํ•ด 2์ง„์ˆ˜๋ฅผ 10์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ด ์ €์žฅํ•˜๊ณ , ์ด ๊ฐ๊ฐ์„ ์ธ๋ฑ์Šค์— ๋งž์ถฐ ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”์ง€ ๋น„ํŠธ๋ฅผ ํ†ตํ•ด ์ฒดํฌํ•˜์˜€๋‹ค.

์ž๊ธฐ ์Šค์Šค๋กœ์™€๋Š” ๋งํฌ๋  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ mat[i] &= ~(1<<i)๋ฅผ index๋งˆ๋‹ค ์ž‘์„ฑํ•˜์—ฌ 0์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.

Q๋ฅผ ์ด์šฉํ•ด ์•ž์—์„œ ์‹œ์ž‘์ ์„ ๋ฝ‘์•„๋‚ด๊ณ , ์ดํ›„ ์‹œ์ž‘์ ๊ณผ ์—ฐ๊ฒฐ๋œ ์ ๋“ค์„ ํ•˜๋‚˜ํ•˜๋‚˜ ํ™•์ธํ•ด ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋ฐฉ๋ฌธ ์ฒดํฌ๋ฅผ ํ•œ๋‹ค.

์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•ด์•ผํ•˜๋ฏ€๋กœ BFS๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์€๋ฐ, ๊ฒฝ๋กœ๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒดํฌํ•ด์•ผํ• ๊นŒ ํ•˜๋‹ค๊ฐ€ Q์™€ ๊ฐ™์ด ๋™์ž‘ํ•˜๋Š” paths ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด popleftํ•˜์—ฌ ์‹œ์ž‘์ ์˜ ๋„์ฐฉ์ ์„ ๋‹ด์•„ append ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ Q์™€ ์ˆœ์„œ๋ฅผ ๋งž์ถฐ์ฃผ์—ˆ๋‹ค. ๋•Œ๋ฌธ์— Q์—์„œ ๊บผ๋‚ธ ์‹œ์ž‘์ ๊ณผ ์—ฐ๊ฒฐ๋œ ์ ์ด ๋„์ฐฉ์ ์ด ์žˆ๋‹ค๋ฉด, ํ•จ๊ป˜ ๊บผ๋‚ด์˜จ path์— ๋„์ฐฉ์ ๊นŒ์ง€ ๋„ฃ์–ด์„œ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋˜๊ฒ ๋‹ค.

๊ฒฝ๋กœ๋Š” ์ธ๋ฑ์Šค ๋•Œ๋ฌธ์— ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋Š” ํ˜•์‹๋ณด๋‹ค 1์”ฉ ์ž‘์œผ๋ฏ€๋กœ for๋ฌธ์„ ์ด์šฉํ•ด 1์”ฉ ํ‚ค์›Œ์„œ ์ถœ๋ ฅํ•ด์ฃผ์—ˆ๋‹ค. ๋งŒ์•ฝ Q๊ฐ€ ๋™๋‚  ๋•Œ๊นŒ์ง€ ์ถœ๋ ฅํ•˜์ง€ ๋ชปํ•˜๋ฉด, -1์„ ์ถœ๋ ฅํ•˜๊ณ  ๋งˆ์นœ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

# 2479 ๊ฒฝ๋กœ์ฐพ๊ธฐ
from sys import stdin
import sys
from collections import defaultdict
def dfs(now, road):
    if now == e:
        print(*road)
        sys.exit()
    
    for i in range(k):
        temp = list(ham[now])
        temp[i] = str(abs(1- int(temp[i])))
        temp = ''.join(temp)
        if temp in ham and ham[temp][0] == 0:
            ham[temp][0] = 1
            dfs(ham[temp][1], road + [ham[temp][1]])
            ham[temp][0] = 0

n, k = map(int, stdin.readline().split())
ham = defaultdict(list)
for i in range(n):
    ham[i+1] = ''.join(list(stdin.readline().rstrip()))
    ham[ham[i+1]] = [0, i+1]
s, e = map(int, stdin.readline().split())
dfs(s, [s])
print(-1)

์ฝ”๋“œ๊ธธ์ด๊ฐ€ ์งง์•„์„œ ๊ฐ€์ ธ์˜จ ํ’€์ด์ด๋‹ค. ๋‚˜๋Š” BFS๋ฅผ ์ด์šฉํ–ˆ๋Š”๋ฐ, ์ด ๋ถ„์€ DFS๋ฅผ ์ด์šฉํ•ด ํ‘ธ์…จ๋‹ค. defaultdict๋ผ๋Š” ๋ชจ๋“ˆ์„ importํ•ด ์‚ฌ์šฉํ•˜์…จ๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ•œ ์ข…๋ฅ˜์ธ ๊ฒƒ ๊ฐ™๋‹ค. ๊ฒฝ๋กœ ์„ค์ •์€ ์ธ์ž๋กœ ๋„ฃ์–ด์„œ ๋งค๋ฒˆ ์ „๋‹ฌํ•ด์ฃผ์—ˆ๋Š”๋ฐ๋„ ์†๋„๊ฐ€ ๋นจ๋ž๋‹ค. visited ์ฒดํฌ๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ํŠน์ดํ•˜๊ฒŒ ํ•˜์…จ๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ