HDU 1548 A strange lift (简单BFS)

来源:互联网 发布:古装网络剧自制剧男同 编辑:程序博客网 时间:2024/05/20 19:19

A strange lift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26948    Accepted Submission(s): 9702



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 53 3 1 2 50
 

Sample Output
3
 

Recommend
8600
 
本来理解了题目,这道题就是一道BFS搜索水题了,但是自己却一直内存超限,后来发现,每一层楼不能是不能重复进入的,一旦重复进入肯定会陷入死循环,从而导致多次入队然后导致系统栈溢出,所以要加上一个访问数组记录每一层是否已经被访问过。
以为到这就OK了,还有一个低级错误导致了多次WA。。。。。
详情看代码。

#include <iostream>#include <cstring>#include <stack>#include <cstdio>#include <cmath>#include <queue>#include <algorithm>#include <vector>#include <set>#include <map>const double eps=1e-8;const double PI=acos(-1.0);using namespace std;struct st{    int floor;    int step;}node;int a[10005];int v[10005];int n,s,e;int bfs(){    queue<st>q;    node.floor=s;    node.step=0;    q.push(node);    v[node.floor]=1;    while(!q.empty())    {        st tem=q.front();        q.pop();        st tp;         if(tem.floor==e)            {                return tem.step;//cout<<"t:"<<e<<endl;            }        for(int i=0; i<2; i++)        {            if(!i)                tp.floor=tem.floor+a[tem.floor-1];            else                tp.floor=tem.floor-a[tem.floor-1];            if(tp.floor<1||tp.floor>n||v[tp.floor]) continue;            tp.step=tem.step+1;//多次wa的地方,之前写的:tp.step=++temp.step;这样写不要以为跟AC代码写的一样的!很低级的错误,这样写,每次就修改了temp的step的值,从而导致wa            q.push(tp);            v[tp.floor]=1;            //cout<<tp<<endl;        }    }    return -1;}int main(){    while(~scanf("%d",&n)&&n)    {        scanf("%d %d",&s,&e);        memset(a,0,sizeof(a));        memset(v,0,sizeof(v));        for(int i=0; i<n; i++)            scanf("%d",&a[i]);        printf("%d\n",bfs());    }    return 0;}


原创粉丝点击