poj 3278 队列+bfs
2014-04-29 21:45
120 查看
http://poj.org/problem?id=3278
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 X - 1 or X + 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
Sample Output
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.
用到了stl里面的队列,和bfs
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
using namespace std;
const int maxn=100005;
int visit[100005],num[100005];
queue<int> q;
int bfs(int n,int m)
{
int next;
int head;
q.push(n);
visit
=1;
while(!q.empty())
{
head=q.front();
q.pop();
for(int i=0; i<3; i++)
{
if(i==0)
{
next=head-1;
}
else if(i==1)
{
next=head+1;
}
else next=2*head;
if(next<0||next>=maxn)
continue;
if(!visit[next])//已经访问过了,说明在这次到达已到达过一次,这次绝对不是最短的了
{
visit[next]=1;
q.push(next);
num[next]=num[head]+1;
}
if(next==m)
return num[next];
}
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(visit,0,sizeof(visit));
memset(num,0,sizeof(num));
while(!q.empty())
q.pop();
if(n>=m)
printf("%d\n",n-m);
else
printf("%d\n",bfs(n,m));
}
return 0;
}
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 X - 1 or X + 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.
用到了stl里面的队列,和bfs
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
using namespace std;
const int maxn=100005;
int visit[100005],num[100005];
queue<int> q;
int bfs(int n,int m)
{
int next;
int head;
q.push(n);
visit
=1;
while(!q.empty())
{
head=q.front();
q.pop();
for(int i=0; i<3; i++)
{
if(i==0)
{
next=head-1;
}
else if(i==1)
{
next=head+1;
}
else next=2*head;
if(next<0||next>=maxn)
continue;
if(!visit[next])//已经访问过了,说明在这次到达已到达过一次,这次绝对不是最短的了
{
visit[next]=1;
q.push(next);
num[next]=num[head]+1;
}
if(next==m)
return num[next];
}
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(visit,0,sizeof(visit));
memset(num,0,sizeof(num));
while(!q.empty())
q.pop();
if(n>=m)
printf("%d\n",n-m);
else
printf("%d\n",bfs(n,m));
}
return 0;
}
相关文章推荐
- G - 诡异的楼梯 (优先队列 BFS)
- hdu1026 Ignatius and the Princess I (优先队列 BFS)
- 循环队列(BFS所用到队列优化成循环队列。)
- hdu 4158 go! 队列+BFS
- HDOJ 1026 Ignatius and the Princess I (BFS优先队列超时 TLE)
- HDU 1242 Rescue【优先队列BFS】
- 【dijkstra + 优先队列 && bfs && A*】POJ - 2449 Remmarguts' Date
- 图的宽度优先搜索(BFS)以及C++队列知识复习
- ZOJ 2913 Bus Pass(BFS+滚动队列)
- POJ 3278 Catch That Cow (队列+bfs)
- ZOJ-2849-Attack of Panda Virus【bfs】【优先级队列】【4th浙江省赛】【好题】
- [uva-417] Word Index (BFS、队列
- poj 3635 Full Tank? (优先队列 + bfs)
- hdu1072 Nightmare(优先队列,BFS)
- PKU I Wanna Go Home 3767 Dijsktra 优先队列 + BFS
- 【BFS+优先级队列】Rescue
- hdu 2824 Dogs(BFS, 优先队列优化)
- HDU1242 rescue 【BFS+优先队列】
- hdu 1242 Rescue(优先队列 && 广搜BFS)
- poj3278 Catch That Cow (BFS算法和队列)