poj3278 BFS入门
2016-03-23 18:46
239 查看
M - 搜索
Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 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.
题目大意:农夫去追他跑丢的牛,题目给出了他和牛的位置,用数字N和K表示,假定牛不动,问农夫移动到牛的位置的最小步数,农夫每次的移动有三种选择:位置加1,位置减1,位置乘2.
思路分析:求最小步数,用BFS即可水过,不过由于本弱BFS刚刚入门,在做题的时候还是出现了很多问题,BFS一般要采用队列来进行实现,刚开始使用的是queue<int>队列,但是在对步数的保存上出现了问题,这时候我选择了queue<pair <int,int> >来记录位置和步数,每次将n+1,n-1,n*2都压入到队列当中去,直到n==m,结束。程序可以运行,样例的输出答案也是正确的,但是提交的时候MLE了,这让我明白了剪枝的重要性,最后程序在两个方面进行了剪枝,一个是建立一个标记数组,对搜索到的每一点都进行标记,避免重复搜索,第二就是对于n>m的情况,毫无疑问应该采取n-1。应用了这两点剪枝策略以后,成功A掉。
代码:#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
const int maxn=4e5+5;
int n,m;
queue<pair<int,int> >q;
bool hash[maxn];
int main()
{
int m,n;
while(cin>>n>>m)
{
memset(hash,false,sizeof(hash));
pair<int,int> p;
p.first=n;
p.second=0;
hash
=true;
q.push(p);
while(!q.empty())
{
pair<int,int> a,b;
a=q.front();
if(a.first==m)
{
cout<<a.second<<endl;
break;
}
a.second++;
b=a;
if(b.first>m)
{
b.first=a.first-1;
if(hash[b.first]==false)
{
hash[b.first]=true;//标记该点,表示已经走过
q.push(b);
}
}
if(b.first<m)
{
b.first=a.first+1;
if(hash[b.first]==false)
{
hash[b.first]=true;//标记该点,表示已经走过
q.push(b);
}
b.first=a.first*2;
if(hash[b.first]==false)
{
hash[b.first]=true;
q.push(b);
}
b.first=a.first-1;
if(hash[b.first]==false)
{
hash[b.first]=true;
q.push(b);
}
}
q.pop();//弹出队列第一个元素
}
while(!q.empty())
q.pop();//注意清空队列
}
return 0;
}
梦想起航!加油!
Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 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
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.
题目大意:农夫去追他跑丢的牛,题目给出了他和牛的位置,用数字N和K表示,假定牛不动,问农夫移动到牛的位置的最小步数,农夫每次的移动有三种选择:位置加1,位置减1,位置乘2.
思路分析:求最小步数,用BFS即可水过,不过由于本弱BFS刚刚入门,在做题的时候还是出现了很多问题,BFS一般要采用队列来进行实现,刚开始使用的是queue<int>队列,但是在对步数的保存上出现了问题,这时候我选择了queue<pair <int,int> >来记录位置和步数,每次将n+1,n-1,n*2都压入到队列当中去,直到n==m,结束。程序可以运行,样例的输出答案也是正确的,但是提交的时候MLE了,这让我明白了剪枝的重要性,最后程序在两个方面进行了剪枝,一个是建立一个标记数组,对搜索到的每一点都进行标记,避免重复搜索,第二就是对于n>m的情况,毫无疑问应该采取n-1。应用了这两点剪枝策略以后,成功A掉。
代码:#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
const int maxn=4e5+5;
int n,m;
queue<pair<int,int> >q;
bool hash[maxn];
int main()
{
int m,n;
while(cin>>n>>m)
{
memset(hash,false,sizeof(hash));
pair<int,int> p;
p.first=n;
p.second=0;
hash
=true;
q.push(p);
while(!q.empty())
{
pair<int,int> a,b;
a=q.front();
if(a.first==m)
{
cout<<a.second<<endl;
break;
}
a.second++;
b=a;
if(b.first>m)
{
b.first=a.first-1;
if(hash[b.first]==false)
{
hash[b.first]=true;//标记该点,表示已经走过
q.push(b);
}
}
if(b.first<m)
{
b.first=a.first+1;
if(hash[b.first]==false)
{
hash[b.first]=true;//标记该点,表示已经走过
q.push(b);
}
b.first=a.first*2;
if(hash[b.first]==false)
{
hash[b.first]=true;
q.push(b);
}
b.first=a.first-1;
if(hash[b.first]==false)
{
hash[b.first]=true;
q.push(b);
}
}
q.pop();//弹出队列第一个元素
}
while(!q.empty())
q.pop();//注意清空队列
}
return 0;
}
梦想起航!加油!
相关文章推荐
- 注册功能
- C语言:全局变量在多个c文件中公用的方法 [转]
- IP数据报的格式
- 常用的Git代码托管平台
- HashSet,TreeSet和LinkedHashSet的区别
- Android Fragment 真正的完全解析(上)
- 近5年126个Java面试问题汇总 收藏
- C++扬帆远航——12(抓小偷)
- Ajax学习笔记
- C# 编写计算器
- 第4周项目1-(2)
- 破解Android或IOS游戏包时,将原本的黑白图(png文件),彩色图(jpg)文件,合成为一张png文件
- vector中插入pair
- 如何设置DNS的SPF记录
- Nodejs学习笔记(十四)— Mongoose介绍和入门
- 51nod--1134 最长递增子序列 (动态规划)
- file的getPath getAbsolutePath和getCanonicalPath的不同
- 文件处理之浏览文件
- SpringMVC之application-context.xml,了解数据库相关配置
- RadioButton使用方法