๐ 12906 ์๋ก์ด ํ๋ ธ์ด ํ ๐
๐ค ๋ฌธ์ ํ์ด ๐ค
pypy3๋ก ์ฑ์ ์ ํ๋ฉด ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ ๋ฐ์!
์ด ๋ฌธ์ ์ ํต์ฌ์ ์ด๋ป๊ฒ ๋ฐฉ๋ฌธ ์ฒดํฌ๋ฅผ ํ ๊ฒ์ธ์ง๋ฅผ ํ์ ํ๋ ๊ฒ์ด๋ค.
๊ธฐ๋ณธ์ ์ธ BFS์ฒ๋ผ ๋ฐฐ์ด๋ก ๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ํ ์๊ฐ ์๋ค.
๊ฐ๊ฐ์ ๊ธฐ๋ฅ์ ๋ค์ด๊ฐ์๋ ์ํ์ "/"์ผ๋ก ๊ตฌ๋ถ์์ผ ํ๋์ ๋ฌธ์์ด๋ก ๋ง๋ ๋ค.
์ด ํ set()๋ dict()๋ฅผ ์ด์ฉํด ๋ฌธ์์ด์ ์ค๋ณต ๋ฐฉ๋ฌธ ์ฌ๋ถ๋ฅผ ํ์ธํด์ฃผ๋ฉด ๋๋ค.
โจ๏ธ ์ฝ๋ โจ๏ธ
import collections
arr = [input()[2:] for _ in range(3)]
#visited = set()
visited = collections.defaultdict(int)
q = collections.deque()
q.append((arr[0], arr[1], arr[2], 0))
while q:
a, b, c, count = q.popleft()
cont_str = a + "/" + b + "/" + c
if 'B' not in a and 'C' not in a:
if 'A' not in b and 'C' not in b:
if 'A' not in c and 'B' not in c:
print(count)
exit(0)
#if not cont_str in visited:
#visited.add(cont_str)
if not visited[cont_str]:
visited[cont_str] += 1
if len(a) > 0:
q.append((a[:-1], b + a[-1], c, count + 1))
q.append((a[:-1], b, c + a[-1], count + 1))
if len(b) > 0:
q.append((a, b[:-1], c + b[-1], count + 1))
q.append((a + b[-1], b[:-1], c, count + 1))
if len(c) > 0:
q.append((a, b + c[-1], c[:-1], count + 1))
q.append((a + c[-1], b, c[:-1], count + 1))
print(-1)
Reference