結果

問題 No.3 ビットすごろく
ユーザー MoonOnRain
提出日時 2018-05-21 12:21:38
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 1,312 bytes
コンパイル時間 987 ms
コンパイル使用メモリ 104,960 KB
実行使用メモリ 18,816 KB
最終ジャッジ日時 2024-06-28 14:55:43
合計ジャッジ時間 2,711 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10 WA * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
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