結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
oemon
|
| 提出日時 | 2016-11-27 23:19:11 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 31 ms / 5,000 ms |
| コード長 | 2,107 bytes |
| コンパイル時間 | 999 ms |
| コンパイル使用メモリ | 107,648 KB |
| 実行使用メモリ | 19,328 KB |
| 最終ジャッジ日時 | 2024-07-01 08:11:15 |
| 合計ジャッジ時間 | 2,911 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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;
namespace No3
{
class Program
{
private static int N;
private static int[] map;
static void Main(string[] args)
{
N = int.Parse(Console.ReadLine());
Width();
}
static void Deep()
{
map = Enumerable.Repeat(-1, N).ToArray();
Rec(0, 0);
Console.Write(map.Last());
}
static void Rec(int index, int preventCount)
{
if (index < 0) return;
if (index >= N) return;
if (map[index] != -1
&& map[index] <= preventCount + 1) return;
map[index] = preventCount + 1;
if (index == N - 1) return;
var bitcount = BitCount(index + 1);
Rec(index + bitcount, preventCount + 1);
Rec(index - bitcount, preventCount + 1);
}
static int BitCount(int value)
{
var count = 0;
for (var i = 0; i < 32; i++)
{
var tmp = 1 << i;
if ((value & tmp) != 0) count++;
}
return count;
}
static void Width()
{
map = Enumerable.Repeat(-1, N).ToArray();
map[0] = 1;
var queue = new Queue<int>(new[] { 0 });
Func<int, int, bool> enqueue = (next, current) =>
{
if (next < 0) return false;
if (next >= N) return false;
if (map[next] != -1) return false;
map[next] = map[current] + 1;
queue.Enqueue(next);
return true;
};
while (queue.Any())
{
var current = queue.Dequeue();
if (current == N - 1) continue;
var bitcount = BitCount(current + 1);
enqueue(current + bitcount, current);
enqueue(current - bitcount, current);
}
Console.Write(map.Last());
}
}
}
oemon