POJ 3278/HDU2717 —Catch That Cow (BFS)(另附测试数据)

本文深入探讨了BFS(宽度优先搜索)算法在解决特定类型问题中的应用,特别是通过一个具体的实例——追捕逃逸奶牛的问题,展示了如何使用BFS算法找到最短路径。文章详细解释了算法的实现过程,包括如何利用队列进行节点遍历,以及如何避免重复搜索等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当时我们校内比赛 我一看就是一道搜索题 结果没认真看 用的DFS怎么查 也查不出来  课下用的BFS做出来的 (PS:以后一定要看清题 求得是 最短 最少    一定要用BFS  别用DFS

 

/*
测试数据:
1 1
0
1 5
3
5 1
4
1 100000
21
200 100000
16
13 15000
15
21 4500
14
64 7913
12
15 438
8
159 753
32
486 4267
52
147 963
31
486 15347
14

*/

 

Catch That Cow

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 135540 Accepted: 41919

Description

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.

/*
测试数据:
1 1
0
1 5
3
5 1
4
1 100000
21
200 100000
16
13 15000
15
21 4500
14
64 7913
12
15 438
8
159 753
32
486 4267
52
147 963
31
486 15347
14

*/

#include<queue>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define M 1000100
int vis[M];
int n,k;

bool check(int x){
	
	if(x<0 || x>M || vis[x])
	return false;
	return true;
}

struct node
{
	int x;
	int step;
//	node(int xx,int ss){
//		x=xx;
//		step=ss;
//	}
};



int  bfs(int x)  
{ 
	queue<node> q;
	vis[x]=1;
	node a,b;
	a.x=x;
	a.step=0;
	
	q.push(a);	
	while(!q.empty())
	{
		a = q.front();
		q.pop();
		if(a.x == k)  return a.step;
		b.step = 0;
		b.x=a.x-1;
		if(check(b.x) ){
			vis[b.x]=1;
			b.step =a.step+1;
			q.push(b);
		}
		b.x=a.x+1;
		if(check(b.x)){
			vis[b.x]=1;
			b.step =a.step+1;
			q.push(b);
		}
		b.x=a.x*2;
		if(check(b.x)){
			vis[b.x]=1;
			b.step =a.step+1;
			q.push(b);
		}
	  	
	}
	return -1;	
}

int main()
{
	while(scanf("%d%d",&n,&k)!=EOF)
	{
		memset(vis,0,sizeof(vis));
		
		printf("%d\n",bfs(n));
	
	}	
	return 0;
 } 

这道题写的我很气 明明 ‘queue<node> q;’  可以写bfs函数上面  也可以写在bfs函数里面 可是hdu偏偏不行 必须写到bfs函数里面 要么就WA 我找了俩小时 提交了快20次 才知道我错哪儿了

but  poj就行 (滑稽.jpg)

 也可以参照我另一个题的解法 https://blog.youkuaiyun.com/intmain_S/article/details/89243678 

但是要额外考虑 N>K  N=K 的情况

代码如下: 

#include<queue>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAX 1000100
int vis[MAX],num[MAX];
int N,K;
int ans,to;
 
bool check(int x)
{
	if(x<=0 || x>=MAX || vis[x])
	return false;
	return true;
} 
 
struct node
{
	int n;
	int step;
	node(int xx,int ss){
		n=xx;
		step =ss;
		
	}
};
 
queue <node>  q;
void bfs()
{
	q.push(node(N,0));
	while(!q.empty())
	{
		node now =q.front();
		q.pop();
		if(to==K) break;
		to=now.n+1;
		if(check(to)){
			vis[to]=1;
			num[to]=now.step+1;
			q.push(node(to,now.step+1));
		}
		to=now.n-1;
		if(check(to)){
			vis[to]=1;
			num[to]=now.step+1;
			q.push(node(to,now.step+1));
		}
		to=now.n*2;
		if(check(to)){
			vis[to]=1;
			num[to]=now.step+1;
			q.push(node(to,now.step+1));
		}
	}
	
	
}
 
int main()
{
	scanf("%d%d",&N,&K);
		if(N==K)  {
			printf("0\n");
			return 0;
		}
		if(N>K)  {
			printf("%d\n",N-K);
		return 0;
		}
		memset(vis,0,sizeof(vis));
		bfs();
		printf("%d\n",num[K]);
	
	return 0;

}

 

#include<queue>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAX 1000100
int vis[MAX],num[MAX];
int N,K;
int ans,to;
 
bool check(int x)
{
	if(x<=0 || x>=MAX || vis[x])
	return false;
	return true;
} 
 
struct node
{
	int n;
	int step;
	node(int xx,int ss){
		n=xx;
		step =ss;
		
	}
};
 
queue <node>  q;
void bfs()
{
	q.push(node(N,0));
	while(!q.empty())
	{
		node now =q.front();
		q.pop();
		if(now.n==K){
			
			ans = now.step;
			break;
		}
		to=now.n+1;
		if(check(to)){
			vis[to]=1;
			//num[to]=now.step+1;
			q.push(node(to,now.step+1));
		}
		to=now.n-1;
		if(check(to)){
			vis[to]=1;
			//num[to]=now.step+1;
			q.push(node(to,now.step+1));
		}
		to=now.n*2;
		if(check(to)){
			vis[to]=1;
			//num[to]=now.step+1;
			q.push(node(to,now.step+1));
		}
	}
	
	
}
 
int main()
{
	scanf("%d%d",&N,&K);
		if(N==K)  {
			printf("0\n");
			return 0;
		}
		if(N>K)  {
			printf("%d\n",N-K);
		return 0;
		}
		memset(vis,0,sizeof(vis));
		ans = 0;
		bfs();
		printf("%d\n",ans);
	
	return 0;
 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值