A strange lift
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 148 Accepted Submission(s) : 67
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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 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
Sample Input
5 1 5 3 3 1 2 5 0
Sample Output
3
#include<iostream>
#include<cstdio>
#include<map>
#include<queue>
int n, x, y, a[201];
using namespace std;
struct node
{
int h, step;
};
int main()
{
while (scanf_s("%d", &n) != EOF)
{
if (n == 0) break;
scanf_s("%d%d", &x, &y);
for (int i = 1; i <= n; i++)
scanf_s("%d", &a[i]);
queue<node> P;
node dc; dc.h = x, dc.step = 0; P.push(dc);
bool bb = 0;
while (!P.empty())
{
node dr = P.front(); P.pop();
if (dr.h == y)
{
printf("%d\n", dr.step);
bb = 1; break;
}
if (a[dr.h])
{
if (dr.h + a[dr.h] <= n)
{
node dd;
dd.h = dr.h + a[dr.h]; dd.step = dr.step + 1;
P.push(dd);
}
if (dr.h - a[dr.h] >= 1)
{
node dd;
dd.h = dr.h - a[dr.h]; dd.step = dr.step + 1;
P.push(dd);
}
a[dr.h] = 0;
}
}
if (bb == 0) printf("-1\n");
while (!P.empty()) P.pop();
}
return 0;
}
奇怪电梯的算法挑战
本文介绍了一种特殊电梯的工作原理,电梯只能上行或下行特定楼层数。文章提出了一道算法问题:从任意楼层A出发,最少需要按多少次按钮才能到达目标楼层B。通过广度优先搜索算法给出了解决方案。
1122

被折叠的 条评论
为什么被折叠?



