結果

問題 No.3 ビットすごろく
ユーザー Adamantite
提出日時 2023-06-29 21:33:41
言語 C#
(.NET 8.0.404)
結果
AC  
実行時間 526 ms / 5,000 ms
コード長 1,357 bytes
コンパイル時間 16,759 ms
コンパイル使用メモリ 168,728 KB
実行使用メモリ 184,564 KB
最終ジャッジ日時 2024-07-06 11:39:59
合計ジャッジ時間 18,488 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (94 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

using System;
using System.Collections.Generic;

namespace No00003_BitSugoroku
{
    internal class Program
    {
        static void Main(string[] args) {
            int n = int.Parse(Console.ReadLine());

            int[] map = new int[n + 1];
            map[1] = 1;
            Queue<int> q = new Queue<int>();
            q.Enqueue(1);

            while (0 < q.Count) {
                int x = q.Dequeue();
                if(x == n) {
                    break;
                }
                int diff = BitCount(x);

                if (x + diff <= n) {
                    if (map[x + diff] == 0 || map[x] < map[x + diff]) {
                        q.Enqueue(x + diff);
                        map[x + diff] = map[x] + 1;
                    }
                }
                if (0 < x - diff) {
                    if (map[x - diff] == 0 || map[x] < map[x - diff]) {
                        q.Enqueue(x - diff);
                        map[x - diff] = map[x] + 1;
                    }
                }
            }
            Console.WriteLine(map[n] == 0 ? -1: map[n]);
        }

        static int BitCount(int x) {
            int count = 0;
            while (0 < x) {
                if(x % 2 == 1) {
                    count++;
                }
                x /= 2;
            }
            return count;
        }
    }
}
0