poj 3278 Catch That Cow bfs
2018-03-07 20:30
387 查看
Catch That Cow
DescriptionFarmer 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?
InputLine 1: Two space-separated integers: N and KOutputLine 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.Sample Input5 17Sample Output4HintThe 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.
题意:给你n和k
n有三种变化方式
n=n+1; n = n-1; n = n*2;
问你经过几次变化 n==k;
思路:这个题是归类到了bfs中 很好想 三方向的bfs
如果是直接碰到的话 可能想不出来.....
不过写完之后就re了...
坑点 n在变化的时候 n-- 和 n *= 2 的时候会发生数组越界 从而re 所以需要剪枝
ac代码#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int step[100005];
bool vis[100005];
queue<int>q;
int bfs(int n,int k)
{
int head,next;
q.push(n);
vis
= true;
step
= 0;
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)
{
if(head > 0)//剪枝
next = head - 1;
else
continue;
}
else if( i == 2)
{
if(head <= 50000)//剪枝
next = head*2;
else
continue;
}
if(!vis[next])
{
q.push(next);
step[next] = step[head] + 1;
vis[next] = true;
}
if(next == k)
a20b
return step[next];
}
}
}
int main()
{
int n,k;
while(~scanf("%d %d",&n,&k))
{
memset(vis,false,sizeof(vis));
memset(step,0,sizeof(step));
while(!q.empty())
q.pop();
if(n >= k)
printf("%d\n",n-k);
else
printf("%d\n",bfs(n,k));
}
return 0;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 106185 | Accepted: 33180 |
* 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?
InputLine 1: Two space-separated integers: N and KOutputLine 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.Sample Input5 17Sample Output4HintThe 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.
题意:给你n和k
n有三种变化方式
n=n+1; n = n-1; n = n*2;
问你经过几次变化 n==k;
思路:这个题是归类到了bfs中 很好想 三方向的bfs
如果是直接碰到的话 可能想不出来.....
不过写完之后就re了...
坑点 n在变化的时候 n-- 和 n *= 2 的时候会发生数组越界 从而re 所以需要剪枝
ac代码#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int step[100005];
bool vis[100005];
queue<int>q;
int bfs(int n,int k)
{
int head,next;
q.push(n);
vis
= true;
step
= 0;
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)
{
if(head > 0)//剪枝
next = head - 1;
else
continue;
}
else if( i == 2)
{
if(head <= 50000)//剪枝
next = head*2;
else
continue;
}
if(!vis[next])
{
q.push(next);
step[next] = step[head] + 1;
vis[next] = true;
}
if(next == k)
a20b
return step[next];
}
}
}
int main()
{
int n,k;
while(~scanf("%d %d",&n,&k))
{
memset(vis,false,sizeof(vis));
memset(step,0,sizeof(step));
while(!q.empty())
q.pop();
if(n >= k)
printf("%d\n",n-k);
else
printf("%d\n",bfs(n,k));
}
return 0;
}
相关文章推荐
- POJ - 3278-Catch That Cow(BFS)
- poj 3278 Catch That Cow bfs
- POJ 3278 Catch that cow 广度优先搜索bfs
- POJ 3278 Catch That Cow(BFS)
- POJ - 3278 Catch That Cow (BFS)
- POJ 3278 Catch That Cow(BFS 剪枝)
- POJ 3278 Catch That Cow(BFS,板子题)
- POJ---3278-Catch That Cow(BFS/deque)
- POJ 3278 Catch That Cow【BFS】
- POJ 3278 Catch That Cow(BFS广度优先搜索)
- POJ 3278 Catch That Cow bfs 难度:1
- POJ-3278-Catch That Cow-广搜(BFS)
- POJ 3278 Catch That Cow (BFS)
- POJ 3278 Catch That Cow (BFS)
- poj 3278 Catch That Cow (BFS)
- POJ 3278 && HDU 2717 Catch That Cow(bfs)
- poj 3278 Catch That Cow(bfs)
- POJ 3278 Catch That Cow (BFS)
- POJ 3278 Catch That Cow (简单BFS)
- 文章标题poj 3278 Catch That Cow(bfs实现)