結果

問題 No.3 ビットすごろく
ユーザー te-sh
提出日時 2016-08-27 18:12:12
言語 D
(dmd 2.109.1)
結果
TLE  
実行時間 -
コード長 816 bytes
コンパイル時間 820 ms
コンパイル使用メモリ 106,256 KB
実行使用メモリ 77,116 KB
最終ジャッジ日時 2024-06-12 03:45:53
合計ジャッジ時間 20,410 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15 TLE * 1 -- * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.array, std.range;
import std.string, std.conv;
import std.math;
import std.stdio, std.typecons;

alias Tuple!(int, "p", int, "c") qitem;

void main()
{
  auto n = readln.chomp.to!int;

  writeln(calc(n));
}

int calc(int n)
{
  auto q = [qitem(1, 1)];
  auto visited = new bool[n + 1];

  while (!q.empty) {
    auto qi = q.front;
    q.popFront;

    visited[qi.p] = true;

    if (qi.p == n) {
      return qi.c;
    } else {
      auto b = bits(qi.p);

      auto p1 = qi.p - b;
      if (p1 >= 1 && !visited[p1])
        q ~= qitem(p1, qi.c + 1);

      auto p2 = qi.p + b;
      if (p2 <= n && !visited[p2])
        q ~= qitem(p2, qi.c + 1);
    }
  }
  return -1;
}

int bits(int n)
{
  auto c = 0;
  while (n > 0) {
    if ((n & 1) == 1)
      c += 1;
    n >>= 1;
  }
  return c;
}
0