結果
問題 |
No.3 ビットすごろく
|
ユーザー |
|
提出日時 | 2017-11-16 23:55:08 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,573 bytes |
コンパイル時間 | 920 ms |
コンパイル使用メモリ | 110,792 KB |
実行使用メモリ | 27,928 KB |
最終ジャッジ日時 | 2024-11-25 06:59:20 |
合計ジャッジ時間 | 2,518 ms |
ジャッジサーバーID (参考情報) |
judge5 / 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.
ソースコード
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace yuki0003 { internal static class Program { internal static int CountHighBits(int value) { return value == 0 ? 0 : ( value % 2 + CountHighBits( value >> 1 ) ); } public static int Solve(System.IO.TextReader reader) { var n = int.Parse( reader.ReadLine() ); var cost = new int[n]; var stack = new Stack<int>(); var answer = -1; const int FIRST = 1; stack.Push( FIRST ); cost[ FIRST - 1 ] = 1; int cur, backward, forward; while (stack.Count>0) { cur = stack.Pop(); if (cur == n) { answer = cost[ cur - 1 ]; break; } backward = cur - CountHighBits( cur ); if (backward >= 1 && cost[backward-1]==0) { stack.Push( backward ); cost[ backward - 1 ] = cost[ cur - 1 ] + 1; } forward = cur + CountHighBits( cur ); if (forward <= n && cost[ forward - 1 ] == 0) { stack.Push( forward ); cost[ forward - 1 ] = cost[ cur - 1 ] + 1; } } return answer; } static void Main(string[] args) { Console.WriteLine( Solve( System.Console.In ) ); } } }