原来这个叫做广搜啊。。。
来源:互联网 发布:网络安全工程师学什么 编辑:程序博客网 时间:2024/04/28 11:14
题目来源 poj3278 ;
Catch That Cow
Time Limit: 2000MSMemory Limit: 65536K
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?
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 fastes 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 <queue>库函数的一些用法。。
代码:
#include <iostream>#include <queue>#include <cstdio>#include <cstring>using namespace std;int main(){int line[100001]={0};bool sign[100001]={0};queue<int>x; // 创建一个int型的队列。。int n,k;cin>>n>>k; x.push(n); // 将n压入队列x队首。 line[n]=0; while(!x.empty()){ int y=x.front() ; //将x的队头赋值给y。; x.pop(); //上一步没有将队头取出,这一步取出队头。 sign[n]=true; //初始位置设为走过。 if(y==k) break; else {if(y>=1&&!sign[y-1]) {x.push(y-1); //将y-1压入x队首。line[y-1]=line[y]+1;sign[y-1]=true; }if(y+1<=100000&&!sign[y+1]) {x.push(y+1);line[y+1]=line[y]+1;sign[y+1]=true; }if(y<=50000&&!sign[y*2]) {x.push(y*2);line[y*2]=line[y]+1;sign[y*2]=true; } }}printf("%d\n",line[k]);return 0;}
- 原来这个叫做广搜啊。。。
- 原来有种神兽叫做古鸽
- 这个世界上有一个故事,叫做《大话西游》。
- 这个世界上还有一个故事,叫做《大话西游》
- 这个世界上还有一个故事,叫做《大话西游》
- 【学记】这个部分叫做熬
- 原来还有这个功能啊!!!
- 原来是灰尘这个杀手
- 原来是这个样子的!
- 原来这个是第一天。
- 原来这个分类是powerdesigner
- linux原来是这个样子!
- 原来封装是这个意思啊!
- 原来数组指针和这个定义等价!
- 原来位图是这个样子的阿
- [转]原来位图是这个样子的
- PHP原来有php_strip_whitespace这个函数啊~
- 原来位图是这个样子的阿
- Cisco ASA在线配置手册
- 好久没来了。
- TCP/IP协议笔记
- 大规模tcp并连接支持的思路
- More Effective C++摘录
- 原来这个叫做广搜啊。。。
- 多线程编程技术
- C# FileStream 按大小分段读取文本内容
- php mysql数据清零问题
- gtest中的字符串比较断言
- vc中用SendMessage自定义消息传递
- Linux 的多线程编程的高效开发经验
- dlmalloc
- javascript设置 asp:Image src的值,在服务器端取不到imageUrl的值