結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  nuwasogi | 
| 提出日時 | 2015-11-22 13:36:48 | 
| 言語 | C#(csc) (csc 3.9.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 26 ms / 5,000 ms | 
| コード長 | 1,409 bytes | 
| コンパイル時間 | 1,683 ms | 
| コンパイル使用メモリ | 103,424 KB | 
| 実行使用メモリ | 25,952 KB | 
| 最終ジャッジ日時 | 2024-07-01 07:36:46 | 
| 合計ジャッジ時間 | 3,511 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.Collections.Generic;
namespace BitSugorokuCS
{
    class Program
    {
        static int CountBit(int x)
        {
            int ret = 0;
            while (x >0)
            {
                x &= x - 1;
                ++ret;
            }
            return ret;
        }
        static int BitSugoroku(int N)
        {
            int[] D = new int[N+1];
            D[1] = 1;
            Queue<int> q = new Queue<int>();
            q.Enqueue(1);
            while(q.Count > 0)
            {
                int v_now = q.Dequeue();
                if(v_now == N)
                {
                    return D[v_now];
                }
                int bitcount = CountBit(v_now);
                int v_rear = v_now + bitcount;
                int v_front = v_now - bitcount;
                if(0< v_rear && v_rear <=N && D[v_rear] == 0)
                {
                    q.Enqueue(v_rear);
                    D[v_rear] = D[v_now] + 1;
                }
                if (0 < v_front && v_front <= N && D[v_front] == 0)
                {
                    q.Enqueue(v_front);
                    D[v_front] = D[v_now] + 1;
                }
            }
            return -1;
        }
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            Console.WriteLine(BitSugoroku(N));
        }
    }
}
            
            
            
        