HDU 1548 <BFS+标记+找最短>
来源:互联网 发布:淘宝的一元秒杀在哪里 编辑:程序博客网 时间:2024/06/14 02:32
the reason of failure:MLE,内存超出范围...一直看错题意,花了两个小时debug,可见题意的重要
thinking:由于电梯在i楼时向上是i+ki,向下是k-ki,那么每次在i发生的情况都一样,所以可以用walked标记走过
题意:有一个电梯有N层楼,从i到j最少需要按多少次电梯,每次按电梯向上或者向下,高度为ki,也就是说按了向上后高度变为i+ki,向下变为j+kj.
A strange lift
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
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
#include <iostream>#include <queue>#include <string.h>using namespace std;struct ttt{int high,id;};int l[205];bool mark[205];int main(){freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);int n,end,i,f4;ttt f2;ttt now1;queue<ttt>qq; bool f1;while(cin >> n&&n!=0){cin >> now1.high >> end;for(i=1;i<=n;i++)cin >> l[i];now1.id=0;f1=0;f4=-1;memset(mark,0,sizeof(mark));while(!qq.empty())qq.pop();qq.push(now1);mark[now1.high]=1;while(!qq.empty()&&now1.id<n+1){now1=qq.front();qq.pop();if(now1.high==end){f1=1;break;}else{now1.id++;f2=now1;f2.high=now1.high+l[now1.high];if(f2.high<=n&&!mark[f2.high]){qq.push(f2);mark[f2.high]=1;}f2.high=now1.high-l[now1.high];if(f2.high>0&&!mark[f2.high]){mark[f2.high]=1;qq.push(f2);}}}if(f1==1)cout << now1.id << endl;else cout << "-1" << endl;}}
0 0
- HDU 1548 <BFS+标记+找最短>
- HDU 1495 非常可乐(bfs+标记)
- HDU 1072不用标记的BFS
- hdu 1254(推箱子游戏bfs+状态标记)
- hdu 3345 War Chess【bfs+优先级队列or状态标记】
- HDU 2364 Escape (多维数组标记状态bfs)
- hdu Nightmare(BFS||DFS)(不标记的剪枝)
- hdu 2918 Tobo or not Tobo(BFS+状态标记)
- HDU 2579 Dating with girls(2) BFS+三重标记(标记时间)
- HDU-1548(BFS搜索)
- HDU 1548 BFS
- hdu 1548 BFS
- hdu 1548(bfs)
- hdu~1548(bfs)
- HDU 1548 BFS
- HDU-1548(BFS+剪枝)
- HDU 1548 BFS入门
- 图论 BFS HDU 1548
- Java内存泄漏与内存溢出
- JVM学习系列(一)——基础知识
- 年度最牛广告文案,看到最后我服了
- Android WebView中打开相机拍照和选择相册
- 骑士旅行问题-java解答
- HDU 1548 <BFS+标记+找最短>
- 《将博客搬至CSDN》
- maven仓库
- 利用润乾时间日期函数实现日期扩展功能
- MySQL常用命令总结
- Java中的final
- 打包文件
- maven详解之坐标与依赖
- poj Period 1961