搜索专题训练 A - Catch That Cow

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points - 1 or + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

Input

Line 1: Two space-separated integers:  N and  K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
 
 
题解

bfs基础题 题目大概意思是 在一条横坐标轴上 牛跑了 农民要抓牛 牛不知农民要抓牛 牛不动 农民要去追牛只有三种方式 1.前进一步 2.后退一步 3.任意门传送至自己当前坐标的2倍 

目标是求最少步数

eg:题目所给 5 → 17 5 → 10 → 20 → 19 → 18 → 17 or 5 → 10 → 9 → 18 → 17

  图示  红色代表目标结点 肉色代表已访问过 

 

  BFS伪代码    相当于一个模板    //网上找的 比较便于理解

 1 q.push(head);
 2 while(!q.empty()){
 3     temp=q.front();
 4     q.pop();
 5     if(temp为目标状态)  输出
 6     if(temp不合法)  continue;
 7     if(temp合法) q.push(temp+△);        
 8         //表示所有temp在搜索树中的所有子节点 
 9 } 

bfs广搜 利用队列实现 将当前搜索到的状态的每一子状态压入队列中 并进行记录 检查队列是否为空 如果不为空 就将队首元素弹出 并以这个状态位为根节点

进行 bfs 直到整个队列为空为止
 
  

 AC代码

 1 #include<iostream>
 2 #include<queue>
 3 using namespace std;
 4 queue <int> q;
 5 int n,k,temp,t,vis[200010],step[200010]; 
 6 
 7 void bfs(){
 8     q.push(n);
 9     vis[n]=1;
10     while(!q.empty()){
11         temp=q.front();
12         q.pop();
13         for(int i=0;i<3;i++){
14             if(i==0) t=temp-1;
15             if(i==1) t=temp+1;
16             if(i==2) t=temp*2;
17             if(vis[t]==0&&t<=100000){        //未被访问    注意注意!!!!!!要写上t<=100000 因为在×2中 数组会超限 产生 RE
18                 vis[t]=1;
19                 q.push(t); 
20                 step[t]=step[temp]+1;    //可以只用一个vis[]数组 vis[t]=vis[temp]+1; 
21                 if(t==k){
22                     cout<<step[t]<<endl;  //如果使用vis[]数组记录步数  需要cout<<vis[t]-1<<endl;  因为之前多+了 1 因此现在-去
23                 }
24             }
25         }
26     }
27 } 
28 
29 int main(){
30     cin>>n>>k;
31     if(n>=k) cout<<n-k<<endl;
32     else bfs(); 
33     return 0;
34 } 
AC代码
1
#include<iostream> 2 #include<queue> 3 using namespace std; 4 5 queue <int> q; 6 7 int n,k,temp,t,vis[200010]; 8 9 void bfs(){ 10 q.push(n); 11 vis[n]=1; 12 while(!q.empty()){ 13 temp=q.front(); 14 q.pop(); 15 int t1=temp-1; 16 int t2=temp+1; 17 int t3=temp*2; 18 if(t1==k){ 19 vis[t1]=vis[temp]+1; 20 cout<<vis[t1]-1<<endl; 21 return; 22 } 23 if(t2==k){ 24 vis[t2]=vis[temp]+1; 25 cout<<vis[t2]-1<<endl; 26 return; 27 } 28 if(t3==k){ 29 vis[t3]=vis[temp]+1; 30 cout<<vis[t3]-1<<endl; 31 return; 32 } 33 else{ 34 if(vis[t1]==0&&t1<=100000){ 35 vis[t1]=1; 36 q.push(t1); 37 vis[t1]=vis[temp]+1; 38 } 39 if(vis[t2]==0&&t2<=100000){ 40 vis[t2]=1; 41 q.push(t2); 42 vis[t2]=vis[temp]+1; 43 } 44 if(vis[t3]==0&&t3<=100000){ 45 vis[t3]=1; 46 q.push(t3); 47 vis[t3]=vis[temp]+1; 48 } 49 } 50 } 51 } 52 53 int main(){ 54 cin>>n>>k; 55 if(n>=k) cout<<n-k<<endl; 56 else bfs(); 57 return 0; 58 }
 

猜你喜欢

转载自www.cnblogs.com/jjjjjjy/p/11278473.html