結果

問題 No.3 ビットすごろく
ユーザー ko_ki_leo
提出日時 2015-10-11 19:47:28
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 676 bytes
コンパイル時間 1,342 ms
コンパイル使用メモリ 161,652 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-01 07:34:29
合計ジャッジ時間 6,623 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other RE * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int solve(int)’:
main.cpp:41:1: warning: no return statement in function returning non-void [-Wreturn-type]
   41 | }
      | ^

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

queue < int > que;
int ans[11111];

int n;

int solve(int p){

  for(int i=0;i<11111;i++) ans[i] = 1<<28;
  ans[1] = 1;
  que.push( 1 );

  while(!que.empty()){
    p = que.front(); que.pop();

    int cnt = 0;
    for(int i=0;i<32;i++)
      if((p & (1 << i))) cnt++;

    if(p + cnt <= n){
      if(ans[p+cnt] > ans[p]+1){
	
	ans[p+cnt] = ans[p] + 1;
	que.push( p + cnt );

      }
    }

    if(p - cnt > 0){
      if(ans[p-cnt] > ans[p]+1){
	
	ans[p-cnt] = ans[p] + 1;
	que.push( p - cnt );
      }
    }
    
  }
  
}

int main(){

  cin >> n;
  solve(1);

  if(ans[n] == 1<<28) puts("-1");
  else cout << ans[n] << endl;

}
0