Submission #76895373
Source Code Expand
from collections import deque
M = int(input())
graph = [[] for n in range(10)]
for m in range(M):
u, v = map(int, input().split())
graph[u].append(v)
graph[v].append(u)
p_list = list(map(int, input().split()))
e = 0
for n in range(1, 10):
if n not in p_list:
e = n
p_list.append(e)
p_info = tuple(p_list)
used = {}
used[p_info] = 0
queue = deque()
queue.append((p_info, 0))
while queue:
q = queue.popleft()
now = q[0]
c = q[1]
e = now[-1]
for neibor in graph[e]:
nex = []
for n in range(1, 10):
p = now[n - 1]
if p == neibor:
nex.append(e)
elif p == e:
nex.append(neibor)
else:
nex.append(p)
ne = tuple(nex)
if ne in used:
continue
else:
used[ne] = c + 1
queue.append((ne, c + 1))
# print(used)
goal = [n + 1 for n in range(9)]
goal = tuple(goal)
if goal not in used:
print(-1)
else:
print(used[goal])
Submission Info
| Submission Time | |
|---|---|
| Task | G - 8 Puzzle on Graph |
| User | kurome_rome |
| Language | Python (PyPy 3.11-v7.3.20) |
| Score | 400 |
| Code Size | 1106 Byte |
| Status | AC |
| Exec Time | 1915 ms |
| Memory | 294064 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 400 / 400 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | example0.txt, example1.txt, example2.txt, example3.txt |
| All | 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, example0.txt, example1.txt, example2.txt, example3.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 000.txt | AC | 75 ms | 93712 KiB |
| 001.txt | AC | 75 ms | 93704 KiB |
| 002.txt | AC | 1821 ms | 294064 KiB |
| 003.txt | AC | 1898 ms | 293952 KiB |
| 004.txt | AC | 1915 ms | 293896 KiB |
| 005.txt | AC | 1795 ms | 293704 KiB |
| 006.txt | AC | 1800 ms | 293700 KiB |
| 007.txt | AC | 454 ms | 185728 KiB |
| 008.txt | AC | 458 ms | 185000 KiB |
| 009.txt | AC | 458 ms | 186304 KiB |
| 010.txt | AC | 455 ms | 188012 KiB |
| 011.txt | AC | 478 ms | 186008 KiB |
| 012.txt | AC | 476 ms | 188792 KiB |
| 013.txt | AC | 479 ms | 186100 KiB |
| 014.txt | AC | 523 ms | 185880 KiB |
| 015.txt | AC | 75 ms | 93604 KiB |
| 016.txt | AC | 76 ms | 93836 KiB |
| 017.txt | AC | 75 ms | 93868 KiB |
| 018.txt | AC | 80 ms | 100364 KiB |
| 019.txt | AC | 80 ms | 100224 KiB |
| 020.txt | AC | 80 ms | 100456 KiB |
| 021.txt | AC | 809 ms | 275420 KiB |
| 022.txt | AC | 803 ms | 276404 KiB |
| 023.txt | AC | 787 ms | 276520 KiB |
| 024.txt | AC | 786 ms | 275988 KiB |
| 025.txt | AC | 75 ms | 93932 KiB |
| 026.txt | AC | 75 ms | 93932 KiB |
| 027.txt | AC | 76 ms | 93672 KiB |
| 028.txt | AC | 76 ms | 93980 KiB |
| 029.txt | AC | 76 ms | 93708 KiB |
| 030.txt | AC | 76 ms | 93832 KiB |
| 031.txt | AC | 75 ms | 93652 KiB |
| 032.txt | AC | 1358 ms | 293468 KiB |
| 033.txt | AC | 1086 ms | 290236 KiB |
| 034.txt | AC | 1402 ms | 293424 KiB |
| 035.txt | AC | 1318 ms | 290628 KiB |
| 036.txt | AC | 1283 ms | 290176 KiB |
| 037.txt | AC | 75 ms | 93832 KiB |
| 038.txt | AC | 96 ms | 109416 KiB |
| 039.txt | AC | 75 ms | 93968 KiB |
| 040.txt | AC | 75 ms | 93852 KiB |
| 041.txt | AC | 75 ms | 93852 KiB |
| example0.txt | AC | 75 ms | 94108 KiB |
| example1.txt | AC | 75 ms | 93876 KiB |
| example2.txt | AC | 486 ms | 185856 KiB |
| example3.txt | AC | 494 ms | 185748 KiB |