結果

問題 No.3 ビットすごろく
ユーザー yuki2006
提出日時 2017-07-01 15:30:25
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 1,837 bytes
コンパイル時間 1,015 ms
コンパイル使用メモリ 106,908 KB
実行使用メモリ 25,792 KB
最終ジャッジ日時 2024-10-05 03:54:44
合計ジャッジ時間 2,300 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14 WA * 19
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
            while (current.Count > 0)
            {
                List<int> next = new List<int>();

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

                    int nextR = count + cur;
                    int nextL = count - cur;
                    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