結果
問題 | No.367 ナイトの転身 |
ユーザー | むらため |
提出日時 | 2019-02-04 23:01:55 |
言語 | Nim (2.0.2) |
結果 |
AC
|
実行時間 | 59 ms / 2,000 ms |
コード長 | 2,004 bytes |
コンパイル時間 | 2,954 ms |
コンパイル使用メモリ | 77,384 KB |
実行使用メモリ | 7,992 KB |
最終ジャッジ日時 | 2024-07-01 11:22:28 |
合計ジャッジ時間 | 3,960 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 1 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,944 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,940 KB |
testcase_10 | AC | 38 ms
7,948 KB |
testcase_11 | AC | 59 ms
7,992 KB |
testcase_12 | AC | 7 ms
6,940 KB |
testcase_13 | AC | 7 ms
6,944 KB |
testcase_14 | AC | 20 ms
7,512 KB |
testcase_15 | AC | 2 ms
6,940 KB |
testcase_16 | AC | 20 ms
7,692 KB |
testcase_17 | AC | 5 ms
7,376 KB |
testcase_18 | AC | 6 ms
6,940 KB |
testcase_19 | AC | 5 ms
6,944 KB |
testcase_20 | AC | 2 ms
6,940 KB |
testcase_21 | AC | 6 ms
6,940 KB |
testcase_22 | AC | 2 ms
6,940 KB |
testcase_23 | AC | 2 ms
6,940 KB |
testcase_24 | AC | 3 ms
6,944 KB |
testcase_25 | AC | 2 ms
6,940 KB |
testcase_26 | AC | 2 ms
6,940 KB |
ソースコード
import sequtils,times#,nimprof type Pos = tuple[x,y:int16] template stopwatch(body) = (let t1 = cpuTime();body;echo "TIME:",(cpuTime() - t1) * 1000,"ms") proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .} proc scan(): int = while true: let k = getchar_unlocked() if k < '0': return result = 10 * result + k.ord - '0'.ord let h = scan() let w = scan() var M : array[500,array[500,bool]] const INF = int16.high var DP : array[500,array[500,array[2,int16]]] var s : Pos var g : Pos for y in 0..<h: for x in 0..<w: let k = getchar_unlocked() if k == 'S' : s = (x.int16,y.int16) elif k == 'G' : g = (x.int16,y.int16) M[x][y] = k == 'R' discard getchar_unlocked() DP[s.x][s.y][0] = -INF var Q : array[500*500*2+100,uint64] # tuple[x,y,isKnight,d:int16] template en(a,b,c,d:int16):uint64 = (cast[uint64](a) shl 48) or (cast[uint64](b) shl 32) or (cast[uint32](c) shl 16) or cast[uint16](d) var l = 0 var r = 0 Q[r] = en(s.x,s.y,1.int16,-INF) r += 1 let D : seq[seq[Pos]] = @[ @[(1,1),(-1,-1),(1,-1),(-1,1)], @[(1,2),(2,1),(-1,-2),(-2,-1),(-1,2),(-2,1),(1,-2),(2,-1)], ].mapIt(it.mapIt((it[0].int16,it[1].int16))) block: proc solve() : bool = while r - l > 0: let qe = Q[l] # echo [l,r] l += 1 let x = cast[int16](qe shr 48) let y = cast[int16](qe shr 32) let isKnight = cast[int16](qe shr 16) let diff = cast[int16](qe) let nextDiff = diff + 1 for i in 0..<(4 + isKnight * 4): let nx = x + D[isKnight][i].x let ny = y + D[isKnight][i].y if nx < 0 or ny < 0 or nx >= w or ny >= h : continue if nx == g.x and ny == g.y : echo nextDiff + INF return true let nextIsKnight = if M[nx][ny]: 1 - isKnight else: isKnight if DP[nx][ny][nextIsKnight] <= nextDiff: continue DP[nx][ny][nextIsKnight] = nextDiff Q[r] = (en(nx,ny,nextIsKnight,nextDiff)) r += 1 return false if not solve(): echo -1