NOI:2971牛を捕まえろ
1628 ワード
タイトルリンク:http://noi.openjudge.cn/ch0205/2971/
标题解:この问题は典型的なBFSを使って解くことができて、最短路を求める问题といえば、BFS(この意识が必要です)
#include
#include
#include
using namespace std;
int size;
int n,k;
struct point {
int x,tmp;
point(int x1,int tmp1){
x=x1;
tmp=tmp1;
}
point(){
x=0;
tmp=0;
}
};
queue a;
bool b[100005];//
void test(){// bfs
point t(n,0);
a.push(t);
while(!a.empty()){
t=a.front();
a.pop();
if(t.x==k){
size=t.tmp;
return;
}else{
if((t.x+1)<=100000&&b[t.x+1]){
point t2(t.x+1,t.tmp+1);
b[t.x+1]=false;
a.push(t2);
}
if(t.x-1>=0&&b[t.x-1]){
point t2(t.x-1,t.tmp+1);
b[t.x-1]=false;
a.push(t2);
}
if((t.x*2)<=100000&&b[t.x*2]){
point t2(t.x*2,t.tmp+1);
b[t.x*2]=false;
a.push(t2);
}
}
}
}
int main(){
cin>>n>>k;
for(int i=0;i<100005;i++){
b[i]=true;
}
size=0;
test();
cout<