hdu1548
来源:互联网 发布:编程语言执行效率排行 编辑:程序博客网 时间:2024/05/29 18:57
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"?
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.
5 1 53 3 1 2 50
3
解题思路:
这道题虽然就是一个bfs,开始的时候也是正常写,但是没有做vis标记,当时不觉得需要用vis(但是实际上是需要的,因为如果重复走了一个台阶的话,就不是最优的情况了,虽然能得出答案)交了之后,就mle,于是加了限制条件剪枝,但是没有考虑清楚加错了,最后还是老实加了vis数组,其实就是一个很简单的题目,一直想偏,一直也过不了
代码:
#include<iostream>#include<cstring>#include<queue>#include<cmath>using namespace std;int n,beginn,endd;struct node{ int pos; int go; int num;};node a[250];int vis[250];int bfs(node now){ queue<node> q; q.push(now); vis[now.pos] = 1; node help; node help1; node help2; while(!q.empty()) { // cout<<"here"<<endl; help = q.front(); q.pop(); if(help.pos==endd) return help.num; help1.pos = help.pos+help.go; if(help1.pos==endd) return help.num+1; if(help1.pos>=1&&help1.pos<=n&&!vis[help1.pos]) { help1.go = a[help1.pos].go; help1.num = help.num+1; q.push(help1); vis[help1.pos] = 1; } help2.pos = help.pos-help.go; if(help2.pos==endd) return help.num+1; if(help2.pos>=1&&help2.pos<=n&&!vis[help2.pos]) { // cout<<"here"<<endl; help2.go = a[help2.pos].go; help2.num = help.num+1; q.push(help2); vis[help2.pos] = 1; } } return -1;}int main(){ while(cin>>n) { memset(vis,0,sizeof(vis)); if(n==0) break; cin>>beginn; cin>>endd; for(int i = 1;i<=n;i++) { cin>>a[i].go; a[i].pos = i; a[i].num = 0; } node c; c.go = a[beginn].go; c.pos = beginn; c.num = 0; int re = bfs(c); cout<<re<<endl; }}
阅读全文
0 0
- hdu1548
- hdu1548
- hdu1548
- hdu1548
- hdu1548
- hdu1548
- HDU1548
- hdu1548
- hdu1548
- hdu1548
- HDU1548 BFS
- HDU1548(搜索)
- hdu1548(BFS)
- hdu1548 A strange lift
- HDU1548 A strange lift
- HDU1548 A strange lift
- hdu1548 A strange lift
- HDU1548---A strange lift
- tensorflow构建神经网络及可视化
- jazz数据集处理
- Promise和异步编程
- Android页面布局视图
- 【HDU-1213】How Many Tables
- hdu1548
- Linux下打开超大文件方法
- mysql查询数据的同时对它进行删除操作
- [PAT乙级]1027. 打印沙漏(20)
- LeetCode_Easy心得:35. Search Insert Position(C语言)
- linux 使用find命令查找包含某字符串的文件
- JSON.parse()和JSON.stringify()
- PHP递归逆置字符串
- Java多线程的调度与控制