結果

問題 No.124 門松列(3)
ユーザー 6soukiti296soukiti29
提出日時 2017-09-03 20:40:11
言語 Nim
(2.0.2)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 2,751 bytes
コンパイル時間 2,964 ms
コンパイル使用メモリ 68,868 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-12 15:05:22
合計ジャッジ時間 4,147 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,384 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(25, 25) Warning: Number of spaces around '<=' is not consistent [Spacing]
/home/judge/data/code/Main.nim(1, 26) Warning: imported and not used: 'math' [UnusedImport]

ソースコード

diff #

import sequtils,strutils,math

import sequtils,strutils
type
    priorityQue[I:static[int];T:tuple] = array[I + 1,T]
    item = tuple[cost, x, y, a : int]

proc push[T](A:var priorityQue; b: T;)=
    const key = 0
    A[0][0] += 1
    A[A[0][0]] = b # A[0][0]はsize、最大のIndex
    var index = A[0][0]
    while index > 1 and A[index][key] > A[index div 2][key]:
        (A[index],A[index div 2]) = (A[index div 2],A[index])
        index = index div 2
proc pop(A: var priorityQue):item = #戻り値を書き換える
    const key = 0
    result = A[1]
    A[1] = A[A[0][0]]
    var n : item
    A[A[0][0]] = n
    A[0][0] -= 1
    var index = 1
    while index * 2 <= A[0][0]:
        if index * 2 + 1<= A[0][0]:
            if A[index][key] < A[index * 2][key] and A[index * 2 + 1][key] <= A[index * 2][key]:
                (A[index],A[index * 2]) = (A[index * 2],A[index])
                index = index * 2
            elif A[index][key] < A[index * 2 + 1][key]:
                (A[index],A[index * 2 + 1]) = (A[index * 2 + 1],A[index])
                index = index * 2 + 1
            else:
                break
        elif index * 2 <= A[0][0]:
            if A[index][key] < A[index * 2][key]:
                (A[index],A[index * 2]) = (A[index * 2],A[index])
            break
        else:
            break
proc size(A : priorityQue):int=
    return A[0][0]
    

var
    W, H : int
    hyou : array[103, array[103, int]]
    pq : priorityQue[10000000, item]
    p : item
    D = [[1, 0],[0, 1],[-1, 0],[0, -1]]
    flag : array[103, array[103, array[4, bool]]]
    
(W, H) = stdin.readline.split.map(parseInt)
for h in 1..H:
    hyou[h][1..W] = stdin.readline.split.map(parseInt)

proc isKadomatsuSequence(a, b, c : int) : bool =
    if b == 0 or a == 0:
        return true
    if a != b and b != c and c != a:
        if b == max(@[a, b, c]):
            return true
        elif b == min(@[a, b, c]):
            return true
        else:
            return false
    else:
        return false

p = (0, 1, 1, 0)
pq.push(p)
block tansaku:
    while pq.size > 0:
        p = pq.pop()
        if p.cost < - 10000:
            break
        for j,d in D:
            var
                nx = p.x + d[1]
                ny = p.y + d[0]
            if hyou[ny][nx] == 0:
                continue
            var
                b = hyou[p.y][p.x]
                a = p.a
                c = hyou[ny][nx]
            if isKadomatsuSequence(a, b, c):
                if flag[ny][nx][j] == true:
                    continue
                flag[ny][nx][j] = true
                if nx == W and ny == H:
                    echo -p.cost + 1
                    break tansaku
                pq.push((p.cost - 1, nx, ny, b))
    echo -1
0