結果

問題 No.1577 織姫と彦星2
ユーザー kitatike55kitatike55
提出日時 2021-07-21 16:48:49
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 452 ms / 2,000 ms
コード長 991 bytes
コンパイル時間 94 ms
コンパイル使用メモリ 11,064 KB
実行使用メモリ 17,604 KB
最終ジャッジ日時 2023-09-24 13:14:41
合計ジャッジ時間 9,673 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,720 KB
testcase_01 AC 19 ms
8,520 KB
testcase_02 AC 19 ms
8,716 KB
testcase_03 AC 19 ms
8,628 KB
testcase_04 AC 18 ms
8,712 KB
testcase_05 AC 26 ms
10,520 KB
testcase_06 AC 21 ms
9,064 KB
testcase_07 AC 62 ms
14,892 KB
testcase_08 AC 129 ms
11,488 KB
testcase_09 AC 254 ms
17,308 KB
testcase_10 AC 57 ms
11,032 KB
testcase_11 AC 34 ms
8,980 KB
testcase_12 AC 136 ms
13,936 KB
testcase_13 AC 36 ms
10,728 KB
testcase_14 AC 197 ms
14,248 KB
testcase_15 AC 109 ms
10,868 KB
testcase_16 AC 237 ms
17,400 KB
testcase_17 AC 138 ms
13,644 KB
testcase_18 AC 20 ms
8,680 KB
testcase_19 AC 279 ms
16,540 KB
testcase_20 AC 256 ms
16,004 KB
testcase_21 AC 328 ms
17,444 KB
testcase_22 AC 230 ms
17,536 KB
testcase_23 AC 129 ms
11,360 KB
testcase_24 AC 205 ms
15,228 KB
testcase_25 AC 73 ms
10,712 KB
testcase_26 AC 131 ms
11,380 KB
testcase_27 AC 73 ms
14,264 KB
testcase_28 AC 185 ms
14,396 KB
testcase_29 AC 73 ms
13,228 KB
testcase_30 AC 132 ms
13,756 KB
testcase_31 AC 231 ms
17,188 KB
testcase_32 AC 22 ms
8,744 KB
testcase_33 AC 23 ms
8,700 KB
testcase_34 AC 99 ms
13,248 KB
testcase_35 AC 231 ms
17,148 KB
testcase_36 AC 273 ms
17,504 KB
testcase_37 AC 452 ms
17,492 KB
testcase_38 AC 185 ms
15,572 KB
testcase_39 AC 104 ms
11,656 KB
testcase_40 AC 110 ms
11,668 KB
testcase_41 AC 93 ms
15,908 KB
testcase_42 AC 68 ms
14,180 KB
testcase_43 AC 82 ms
13,264 KB
testcase_44 AC 24 ms
8,772 KB
testcase_45 AC 68 ms
10,420 KB
testcase_46 AC 40 ms
9,992 KB
testcase_47 AC 61 ms
15,668 KB
testcase_48 AC 49 ms
10,904 KB
testcase_49 AC 109 ms
13,540 KB
testcase_50 AC 51 ms
10,640 KB
testcase_51 AC 23 ms
8,924 KB
testcase_52 AC 235 ms
17,604 KB
testcase_53 AC 227 ms
15,536 KB
testcase_54 AC 84 ms
14,092 KB
testcase_55 AC 216 ms
15,364 KB
testcase_56 AC 113 ms
10,808 KB
testcase_57 AC 41 ms
9,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
start,end = map(int,input().split())
stone = list(map(int,input().split()))
stoneMax = max(stone)
maxLen = int(len(bin(stoneMax)[2:]))

setStone= set(stone)
avoidStone = set()
from collections import deque
def bfs(u):
    queue = deque([u,0])
    while queue:
        v = queue.popleft()
        dis = queue.popleft()
        h = serchHam(v)
        if end in h:
            print(dis)
            exit()
        for i in h:
            queue.append(i)
            queue.append(dis+1)
    print(-1)
    exit() 
      
def serchHam(a):
    h=[]
    abin = bin(a+2**maxLen)[2:]
    for i in range(1,maxLen+1):
        if abin[i] == '0':
            hh = 2**(maxLen-i)+a
        else:
            hh = a-2**(maxLen-i)
        if hh == end:
            h.append(hh)
            return h
        if hh in setStone:
            if hh in avoidStone:
                continue
            else:
                h.append(hh)
                avoidStone.add(hh)
    return h

bfs(start)
0