Submission #48410290


Source Code Expand

from collections import deque
from copy import deepcopy

H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]


def to_tuple(a):
    return tuple(tuple(row) for row in a)


dist = {to_tuple(A): 0}
q = deque([A])
while q:
    a = q.popleft()
    for i in range(H - 1):
        b = deepcopy(a)
        b[i], b[i + 1] = b[i + 1], b[i]
        if to_tuple(b) not in dist:
            dist[to_tuple(b)] = dist[to_tuple(a)] + 1
            q.append(b)
    for j in range(W - 1):
        b = deepcopy(a)
        for i in range(H):
            b[i][j + 1], b[i][j] = b[i][j], b[i][j + 1]
        if to_tuple(b) not in dist:
            dist[to_tuple(b)] = dist[to_tuple(a)] + 1
            q.append(b)
if to_tuple(B) not in dist:
    print(-1)
else:
    print(dist[to_tuple(B)])

Submission Info

Submission Time
Task D - Swapping Puzzle
User terrafarm
Language Python (PyPy 3.10-v7.3.12)
Score 425
Code Size 896 Byte
Status AC
Exec Time 698 ms
Memory 119160 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 425 / 425
Status
AC × 4
AC × 42
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, example0.txt, example1.txt, example2.txt, example3.txt
Case Name Status Exec Time Memory
000.txt AC 81 ms 82532 KiB
001.txt AC 81 ms 82300 KiB
002.txt AC 107 ms 84960 KiB
003.txt AC 81 ms 82712 KiB
004.txt AC 81 ms 82628 KiB
005.txt AC 136 ms 86820 KiB
006.txt AC 80 ms 82628 KiB
007.txt AC 80 ms 82380 KiB
008.txt AC 80 ms 82472 KiB
009.txt AC 80 ms 82504 KiB
010.txt AC 270 ms 93692 KiB
011.txt AC 225 ms 88852 KiB
012.txt AC 679 ms 115592 KiB
013.txt AC 80 ms 82448 KiB
014.txt AC 684 ms 118868 KiB
015.txt AC 688 ms 117724 KiB
016.txt AC 690 ms 117744 KiB
017.txt AC 97 ms 84356 KiB
018.txt AC 696 ms 118900 KiB
019.txt AC 690 ms 119132 KiB
020.txt AC 685 ms 118984 KiB
021.txt AC 104 ms 85084 KiB
022.txt AC 80 ms 82336 KiB
023.txt AC 81 ms 82572 KiB
024.txt AC 81 ms 82636 KiB
025.txt AC 80 ms 82272 KiB
026.txt AC 690 ms 116040 KiB
027.txt AC 698 ms 115456 KiB
028.txt AC 694 ms 115460 KiB
029.txt AC 83 ms 82540 KiB
030.txt AC 686 ms 117404 KiB
031.txt AC 694 ms 118956 KiB
032.txt AC 688 ms 117656 KiB
033.txt AC 136 ms 87048 KiB
034.txt AC 677 ms 118960 KiB
035.txt AC 697 ms 119160 KiB
036.txt AC 683 ms 118536 KiB
037.txt AC 111 ms 84524 KiB
example0.txt AC 233 ms 92044 KiB
example1.txt AC 79 ms 82292 KiB
example2.txt AC 90 ms 84236 KiB
example3.txt AC 684 ms 118964 KiB