結果

問題 No.3 ビットすごろく
ユーザー bluemegane
提出日時 2018-10-07 14:38:59
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 27 ms / 5,000 ms
コード長 1,317 bytes
コンパイル時間 947 ms
コンパイル使用メモリ 105,984 KB
実行使用メモリ 18,688 KB
最終ジャッジ日時 2024-07-01 09:09:05
合計ジャッジ時間 2,722 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System.Linq;
using System.Collections.Generic;
using System;

public class P
{
    public int c { get; set; }
    public int a { get; set; }
}

public class Hello
{
    public static void Main()
    {
        var n = int.Parse(Console.ReadLine().Trim());
        var ans = goBfs(n);
        Console.WriteLine(ans);

    }
    public static int goBfs(int n)
    {
        var d = new int[] { 1, -1 };
        var q = new Queue<P>();
        var used = new bool[n + 1];
        q.Enqueue(new P { a = 1, c = 1 });
        while (q.Count() > 0)
        {
            var w = q.Dequeue();
            if (w.a == n) return w.c;
            if (used[w.a]) continue;
            used[w.a] = true;
            for (int i = 0; i < 2; i++)
            {
                var na = w.a + d[i] * bitcount(w.a);
                if (na >= 1 && na <= n && !used[na]) q.Enqueue(new P { a = na, c = w.c + 1 });
            }
        }
        return -1;
    }
    public static int bitcount(int bits)
    {
        bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
        bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
        bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
        bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
        return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
    }
}
0