結果
| 問題 |
No.3 ビットすごろく
|
| ユーザー |
|
| 提出日時 | 2018-02-18 11:46:32 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 959 bytes |
| コンパイル時間 | 1,315 ms |
| コンパイル使用メモリ | 163,688 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-01 08:54:39 |
| 合計ジャッジ時間 | 2,252 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
int N,w[10001],dist[10001];
bool e[10001];
queue<int> Qu;
int main(){
cin >> N;
for(int i = 1; i <= N; i++) e[i] = false;
for(int i = 1; i <= N; i++) dist[i] = 1;
for(int i = 1; i <= N; i++){
int tw = 1;
int t = i;
while(t != 1){
if(t%2) tw++;
t /= 2;
}
w[i] = tw;
}
Qu.push(1);
e[1] = true;
while(!Qu.empty()){
int t = Qu.front();
Qu.pop();
if(t == N){
cout << dist[t] << endl;
return 0;
}
if(t + w[t] <= N && !e[t + w[t]]){
Qu.push(t + w[t]);
e[t + w[t]] = true;
dist[t + w[t]] = dist[t] + 1;
}
if(t - w[t] > 0 && !e[t - w[t]]){
Qu.push(t - w[t]);
e[t - w[t]] = true;
dist[t - w[t]] = dist[t] + 1;
}
}
cout << -1 << endl;
return 0;
}