結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
YoiTaka
|
| 提出日時 | 2017-02-23 11:10:25 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 33 ms / 5,000 ms |
| コード長 | 1,354 bytes |
| コンパイル時間 | 989 ms |
| コンパイル使用メモリ | 109,328 KB |
| 実行使用メモリ | 27,052 KB |
| 最終ジャッジ日時 | 2024-07-01 08:20:14 |
| 合計ジャッジ時間 | 2,990 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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);
int[] dist = new int[N + 1];
Queue<int> que = new Queue<int>();
que.Enqueue(1);
dist[1] = 1;
while(que.Any())
{
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] == 0)//now-bit数が0以上,未訪問の場合
{
dist[now - bit] = dist[now] + 1;//点の距離をnowから+1の距離で確定する
que.Enqueue(now - bit);//次の位置をqueueに入れる
}
if (now + bit <= N && dist[now + bit] == 0)//now+bit数がN以下,未訪問の場合
{
dist[now + bit] = dist[now] + 1;//点の距離をnowから+1の距離で確定する
que.Enqueue(now + bit);//次の位置をqueueに入れる
}
}
if (dist[N] == 0)
{
System.Console.WriteLine("-1");
return;
}
System.Console.WriteLine(dist[N]);
}
}
YoiTaka