Submission #10881245


Source Code Expand

Copy
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def solve(A,B,C,D,E=3**30):
    # 大きさEの穴まで考慮
    if not E:
        return abs(A-C) + abs(B-D)
    a = A//E
    b = B//E
    c = C//E
    d = D//E
    if a != c and b != d:
        return abs(A-C) + abs(B-D)
    if a == c and b == d:
        return solve(A,B,C,D,E//3)
    if b == d and a != c:
        A,B = B,A
        a,b = b,a
        C,D = D,C
        c,d = d,c
    if a == c and b != d:
        if a % 3 != 1:
            return solve(A,B,C,D,E//3)
        if abs(b-d) == 1:
            return solve(A,B,C,D,E//3)
        if B > D:
            A,B,C,D = C,D,A,B
            a,b,c,d = c,d,a,b
        x1 = E * a - 1
        y1 = E * (b+1) - 1
        y2 = E * d
        dist1 = abs(x1-A) + abs(y1-B) + abs(y1-y2) + abs(x1-C) + abs(y2-D)
        x1 = E * (a+1)
        y1 = E * (b+1) - 1
        y2 = E * d
        dist2 = abs(x1-A) + abs(y1-B) + abs(y1-y2) + abs(x1-C) + abs(y2-D)
        return min(dist1, dist2)

solve(2,1,3,1)

N = int(readline())
m = map(int,read().split())
for A,B,C,D in zip(m,m,m,m):
    print(solve(A-1,B-1,C-1,D-1,3**30))

Submission Info

Submission Time
Task F - Fractal Shortest Path
User maspy
Language Python (3.4.3)
Score 600
Code Size 1235 Byte
Status AC
Exec Time 150 ms
Memory 5788 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 1
AC × 22
Set Name Test Cases
Sample example0.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, example0.txt
Case Name Status Exec Time Memory
000.txt AC 150 ms 4952 KB
001.txt AC 133 ms 5788 KB
002.txt AC 134 ms 5788 KB
003.txt AC 134 ms 5788 KB
004.txt AC 134 ms 5788 KB
005.txt AC 132 ms 5788 KB
006.txt AC 134 ms 5788 KB
007.txt AC 132 ms 5788 KB
008.txt AC 134 ms 5788 KB
009.txt AC 133 ms 5788 KB
010.txt AC 133 ms 5788 KB
011.txt AC 135 ms 5788 KB
012.txt AC 134 ms 5788 KB
013.txt AC 132 ms 5788 KB
014.txt AC 132 ms 5788 KB
015.txt AC 134 ms 5788 KB
016.txt AC 134 ms 5788 KB
017.txt AC 132 ms 5788 KB
018.txt AC 136 ms 5788 KB
019.txt AC 135 ms 5788 KB
020.txt AC 133 ms 5788 KB
example0.txt AC 18 ms 3064 KB