HDU 1548 A strange lift 搜索
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11341 Accepted Submission(s): 4289
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"?
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 5
3 3 1 2 5
0
3
#include<iostream>
#include<cstring>
#include<queue>
using namespace std; #define N 205
int v[N],c[N];
int n,a,b,t; struct node
{
int i,time;
}; void bfs(int x)
{
node now,tmp;
queue<node> q;
now.i=x,now.time=0;
memset(v,0,sizeof(v));
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.i==b)
{
t=0;
cout<<now.time<<endl;
return ;
}
for(int k=0;k<2;k++)
{
if(k==0) tmp.i=now.i+c[now.i];
if(k==1) tmp.i=now.i-c[now.i];
if(tmp.i>0&&tmp.i<=200&&!v[tmp.i])
{
v[tmp.i]=1;
tmp.time=now.time + 1;
q.push(tmp);
}
}
}
} int main()
{
while(cin>>n)
{
if(n==0) break;
cin>>a>>b;
for(int k=1;k<=n;k++)
cin>>c[k];
t=1;
bfs(a);
if(t) cout<<-1<<endl;
}
return 0;
}
HDU 1548 A strange lift 搜索的更多相关文章
- hdu 1548 A strange lift
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...
- hdu 1548 A strange lift 宽搜bfs+优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...
- HDU 1548 A strange lift (Dijkstra)
A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...
- HDU 1548 A strange lift (bfs / 最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...
- hdu 1548 A strange lift (bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 1548 A strange lift 题解
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1548 A strange lift (最短路/Dijkstra)
题目链接: 传送门 A strange lift Time Limit: 1000MS Memory Limit: 32768 K Description There is a strange ...
- HDU 1548 A strange lift(BFS)
Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...
- HDU 1548 A strange lift (广搜)
题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...
随机推荐
- 【BZOJ1500】【块状链表】维修数列
Description Input 输入文件的第1行包含两个数N和M,N表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一条命令,格式参见问题描述 ...
- python split()黑魔法
split()用法: #!/usr/bin/python str = "Line1-abcdef \nLine2-abc \nLine4-abcd"; print str.spli ...
- 【C#枚举】根据EnumName获取Value
public static int GetEnumValue(Type enumType, string enumName) { try { if (!enumType.IsEnum) throw n ...
- Java导出Excel和CSV(简单Demo)
Java导出Excel和CSV的简单实现,分别使用POI和JavaCSV. JavaBean public class ReportInfo { int id; String date; int nu ...
- [Winfrom] 捕获窗体最大化、最小化和关闭按钮的事件
const int WM_SYSCOMMAND = 0x112;const int SC_CLOSE = 0xF060;const int SC_MINIMIZE = 0xF020;const int ...
- linux下gdal的python包的安装
由于python包是从C++包编译出来的,所以需要先下载源码进行编译安装.1. gdal下载http://download.osgeo.org/gdal/CURRENT/sudo ./configur ...
- HashMap在Android和Java中的不同实现
起因 今天在项目中遇到一个很"奇葩"的问题.情况大致是这样的:Android终端和服务器(Spring),完全相同的字符串键值对放入HashMap中竟然顺序不一样,这直接导致了服务 ...
- uboot环境变量分析
uboot的环境变量在程序的运行和调试过程中都发挥着比较重要的作用. 一.环境变量 可以理解为全局变量,但是他的生命周期比全局变量要长,当程序已经结束运行时,全局变量就会消亡,但是环境变量在下次上电运 ...
- POJ 1321-棋盘问题(DFS 递归)
POJ 1321-棋盘问题 K - DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- 使用ToUpperInvariant避免使用ToUpper
ToUpperInvariant使用不依赖于区域性进行转换,而ToUpper则使用了当前线程的CultureInfo,进行转换,所以性能会有所影响,以下为测试: [Test] public void ...