http://poj.org/problem?id=3278(bfs)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 76935 | Accepted: 24323 |
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
Output
Sample Input
5 17
Sample Output
4
Hint
#include <iostream>
#include <string.h>
#include <queue>
#define MAXN 200000+10
using namespace std; struct Node //***用结构体表示节点,x表示当前值,step记录当前步数
{
int x, step;
}; int star, end, vis[MAXN]; int bfs(int x)
{
Node a, b, c;
a.x = x, a.step = ; //***赋初值
queue<Node>q; //***用队列实现
q.push(a); //***头节点入队
while(!q.empty()) //***如果队列不为空,继续循环
{
b = q.front(); //***取当前队列的头节点做父节点并将其从队列中删除
q.pop();
vis[b.x] = ; //***标记
if(b.x == end) //***如果达到目标,返回步数
{
return b.step;
}
c = b; //***符合条件的儿子入队
if(c.x >= && !vis[c.x-])
{
c.x-=;
c.step++;
vis[c.x]=;
q.push(c);
}
c = b;
if(c.x < end && !vis[c.x+])
{
c.x+=;
c.step++;
vis[c.x]=;
q.push(c);
}
c = b;
if(c.x < end && !vis[*c.x])
{
c.x*=;
c.step++;
vis[c.x]=;
q.push(c);
}
}
return -;
} int main(void)
{
while(cin >> star >> end)
{
if(star >= end) //***如果star>=end,则每次减1,最少需要star-end步
{
cout << star - end << endl;
continue;
}
memset(vis, , sizeof(vis)); //***标记数组清0,不然会对后面的测试产生影响
int ans=bfs(star); //***深搜
cout << ans << endl;
}
return ;
}
http://poj.org/problem?id=3278(bfs)的更多相关文章
- POJ3278http://poj.org/problem?id=3278
http://poj.org/problem?id=3278 题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步 #include<stdio.h> #include&l ...
- poj 1651 http://poj.org/problem?id=1651
http://poj.org/problem?id=1651Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K To ...
- poj-3056 http://poj.org/problem?id=3056
http://poj.org/problem?id=3056 The Bavarian Beer Party Time Limit: 6000MS Memory Limit: 65536K Tot ...
- poj 1679 http://poj.org/problem?id=1679
http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- poj 1915 http://poj.org/problem?id=1915
/**< */#include <stdio.h> #include <string.h> #include <stdlib.h> #include < ...
- 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258
#include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...
- Roadblocks http://poj.org/problem?id=3255
Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best ...
- http://poj.org/problem?id=2253
floyd的应用求每条路径两点之间最大距离的最小值 #include <iostream> #include <cstdio> #include <algorithm&g ...
- 线段树 (区间更新,区间查询) poj http://poj.org/problem?id=3468
题目链接 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
随机推荐
- jetbrick,新一代 Java 模板引擎,具有高性能和高扩展性
新一代 Java 模板引擎,具有高性能和高扩展性. <!-- Jetbrick Template Engineer --> <dependency> <groupId&g ...
- (一)css代码积累——自己经常忘记,但是总记不住的代码
1.透明度设置 90%透明:filter:alpha(opacity=90);-moz-opacity:0.90;-khtml-opacity: 0.90;opacity: 0.90; 80%透明:f ...
- BZOJ1146——[CTSC2008]网络管理Network
1.题目大意:就是在动态的树上路径权值第k大. 2.分析:这个就是树链剖分+树套树 #include <cstdio> #include <cstdlib> #include ...
- Struts2 验证规则配置文件
1. Action级别校验命名格式: ActionClassName-validation.xml 2. Action中某个方法的校验命名格式: ActionClassName-ActionAlias ...
- word20161201
http://baike.baidu.com/link?url=ZTTkA-suMlJNGb2AeNBE2E6MZQZwjkvWXKgmUpeLBIrCfC-k32cGJOJLrtDlLXjsTfkD ...
- jsoup解析HTML
Connection conn = Jsoup.connect(String url); conn.data("txtBill", key);// 设置关键字查询字段 Docume ...
- PHP获取IP地址
获取客户端IP地址:: function getIp(){ if(!empty($_SERVER['HTTP_CLIENT_IP'])){ return $_SERVER['HTTP_CLIENT_I ...
- ubuntu update dns server
edit: /etc/resolvconf/resolv.conf.d/base nameserver 114.114.114.114 execute this: $ resolvconf -u f ...
- POJ 1065
http://poj.org/problem?id=1065 题目的大体意思就是给一些木头长l,重w,有一个机器,如果切割这些木头的话,在i后面切割的i+1根木头满足长度和重量都要大于等于前一根则不需 ...
- Java RuntimeException异常处理汇总
Java中所有异常的父类是Throwable类,在Throwable类下有两大子类: 一个是Error类,指系统错误异常,例如:VirtualMachineError 虚拟机错误,ThreadDeat ...