結果

問題 No.3 ビットすごろく
ユーザー MoonOnRainMoonOnRain
提出日時 2018-05-21 12:27:44
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 1,377 bytes
コンパイル時間 2,205 ms
コンパイル使用メモリ 102,964 KB
実行使用メモリ 23,552 KB
最終ジャッジ日時 2023-09-11 00:18:39
合計ジャッジ時間 9,055 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
21,604 KB
testcase_01 AC 63 ms
23,552 KB
testcase_02 AC 64 ms
23,492 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;

public class Amatsuki
{
    public static void Main()
    {
        var target = int.Parse(Console.ReadLine());

        var pass = new int[0];
        var result = new List<int>();

        check(1, target, pass, ref result);

        if (result.Any())
        {
            Console.WriteLine(result.Count);
        }
        else
        {
            Console.WriteLine(-1);
        }
    }

    public static void check(int now, int target, int[] pass, ref List<int> result)
    {
        var passList = pass.ToList();
        if (passList.Contains(now)) { return; }

        passList.Add(now);

        if (now == target)
        {
            if(passList.Count < result.Count || result.Count == 0)
            {
                result = passList;
            }
        }

        var bit = bitCount(now);

        if (now + bit <= target)
        {
            check(now + bit, target, passList.ToArray(), ref result);
        }

        if (now - bit > 1)
        {
            check(now - bit, target, passList.ToArray(), ref result);
        }
    }

    public static int bitCount(int dec)
    {
        var result = 0;
        while (dec > 0)
        {
            if (dec % 2 == 1)
            {
                result++;
            }
            dec /= 2;
        }
        return result;
    }
}
0