LoginSignup
1
1

More than 5 years have passed since last update.

増やす減らす二倍する

Last updated at Posted at 2013-09-26

横へなの中では最も基礎的な問題ではないかと.
いつものことながら採点プログラムと同時に動かす必要があります.

hena13pre.cpp
//http://nabetani.sakura.ne.jp/hena/ord13updowndouble/
//http://qiita.com/Nabetani/items/89fb0e2e712d4b396535
#include <queue>
#include <map>
#include <cstdio>
using namespace std;
int main(){
    int n;
    map<int,int>back;
    back[0]=0;
    back[1]=0;
    map<int,int>count;
    count[0]=0;
    count[1]=1;
    queue<int>q;
    q.push(1);
    for(;!q.empty();){
        int x=q.front();q.pop();
        int a[3]={x-1,x+1,x*2};
        for(int i=0;i<3;i++){
            if(a[i]<500000&&count.find(a[i])==count.end()){
                back[a[i]]=x;
                count[a[i]]=count[x]+1;
                q.push(a[i]);
            }
        }
    }
    for(;~scanf("%d",&n);){
        printf("%d\n",count[n]);fflush(stdout);
    }
    return 0;
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1