結果

問題 No.3 ビットすごろく
ユーザー AdamantiteAdamantite
提出日時 2023-06-29 21:27:51
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 1,161 bytes
コンパイル時間 937 ms
コンパイル使用メモリ 112,760 KB
実行使用メモリ 36,760 KB
最終ジャッジ日時 2024-07-06 11:33:48
合計ジャッジ時間 14,875 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15 TLE * 1 -- * 17
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

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();
                if(x == n) {
                    break;
                }
                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]);
        }
    }
}
0