結果

問題 No.3 ビットすごろく
ユーザー yuki2006
提出日時 2017-07-01 15:34:21
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 26 ms / 5,000 ms
コード長 1,951 bytes
コンパイル時間 1,616 ms
コンパイル使用メモリ 108,768 KB
実行使用メモリ 17,920 KB
最終ジャッジ日時 2024-07-01 08:46:19
合計ジャッジ時間 2,596 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
namespace Procon
{
    class MainClass
    {
        static int popCount(int v)
        {
            int count = 0;
            while (v > 0)
            {
                if (v % 2 == 1)
                {
                    count++;
                }
                v /= 2;

            }
            return count;
        }
        public static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            bool[] check = new bool[N + 1];
            List<int> current = new List<int>();
            current.Add(1);
            check[1] = true;
            int turn = 1;

            if (N == 1)
            {
                Console.WriteLine(1);
                return;
            }

            while (current.Count > 0)
            {
                List<int> next = new List<int>();

                foreach (var cur in current)
                {
                    var count = popCount(cur);

                    int nextR = cur + count;
                    int nextL = cur - count;
                    if (nextR == N)
                    {
                        Console.WriteLine(turn + 1);
                        return;
                    }
                    if (0 < nextR && nextR < N)
                    {
                        if (!check[nextR])
                        {
                            next.Add(nextR);
                            check[nextR] = true;
                        }

                    }
                    if (0 < nextL && nextL < N)
                    {
                        if (!check[nextL])
                        {
                            next.Add(nextL);
                            check[nextL] = true;

                        }
                    }
                }
                turn++;
                current = next;
            }
            Console.WriteLine(-1);
        }
    }
}
0