A strange lift

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 64   Accepted Submission(s) : 29

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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 5
3 3 1 2 5
0

Sample Output

3

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<climits>
using namespace std;
int a,b,ans,i,n;
int f[];
int step[];
void dfs(int floor,int k)
{
if (floor==b)
{
if (ans==-) ans=k;
else ans=min(ans,k);
return;
}
if (floor>n || floor<) return;
if (k>=step[floor]) return;
step[floor]=k;
dfs(floor+f[floor],k+);
dfs(floor-f[floor],k+);
return;
}
int main()
{
while(scanf("%d",&n),n)
{
scanf("%d%d",&a,&b);
for(i=;i<=n;i++)
{
scanf("%d",&f[i]);
step[i]=INT_MAX;
}
ans=-;
dfs(a,);
printf("%d\n",ans);
}
return ;
} /*bfs: 第二种方法
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std; int a,b,ans,i,n;
int f[205];
int vis[205];
struct node
{
int floor,step;
};
int check(int a)
{
if(a<1 || a>n || vis[a]) return 0;
return 1;
}
int main()
{
while(scanf("%d",&n),n)
{
scanf("%d%d",&a,&b);
for(i=1;i<=n;i++)
scanf("%d",&f[i]);
if (a==b) {printf("0\n"); continue;}
memset(vis,0,sizeof(vis));
queue<node>s;
node p;
p.floor=a;
p.step=0;
s.push(p);
vis[a]=1;
ans=-1;
while(!s.empty())
{
p=s.front();
s.pop();
int x=p.floor+f[p.floor];
if (check(x))
{
vis[x]=1;
node q;
q.floor=x;
q.step=p.step+1;
s.push(q);
if(x==b) {ans=q.step;break; }
}
x=p.floor-f[p.floor];
if (check(x))
{
vis[x]=1;
node q;
q.floor=x;
q.step=p.step+1;
s.push(q);
if(x==b) {ans=q.step;break; }
}
}
printf("%d\n",ans);
}
return 0;
}*/
/*
之前用递归和bfs做的,这次用的是dijkstra
*/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int inf=0x7fffffff;
int n,m,st,ed;
int vis[],dis[],mp[][];
int dijkstra()
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++) dis[i]=mp[st][i]==?inf:;
vis[st]=;
for(int i=;i<n;i++)
{
int mindis=inf,k;
for(int j=;j<=n;j++)
if (!vis[j] && mindis>dis[j]) mindis=dis[j],k=j;
vis[k]=;
if (k==ed) return dis[k];
for(int j=;j<=n;j++)
if (!vis[j] && mp[k][j])
dis[j]=min(dis[j],dis[k]+);
}
return -;
}
int main()
{
while(scanf("%d",&n) && n!=)
{
scanf("%d%d",&st,&ed);
memset(mp,,sizeof(mp));
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
if(i+x<=n) mp[i][i+x]=;
if(i-x>) mp[i][i-x]=;
}
if (st==ed) {printf("0\n");continue;}
printf("%d\n",dijkstra());
}
return ;
}

HDU1548:A strange lift的更多相关文章

  1. HDU1548——A strange lift(最短路径:dijkstra算法)

    A strange lift DescriptionThere is a strange lift.The lift can stop can at every floor as you want, ...

  2. 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 ...

  3. Hdu1548 A strange lift 2017-01-17 10:34 35人阅读 评论(0) 收藏

    A strange lift Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tota ...

  4. hdu1548 A strange lift(bfs 或Dijkstra最短路径)

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #d ...

  5. HDU 1548 A strange lift (最短路/Dijkstra)

    题目链接: 传送门 A strange lift Time Limit: 1000MS     Memory Limit: 32768 K Description There is a strange ...

  6. 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 ...

  7. A strange lift

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  8. bfs A strange lift

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at e ...

  9. hdu 1548 A strange lift

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...

随机推荐

  1. webapi中的自定义路由约束

    Custom Route Constraints You can create custom route constraints by implementing the IHttpRouteConst ...

  2. 用Bash脚本将Linux普通用户添加为系统管理员

    将Linux普通用户添加为系统管理员在Gnome或KDE这样强大与完善的桌面环境下是非常简单的事情,一般来说在用户设置的对话框里就直接有相应选项.不过,出于简洁与高效的风格,自己目前并未使用这些高端但 ...

  3. 可以有效防护XSS,sql注射,代码执行,文件包含等多种高危漏洞。

    http://bbs.aliyun.com/read/137391.html <?php /** * 云体检通用漏洞防护补丁v1.1 * 更新时间:2013-05-25 * 功能说明:防护XSS ...

  4. sql语句的group by与having子句

    准备数据: DROP TABLE IF EXISTS `t_player`; CREATE TABLE `t_player` ( `player_id` int(11) NOT NULL AUTO_I ...

  5. 用xftp传送避免乱码问题

    用xftp传送文件时,需要输入ip地址,可连通的端口号,采用sftp协议 输入数据库传送,属性binary,二进制 上传文件,图片中文名称正常显示等,需要该属性支持UTF-8

  6. mysql 1053错误,无法启动的解决方法

    mysql 1053错误,无法启动的解决方法 windows2003服务器中,服务器重启后Mysql却没有启动,手动启动服务时提示1053错误. 尝试了以下方法,终于解决. 1.在DOS命令行使用 第 ...

  7. MFC通过ODBC连接Mysql程序

    分享到 一键分享 QQ空间 新浪微博 百度云收藏 人人网 腾讯微博 百度相册 开心网 腾讯朋友 百度贴吧 豆瓣网 搜狐微博 百度新首页 QQ好友 和讯微博 更多... 百度分享 MFC通过ODBC连接 ...

  8. WKWebView与Js交互

    首先打开webstorm,将最下面h5拷贝到html中.然后导入工程 #define kMessageHandlerName @"mymobile" 1.创建配置类 - (WKWe ...

  9. Unity3d之将terrain转化成mesh

    Unity3d中,terrain还是比较耗的,DrawCall数也比较多,为了优化性能,可能需要将terrain转化成mesh. 现提供一工具,思路是根据terrain高度图生成mesh等,可参考:  ...

  10. 在选定的数据源上未找到名为“TitleSub”的字段或属

    在.NET开发过程中时常会遇到“在选定的数据源上未找到名为“TitleSub”的字段或属性”的错误”,导致这类错误的原因有很多,在我的项目中,详细情况是这样:1.有两个控件:DropDownList类 ...