結果

問題 No.3 ビットすごろく
ユーザー MoonOnRainMoonOnRain
提出日時 2018-05-21 12:21:38
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 1,312 bytes
コンパイル時間 2,120 ms
コンパイル使用メモリ 102,924 KB
実行使用メモリ 25,580 KB
最終ジャッジ日時 2023-09-11 00:18:28
合計ジャッジ時間 5,349 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 59 ms
21,568 KB
testcase_03 AC 59 ms
23,608 KB
testcase_04 WA -
testcase_05 AC 58 ms
21,388 KB
testcase_06 AC 59 ms
21,324 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 58 ms
21,496 KB
testcase_13 AC 58 ms
21,476 KB
testcase_14 AC 59 ms
21,444 KB
testcase_15 AC 57 ms
21,612 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 59 ms
21,380 KB
testcase_23 AC 59 ms
23,476 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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();
        passList.Add(now);
        
        if(now == target && passList.Count < result.Count){
            result = passList;
        }
        
        if(passList.Contains(now)){
            return;
        }
        
        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