結果

問題 No.3 ビットすごろく
ユーザー shi-mo
提出日時 2016-03-27 23:49:45
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 660 bytes
コンパイル時間 1,497 ms
コンパイル使用メモリ 134,872 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-12 03:14:57
合計ジャッジ時間 2,622 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio;
import core.bitop;

int[] numMoves;

void moveFrom(uint i, uint n) {
   if (!numMoves[i-1]) { return; }

   foreach(neighbor; [ (i + i.popcnt), (i - i.popcnt) ]) {
       if (neighbor < 1 || n < neighbor) { continue; }

       int nmNeighbor = numMoves[i-1] + 1;
       if (numMoves[neighbor-1] && (numMoves[neighbor-1] <= nmNeighbor)) {
           continue;
       }

       numMoves[neighbor-1] = nmNeighbor;
       moveFrom(neighbor, n);
   }
}

void main() {
   uint n;
   readf("%s\n", &n);

   numMoves.length = n;
   numMoves[0] = 1;
   foreach (i; 1..n) {
       moveFrom(i, n);
   }
   writeln(numMoves[n-1] ? numMoves[n-1] : -1);
}
0