結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  YoiTaka | 
| 提出日時 | 2017-02-23 11:06:36 | 
| 言語 | C#(csc) (csc 3.9.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 33 ms / 5,000 ms | 
| コード長 | 1,552 bytes | 
| コンパイル時間 | 1,010 ms | 
| コンパイル使用メモリ | 113,996 KB | 
| 実行使用メモリ | 26,932 KB | 
| 最終ジャッジ日時 | 2024-07-01 08:20:01 | 
| 合計ジャッジ時間 | 3,135 ms | 
| ジャッジサーバーID (参考情報) | judge2 / 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;
using System.Collections.Generic;
using System.Linq;
public class Hello
{
    public static void Main()
    {
        
        string line = System.Console.ReadLine().Trim();
        int N = int.Parse(line);
        bool[] flag = new bool[10010];
        int[] dist = new int[10010];
        const int INF = (int)1e8;
        for (int i = 0; i < 10010; i++)
        {
            flag[i] = false;
            dist[i] = INF;
        }
        Queue<int> que = new Queue<int>();
        que.Enqueue(1);
        dist[1] = 1;
        while(que.Count !=0)
        {
            int now = que.Dequeue();
            string bin = Convert.ToString(now, 2);
            int bit = bin.Where(c => c == '1').Count();
            if (now - bit > 0 && dist[now - bit] == INF)//now-bit数が0以上,未訪問の場合
            {
                dist[now - bit] = dist[now] + 1;//点の距離をnowから+1の距離で確定する
                que.Enqueue(now - bit);//次の位置をqueueに入れる
            }
            if (now + bit <= N && dist[now + bit] == INF)//now+bit数がN以下,未訪問の場合
            {
                dist[now + bit] = dist[now] + 1;//点の距離をnowから+1の距離で確定する
                que.Enqueue(now + bit);//次の位置をqueueに入れる
            }               
        }
        if (dist[N] == (int)1e8)
        {
            System.Console.WriteLine("-1");
            return;
        }
        System.Console.WriteLine(dist[N]);
    }
}
            
            
            
        