hdu 1548 A strange lift(bfs)
来源:互联网 发布:win7网络共享打印机 编辑:程序博客网 时间:2024/05/20 21:19
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13821 Accepted Submission(s): 5274
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"?
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.
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 53 3 1 2 50
Sample Output
3
题目分析:裸的bfs
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <queue>#define MAX 207using namespace std;typedef pair<int,int> PII; int n,a,b;int k[MAX];int ans;bool used[MAX];int bfs ( ){ queue<PII> q; memset ( used , 0 , sizeof (used)); q.push( make_pair( 0 , a ) ); while ( !q.empty()) { int t = q.front().first; int x = q.front().second; q.pop(); if ( x == b ) return t; int tx = x+k[x]; if ( tx <= n && !used[tx] ) { q.push( make_pair(t+1,tx) ); used[tx] = 1; } tx = x-k[x]; if ( tx > 0 && !used[tx] ) { q.push( make_pair(t+1,tx) ); used[tx] = 1; } } return -1;}int main ( ){ while ( ~scanf ( "%d" , &n ) , n ) { scanf ( "%d%d" , &a , &b ); for ( int i = 1 ; i <= n ; i++ ) scanf ( "%d" , &k[i] ); printf ( "%d\n" , bfs() ); }}
0 0
- hdu 1548 A strange lift BFS 解法
- hdu 1548 A strange lift (bfs)
- hdu 1548 A strange lift (bfs)
- 【BFS/Dijkstra】hdu 1548 A Strange Lift
- hdu 1548 A strange lift (BFS、Dijkstra)
- 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)
- hdu 1548 A strange lift(bfs)
- bfs入门 hdu 1548 a strange lift
- hdu 1548 A strange lift(BFS)
- HDU 1548 A strange lift(BFS)
- Android Studio 结束 Gradle Task
- Andorid-15k+的面试题。
- Douglas-Peucker算法
- java词汇大全
- webserviceHTTP框架出错 404错误
- hdu 1548 A strange lift(bfs)
- hdu1160
- cocos2dx-2.x CCFileUtils文件管理类分析(4)
- Python_cmd的各种实现方法及优劣(subprocess.Popen, os.system和commands.getstatusoutput)
- 委托的使用
- mysql统计网站每天的访问量
- socket编程中的read、write与recv、send的区别
- PJSUA-API Media Manipulation 媒体操作api
- 返回指针的函数与指向函数的指针的用法