結果

問題 No.3 ビットすごろく
ユーザー Wataru MaedaWataru Maeda
提出日時 2018-10-03 20:12:34
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 1,192 bytes
コンパイル時間 914 ms
コンパイル使用メモリ 112,004 KB
実行使用メモリ 28,164 KB
最終ジャッジ日時 2024-04-20 14:49:30
合計ジャッジ時間 2,655 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
25,524 KB
testcase_01 AC 26 ms
25,600 KB
testcase_02 AC 27 ms
25,736 KB
testcase_03 AC 27 ms
25,648 KB
testcase_04 AC 27 ms
27,644 KB
testcase_05 AC 28 ms
25,760 KB
testcase_06 AC 27 ms
27,664 KB
testcase_07 AC 27 ms
25,472 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 27 ms
25,696 KB
testcase_13 AC 27 ms
23,352 KB
testcase_14 AC 28 ms
25,464 KB
testcase_15 AC 29 ms
27,720 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 27 ms
27,788 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 26 ms
23,600 KB
testcase_22 AC 28 ms
27,836 KB
testcase_23 AC 28 ms
25,692 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 26 ms
25,472 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 27 ms
25,352 KB
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;

namespace Problem003
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = int.Parse(Console.ReadLine());
            Console.WriteLine(Process(n, 1, new HashSet<int>()));
        }

        static int Process(int n, int pos, HashSet<int> db, int depth = 1)
        {
            if (db.Contains(pos)) return -1;
            db.Add(pos);

            if (pos == n) return depth;

            var steps = Count1(pos);

            // 前に移動
            var next = pos + steps;
            if (next <= n) {
                var ans = Process(n, next, db, depth + 1);
                if (ans > 0) return ans;
            }
            // 後ろに移動
            next = pos - steps;
            if (next > 1) {
                var ans = Process(n, next, db, depth + 1);
                if (ans > 0) return ans;
            }
            return -1;
        }

        static int Count1(int v)
        {
            var count = 0;
            while (v != 0)
            {
                if ((v & 1) == 1) count++;
                v >>= 1;
            }
            return count;
        }
    }
}
0