結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  iwkjosec | 
| 提出日時 | 2018-06-29 21:28:09 | 
| 言語 | C#(csc) (csc 3.9.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 28 ms / 5,000 ms | 
| コード長 | 1,413 bytes | 
| コンパイル時間 | 2,181 ms | 
| コンパイル使用メモリ | 112,624 KB | 
| 実行使用メモリ | 26,092 KB | 
| 最終ジャッジ日時 | 2024-07-01 09:04:21 | 
| 合計ジャッジ時間 | 2,735 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| 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;
using System.Linq;
using static System.Console;
class Program
{
    static void Main()
    {
        var N = int.Parse(ReadLine());
        var g = new List<int>[N];
        for (int i = 0; i < N; i++)
        {
            g[i] = new List<int>();
        }
        for (int i = 0; i < N; i++)
        {
            var r = BitCount(i + 1);
            if (i - r >= 0) g[i].Add(i - r);
            if (i + r < N) g[i].Add(i + r);
        }
        WriteLine(BFS(g));
    }
    static int BitCount(int n)
    {
        n = (n & 0x55555555) + (n >> 1 & 0x55555555);
        n = (n & 0x33333333) + (n >> 2 & 0x33333333);
        n = (n & 0x0f0f0f0f) + (n >> 4 & 0x0f0f0f0f);
        n = (n & 0x00ff00ff) + (n >> 8 & 0x00ff00ff);
        return (n & 0x0000ffff) + (n >> 16 & 0x0000ffff);
    }
    static int BFS(List<int>[] graph)
    {
        var q = new Queue<int>();
        q.Enqueue(0);
        var dist = new int[graph.Length];
        dist[0]=1;
        while (q.Count != 0)
        {
            var v = q.Dequeue();
            if (v == graph.Length - 1) return dist[graph.Length - 1];
            foreach (var vertex in graph[v])
            {
                if (dist[vertex]==0)
                {
                    dist[vertex] = dist[v] + 1;
                    q.Enqueue(vertex);
                }
            }
        }
        return -1;
    }
}
            
            
            
        