Catch That Cow && A strange lift
2013-07-23 08:27
127 查看
Catch That Cow
Time Limit : 5000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)Total Submission(s) : 35 Accepted Submission(s) : 11
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem 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 Johnhas 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 KOutput
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
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
typedef struct Node
{int x;
int sum;
}Node;
int m,n;
int flag[200000];
int BFS()
{Node p,q;
queue<Node> qu;
p.x=m;
p.sum=0;
qu.push(p);
while(!qu.empty())
{ p=qu.front();
qu.pop();
if(m==n) return 0;
if(p.x==n) return p.sum;
p.sum++;
q=p;
q.x=q.x*2;
if(q.x>=0&&q.x<=100000&&flag[q.x]){flag[q.x]=0;
qu.push(q);}
q=p;
q.x--;
if(q.x>=0&&q.x<=100000&&flag[q.x]) {flag[q.x]=0;qu.push(q);}
q=p;
q.x++;
if(q.x>=0&&q.x<=100000&&flag[q.x]) { flag[q.x]=0; qu.push(q);}
}
}
main()
{int a;
while(scanf("%d%d",&m,&n)!=EOF)
{memset(flag,1,sizeof(flag));
a=BFS();
printf("%d\n",a);}
return 0;
}
A strange lift
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 54 Accepted Submission(s) : 14
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up Kifloor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding
with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as
you know ,the -2 th floor isn't exist.
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at least he has to press the button "UP" or "DOWN"?
Input
The input consists of several test cases.,Each test case contains two lines.The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
A single 0 indicate the end of the input.
Output
For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can't reach floor B,printf "-1".Sample Input
5 1 5 3 3 1 2 5 0
#include <stdio.h>
#include <iostream>
#include <queue>
using namespace std;
int n,a,b;
bool map[205],flag;
int c[205];
struct node
{
int x;
int step;
}mp,n1,n2;
int bfs()
{
queue<node>q;
mp.x=a;
mp.step=0;
q.push(mp);
map[mp.x]=true;
while(!q.empty())
{
mp=q.front();
q.pop();
if(mp.x==b) {flag=true; return mp.step;}
n1.x=mp.x+c[mp.x];
n2.x=mp.x-c[mp.x];
if(n1.x>0&&n1.x<=b&&!map[n1.x])
{
n1.step=mp.step+1;
map[n1.x]=true;
q.push(n1);
}
if(n2.x>0&&n2.x<=b&&!map[n2.x])
{
n2.step=mp.step+1;
map[n2.x]=true;
q.push(n2);
}
}
}
int main()
{
int i;
while(scanf("%d",&n))
{
if(n==0) {break;}
scanf("%d%d",&a,&b);
for(i=1;i<=n;i++)
{
scanf("%d",&c[i]);
map[i]=false;
}
flag=false;
bfs();
if(flag)
{
printf("%d\n",mp.step);
}
else
{
printf("-1\n");
}
}
return 0;
}
相关文章推荐
- Jeff Deyo & Sonicflood - "Lord I Lift Your Name On High"(主我高举你的名)
- 搜索—Problem_1013&1014-A strange lift
- HDU 1548---A strange lift(搜索&&最短路)
- HDU 1548 A strange lift(最短路&&bfs)
- 杭电1548--A strange lift(BFS)
- hdu 1548 A strange lift <java>
- 1014 A strange lift
- HDU 1548 A strange lift
- 算法训练 Lift and Throw (DFS && 位运算)
- A strange lift
- HDU1548:A strange lift(Dijkstra && BFS)
- A strange lift (关键构图,电梯升降)
- A strange lift宽度搜索优先队列
- c/c++编程基础篇之浅析堆&栈
- "耽美" "同人"
- linux ipc &pipe fifo
- 【词汇】Sprint & Linux Mint & Google Chrome & 802.11n标准发展大事记 &Linux 发行版 & Live CD & 流行的发行版
- 实现"通过串口升级嵌入式目标板软件"功能的一些心得体会
- Policy didn't be pushed to users' notes client
- OpenGL开发&学习_1_环境配置