結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-05-15 18:05:26 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 1,212 bytes |
| コンパイル時間 | 510 ms |
| コンパイル使用メモリ | 65,336 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-01 09:47:21 |
| 合計ジャッジ時間 | 1,531 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
int calc( int x )
{
if ( x < 2 ) {
return x;
} else {
return ( x % 2 ) + calc( x >> 1 );
}
}
int main()
{
// input
int N;
scanf( "%d", &N );
// process
int cost[10000];
for ( int i=0; i<N; i++ ) {
cost[i] = -1;
}
std::queue<int> q_state;
q_state.push(1);
cost[0] = 1;
while ( not q_state.empty() ) {
int cur = q_state.front();
q_state.pop();
int step = calc( cur );
int cur_back = cur - step;
if ( cur_back > 0 && cur_back <=N ) {
if ( cost[cur_back-1] == -1 or cost[cur_back-1] > cost[cur-1] + 1 ) {
cost[cur_back-1] = cost[cur-1] + 1;
q_state.push(cur_back);
}
}
int cur_forw = cur + step;
if ( cur_forw > 0 && cur_forw <=N ) {
if ( cost[cur_forw-1] == -1 or cost[cur_forw-1] > cost[cur-1] + 1 ) {
cost[cur_forw-1] = cost[cur-1] + 1;
q_state.push(cur_forw);
}
}
}
// output
printf( "%d\n", cost[N-1] );
return 0;
}