結果

問題 No.3 ビットすごろく
ユーザー oemonoemon
提出日時 2016-11-27 23:19:11
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 66 ms / 5,000 ms
コード長 2,107 bytes
コンパイル時間 2,417 ms
コンパイル使用メモリ 104,708 KB
実行使用メモリ 23,736 KB
最終ジャッジ日時 2023-09-14 00:14:14
合計ジャッジ時間 5,868 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
21,640 KB
testcase_01 AC 63 ms
23,668 KB
testcase_02 AC 64 ms
23,712 KB
testcase_03 AC 62 ms
21,716 KB
testcase_04 AC 62 ms
23,632 KB
testcase_05 AC 63 ms
23,616 KB
testcase_06 AC 62 ms
21,620 KB
testcase_07 AC 62 ms
21,636 KB
testcase_08 AC 62 ms
21,684 KB
testcase_09 AC 62 ms
23,736 KB
testcase_10 AC 62 ms
21,692 KB
testcase_11 AC 64 ms
23,636 KB
testcase_12 AC 62 ms
21,648 KB
testcase_13 AC 63 ms
23,620 KB
testcase_14 AC 65 ms
21,600 KB
testcase_15 AC 66 ms
21,680 KB
testcase_16 AC 62 ms
21,524 KB
testcase_17 AC 64 ms
21,600 KB
testcase_18 AC 62 ms
21,632 KB
testcase_19 AC 63 ms
21,708 KB
testcase_20 AC 62 ms
21,656 KB
testcase_21 AC 62 ms
19,532 KB
testcase_22 AC 64 ms
23,664 KB
testcase_23 AC 63 ms
19,684 KB
testcase_24 AC 63 ms
21,624 KB
testcase_25 AC 66 ms
21,632 KB
testcase_26 AC 61 ms
21,640 KB
testcase_27 AC 62 ms
23,652 KB
testcase_28 AC 62 ms
23,612 KB
testcase_29 AC 62 ms
21,636 KB
testcase_30 AC 61 ms
21,572 KB
testcase_31 AC 61 ms
21,556 KB
testcase_32 AC 61 ms
21,596 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 No3
{
    class Program
    {
        private static int N;
        private static int[] map;

        static void Main(string[] args)
        {
            N = int.Parse(Console.ReadLine());

            Width();
        }

        static void Deep()
        {
            map = Enumerable.Repeat(-1, N).ToArray();

            Rec(0, 0);
            Console.Write(map.Last());
        }

        static void Rec(int index, int preventCount)
        {
            if (index < 0) return;
            if (index >= N) return;
            if (map[index] != -1
                && map[index] <= preventCount + 1) return;

            map[index] = preventCount + 1;
            if (index == N - 1) return;

            var bitcount = BitCount(index + 1);

            Rec(index + bitcount, preventCount + 1);
            Rec(index - bitcount, preventCount + 1);
        }

        static int BitCount(int value)
        {
            var count = 0;
            for (var i = 0; i < 32; i++)
            {
                var tmp = 1 << i;
                if ((value & tmp) != 0) count++;
            }

            return count;
        }

        static void Width()
        {
            map = Enumerable.Repeat(-1, N).ToArray();
            map[0] = 1;

            var queue = new Queue<int>(new[] { 0 });

            Func<int, int, bool> enqueue = (next, current) =>
            {
                if (next < 0) return false;
                if (next >= N) return false;
                if (map[next] != -1) return false;

                map[next] = map[current] + 1;

                queue.Enqueue(next);

                return true;
            };

            while (queue.Any())
            {
                var current = queue.Dequeue();

                if (current == N - 1) continue;

                var bitcount = BitCount(current + 1);

                enqueue(current + bitcount, current);
                enqueue(current - bitcount, current);
            }

            Console.Write(map.Last());
        }
    }
}
0