hdu1548 A strange lift(bfs)
2016-11-20 02:43
323 查看
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22088 Accepted Submission(s): 8070
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 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
Sample Output
3
题意:有1~n层楼,每层可以上或者下一定的距离,问从a楼到b楼最少需要上下几次电梯。
题解:简单的bfs找最小的次数,bfs搜索的层数就是最小的次数,搜索不到就说明不能到那一层,输出-1。搜索完一层标记一下就行了。
代码:
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int f[1000];
int flag[1000];
int step[1000];
int n;
void bfs(int a, int b)
{
queue<int > q;
q.push(a);
flag[a] = 1;
int t, next;
while(!q.empty())
{
t = q.front();
q.pop();
if(t == b)
break;
next = t + f[t];
if(next > 0 && next <= n && flag[next] == 0)
{
q.push(next);
flag[next] = 1;
step[next] = step[t] + 1;
}
next = t - f[t];
if(next > 0 && next <= n && flag[next] == 0)
{
q.push(next);
flag[next] = 1;
step[next] = step[t] + 1;
}
}
}
int main()
{
int a, b;
while(cin >> n && n != 0)
{
cin >> a >> b;
for(int i = 1; i <= n; i++)
{
cin >> f[i];
}
memset(flag,0,sizeof(flag));
memset(step, 0, sizeof(step));
bfs(a, b);
if(flag[b])
cout << step[b] << endl;
else
cout << "-1" << endl;
}
return 0;
}
相关文章推荐
- hdu 1548 A strange lift(搜索:BFS)
- HDU 1548 A strange lift(Dijkstra,简单BFS)
- hdu 1548 A strange lift (BFS)
- Hdu1548 A strange lift【简单bfs】
- HDU 1548 A strange lift【BFS】
- HDU 1548 A strange lift (简单bfs)
- HDU 1548 A strange lift(Dijkstra、BFS、DP)
- hdu 1548 A strange lift bfs搜索 解题报告
- hdu 1548 A strange lift BFS 解法
- HDU 1548 A strange lift(Dijkstra、BFS、DP)
- HDU 1548 A strange lift(最短路或BFS)
- HDU 1548 A strange lift(BFS)
- hdu 1548 A strange lift(bfs)
- hdu 1548 A strange lift(BFS)
- hdu 1548 A strange lift (bfs)
- HDU 1548 A strange lift(BFS)
- HDU 1548 A strange lift bfs 宽搜解析
- HDU 1548 A strange lift(BFS)
- hdu 1548 A strange lift (bfs、最短路)
- Problem M:A strange lift(HDU 1548)(bfs实现)