結果
問題 | No.3 ビットすごろく |
ユーザー | upamune |
提出日時 | 2015-05-31 23:10:17 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 952 bytes |
コンパイル時間 | 1,304 ms |
コンパイル使用メモリ | 163,936 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-07-01 07:23:10 |
合計ジャッジ時間 | 2,263 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> P; typedef long long ll; #define rep(i,n) for(int i=0;i<n;i++) #define pb push_back #define inf INT_MAX/4 #define ALL(v) v.begin(),v.end() struct State{ int pos; int cost; }; int main(void){ int N; bool visit[10005]; memset(visit, false, sizeof(visit)); bool isClear = false; queue<State> q; cin >> N; q.push((State){1,1}); while(!q.empty()){ State now = q.front();q.pop(); if(now.pos == N){ isClear = true; cout << now.cost << endl; break; } int front = now.pos + __builtin_popcount(now.pos); if(front < N+1 && !visit[front]){ visit[front] = true; q.push((State){front,now.cost+1}); } int back = now.pos - __builtin_popcount(now.pos); if(0 < back && !visit[back]){ visit[back] = true; q.push((State){back,now.cost+1}); } } if(!isClear) cout << -1 << endl; return 0; }