結果

問題 No.3 ビットすごろく
コンテスト
ユーザー monaka
提出日時 2022-01-03 13:47:10
言語 TypeScript
(6.0.2)
コンパイル:
tsc.sh -p tsconfig.json
実行:
node main.js ONLINE_JUDGE
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 664 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,565 ms
コンパイル使用メモリ 349,220 KB
最終ジャッジ日時 2026-06-05 05:58:50
合計ジャッジ時間 5,459 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.ts(2,15): error TS7006: Parameter 'input' implicitly has an 'any' type.
main.ts(4,9): error TS7034: Variable 'minStep' implicitly has type 'any[]' in some locations where its type cannot be determined.
main.ts(5,14): error TS7005: Variable 'minStep' implicitly has an 'any[]' type.
main.ts(6,6): error TS7005: Variable 'minStep' implicitly has an 'any[]' type.
main.ts(7,17): error TS7005: Variable 'minStep' implicitly has an 'any[]' type.
main.ts(13,15): error TS7006: Parameter 'now' implicitly has an 'any' type.
main.ts(13,20): error TS7006: Parameter 'n' implicitly has an 'any' type.
main.ts(13,23): error TS7006: Parameter 'minStep' implicitly has an 'any' type.
main.ts(13,32): error TS7006: Parameter 'step' implicitly has an 'any' type.
main.ts(23,22): error TS7006: Parameter 'i' implicitly has an 'any' type.

ソースコード

diff #
raw source code

function main(input) {
  const n = parseInt(input[0]);
  const minStep = [];
  walk(1, n, minStep, 1);
  if(minStep[n]) {
    console.log(minStep[n]);
  } else {
    console.log(-1);
  }
}

function walk(now, n, minStep, step) {
  if(now < 1 || now > n) return;
  if(!minStep[now]) minStep[now] = step;
  if(minStep[now] < step) return;
  minStep[now] = step;
  const num = countOneBit(now);
  walk(now+num, n, minStep, step+1);
  walk(now-num, n, minStep, step+1);
}

function countOneBit(i) {
  let count = 0;
  while(i > 0) {
    if((i & 1) === 1) count++;
    i >>= 1;
  }
  return count;
}

main(require("fs").readFileSync("/dev/stdin", "utf8").split("\n"));
0