結果

問題 No.367 ナイトの転身
ユーザー むらためむらため
提出日時 2019-02-04 22:17:26
言語 Nim
(2.0.2)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,633 bytes
コンパイル時間 393 ms
コンパイル使用メモリ 42,832 KB
最終ジャッジ日時 2024-04-27 02:48:27
合計ジャッジ時間 669 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 17) Error: cannot open file: queues

ソースコード

diff #

import sequtils,queues,times#,nimprof
type Pos = tuple[x,y:int32]
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 = 1e5.int32
var DP : array[500,array[500,array[2,int32]]]
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.int32,y.int32)
    elif k == 'G' : g = (x.int32,y.int32)
    M[x][y] = k == 'R'
  discard getchar_unlocked()
DP[s.x][s.y][0] = -INF
var q = initQueue[tuple[x,y,isKnight,d:int32]]()
q.enqueue((s.x,s.y,1.int32,-INF))
block:
  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].int32,it[1].int32)))
  proc solve() : bool  =
    while q.len > 0:
      let (x,y,isKnight,diff) = q.dequeue()
      let nextDiff = diff + 1
      for d in D[isKnight]:
        let nx = x + d[0]
        let ny = y + d[1]
        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.enqueue((nx,ny,nextIsKnight,nextDiff))
    return false
  if not solve(): echo -1
0