結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  Adamantite | 
| 提出日時 | 2023-06-29 21:17:46 | 
| 言語 | C# (.NET 8.0.404) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2,474 ms / 5,000 ms | 
| コード長 | 1,087 bytes | 
| コンパイル時間 | 18,331 ms | 
| コンパイル使用メモリ | 168,800 KB | 
| 実行使用メモリ | 211,904 KB | 
| 最終ジャッジ日時 | 2024-07-06 11:24:08 | 
| 合計ジャッジ時間 | 36,733 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (79 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/
ソースコード
using System;
using System.Collections.Generic;
using System.Linq;
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();
                int diff = Convert.ToString(x, 2).Count(x => x == '1');
                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]);
        }
    }
}
            
            
            
        