結果

問題 No.3 ビットすごろく
ユーザー wowilsonwowilson
提出日時 2017-12-22 15:50:18
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 1,876 bytes
コンパイル時間 3,191 ms
コンパイル使用メモリ 110,844 KB
実行使用メモリ 26,096 KB
最終ジャッジ日時 2024-12-17 23:11:13
合計ジャッジ時間 5,062 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18 WA * 15
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace yukicoder
{
    class Program
    {
        public static int N;
        static void Main(string[] args)
        {
            N = int.Parse(Console.ReadLine());
            Console.WriteLine(Bfs(1));
        }

        static int Bfs(int start)
        {
            Queue<int[]> que = new Queue<int[]>();
            que.Enqueue(new int[] { start, 1 });
            bool[] visited = new bool[N];
            while(que.Count != 0)
            {
                int[] nums = que.Dequeue();
                int currentNum = nums[0];
                int count = nums[1];

                if (currentNum == N)
                {
                    return count;
                }

                if (visited[currentNum])
                {
                    continue;
                }


                else
                {
                    visited[currentNum] = true;

                    int next = numofbits5(currentNum);
                    if(currentNum + next <= N)
                    {
                        que.Enqueue(new int[] { currentNum + next , ++count});
                    }
                    if(currentNum - next >= 2)
                    {
                        que.Enqueue(new int[] { currentNum - next , ++count});
                    }
                }

            }
            return -1;
        }

        static int numofbits5(long 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 (int)((bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff));
        }
    }
}
0