poj 3278
来源:互联网 发布:ipad淘宝网不能横屏 编辑:程序博客网 时间:2024/05/17 23:53
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
Output
Sample Input
5 17
Sample Output
4
Hint
Source
第一篇题解,写一道简单的试试水 -_-||
题意:很简单,就是农夫抓牛,告诉你牛的位置和农夫的位置,然后有两种走法,第一种是从当前位置x走到x-1或者x+1,另外一种是走到2*x位置。
题目很简单,没什么说的,直接上代码~
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<queue>using namespace std;#define maxn 1000500struct point { int x;//表示坐标 int step;//表示当前的步数};point start;queue<point> Q;bool used[maxn];bool flag;int fj, cow;int mint;void bfs( point s ){ while( !Q.empty() ) Q.pop();//记得清空队列,为此贡献n次wa int x; Q.push(s); used[fj] = 1; flag = 0; while( !Q.empty() && !flag )//为了节约时间,达到目标位置就break,退出循环。 { point hd = Q.front(); // printf("%d\n", hd.x); Q.pop(); for( int i=0; i<3; i++ ) { if( i == 0 ) x = hd.x - 1; else if( i == 1 ) x = hd.x + 1; else if( i == 2 ) x = hd.x * 2;//三种走法。 if( x>=0 && x <= 1000000 && !used[x] ) { used[x] = 1; point t; t.x = x; t.step = hd.step + 1; Q.push( t ); if( x == cow ) { mint = t.step; flag = 1; break; } } } }}int main(){ while( scanf("%d%d", &fj, &cow) != EOF ) { if( fj == cow ) { printf("0\n"); continue; } start.x = fj; start.step = 0; memset( used, 0, sizeof(used) ); bfs( start ); printf("%d\n", mint); } return 0;}
- POJ 3278
- POJ 3278
- POJ 3278
- POJ 3278
- poj 3278
- poj 3278
- POJ 3278
- poj 3278
- POJ 3278
- poj 3278
- poj 3278
- poj 3278
- poj-3278
- poj--3278
- poj-3278
- poj 3278
- poj 3278
- poj 3278
- js 大整数相加
- 《小团队项目管理》第一问 --- 需求调研需要舌辨群雄?
- CentOS自动登陆
- android SharedPreferences 数据存储
- jQuery+ashx实现加载更多(C#)
- poj 3278
- 最近公共祖先(Tarjan算法)
- TCP/UDP通信模型
- 如何修改计算机名,才能用该用户名远程
- C语言基础知识之#pragma once
- 提示框
- DWR A request has been denied as a potential CSRF atta 的两种情况
- UIViewController 视图控制器
- leetcode之Triangle