結果

問題 No.3 ビットすごろく
ユーザー AreTrash
提出日時 2016-04-09 03:43:52
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 1,127 bytes
コンパイル時間 970 ms
コンパイル使用メモリ 112,776 KB
実行使用メモリ 26,036 KB
最終ジャッジ日時 2024-10-04 05:02:03
合計ジャッジ時間 4,650 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18 WA * 15
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 No3_1{
    public class Program{
        private static List<int> stack = new List<int>();
        private static int min = int.MaxValue;
        public static void Main(string[] args){
            var result = Function(int.Parse(Console.ReadLine()), 1, 1);
            if(result == int.MaxValue) result = -1;
            Console.WriteLine(result);
            Console.ReadLine();
        }

        public static int Function(int input, int place, int cnt){
            if(place == input) return min = cnt;
            if(cnt >= input || cnt >= min || stack.Contains(place) || place < 1 || input < place) return int.MaxValue;
            stack.Add(place);
            var result = Math.Min(Function(input, place + BitOne(place), cnt + 1), Function(input, place - BitOne(place), cnt + 1));
            return result;
        }

        public static int BitOne(int num){
            var result = 0;
            while(true){
                if(num % 2 == 1) result++;
                if((num /= 2) == 0) break;
            }
            return result;
        }
    }
}
0