A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3839 Accepted Submission(s): 1406
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 Ki floor,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 is on floor A,and you want to go to floor B,how many times at least he havt 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
Sample Output
这题是最简单最纯粹的bfs广搜,只要作一下标记去过的地方,不能再去,就OK啦!
代码:
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <queue>
using namespace std;
int N, A, B;
int a[205];
bool map[205], flag;
struct node
{
int x, step;
}n1, n2, m;
int main()
{
int i;
while(scanf("%d", &N), N)
{
if(N == 0) break;
scanf("%d %d", &A, &B);
for(i = 1; i <= N; i++)
{
scanf("%d", &a[i]);
map[i] = false;
}
flag = false;
n1.x = A; n1.step = 0;
queue<node> Q;
Q.push(n1);
map[n1.x] = true;
while(!Q.empty())
{
m = Q.front();
Q.pop();
if(m.x == B) //到达目标
{
flag = true;
break;
}
n1.x = m.x - a[m.x];
n2.x = m.x + a[m.x];
if(n1.x>0 && n1.x<=B && !map[n1.x]) //下去的
{
n1.step = m.step + 1;
map[n1.x] = true; //标记
Q.push(n1);
}
if(n2.x>0 && n2.x<=B && !map[n2.x]) //上去的
{
n2.step = m.step + 1;
map[n2.x] = true; //标记
Q.push(n2);
}
}
if(flag) printf("%d\n", m.step);
else printf("-1\n");
}
return 0;
}
分享到:
相关推荐
hdu 3085 一道bfs题 一点值得注意的,鬼魅先走,mm与gg后走,直接模拟这个这个场景,突出时间的先后性,一秒与一秒的区别。。。这样模拟很难出错
1548 --- basic bfs 2. BNU 1440 --- basic dfs 3. POJ 1190 --- dfs + pruning(Strong) 4. UVALive 2243 --- dfs 5. FZU 2196 --- double bfs 6. PKU 1426 --- bfs + pruning 7. BNU 1038 --- dfs transform 8. PKU...
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
hdu ACM 各种排序
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu_2102_passed_sorce
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
hdu 1005.比较简单的一道题,有兴趣的可以看看。
现在,给你两个正的小数A和B,你的任务是代表大明计算出A+B的值。 Input 本题目包含多组测试数据,请处理到文件结束。 每一组测试数据在一行里面包含两个长度不大于400的正小数A和B。 Output 请在一行里面...
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 1166线段树代码