結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-10-03 20:12:34 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,192 bytes |
| コンパイル時間 | 770 ms |
| コンパイル使用メモリ | 108,236 KB |
| 実行使用メモリ | 29,568 KB |
| 最終ジャッジ日時 | 2024-10-12 10:27:02 |
| 合計ジャッジ時間 | 2,604 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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.
ソースコード
using System;
using System.Collections.Generic;
namespace Problem003
{
class Program
{
static void Main(string[] args)
{
var n = int.Parse(Console.ReadLine());
Console.WriteLine(Process(n, 1, new HashSet<int>()));
}
static int Process(int n, int pos, HashSet<int> db, int depth = 1)
{
if (db.Contains(pos)) return -1;
db.Add(pos);
if (pos == n) return depth;
var steps = Count1(pos);
// 前に移動
var next = pos + steps;
if (next <= n) {
var ans = Process(n, next, db, depth + 1);
if (ans > 0) return ans;
}
// 後ろに移動
next = pos - steps;
if (next > 1) {
var ans = Process(n, next, db, depth + 1);
if (ans > 0) return ans;
}
return -1;
}
static int Count1(int v)
{
var count = 0;
while (v != 0)
{
if ((v & 1) == 1) count++;
v >>= 1;
}
return count;
}
}
}