HDU1548:A strange lift
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
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 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
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的更多相关文章
- HDU1548——A strange lift(最短路径:dijkstra算法)
A strange lift DescriptionThere is a strange lift.The lift can stop can at every floor as you want, ...
- 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 ...
- 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 ...
- hdu1548 A strange lift(bfs 或Dijkstra最短路径)
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #d ...
- 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 / 最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...
- A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 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 ...
- hdu 1548 A strange lift
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...
随机推荐
- JS预览图像将本地图片显示到浏览器上
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- Openjudge-计算概论(A)-大象喝水
描述 一只大象口渴了,要喝20升水才能解渴,但现在只有一个深h厘米,底面半径为r厘米的小圆桶(h和r都是整数).问大象至少要喝多少桶水才会解渴. 输入输入有一行:包行两个整数,以一个空格分开,分别表示 ...
- 查看mms UA/profile
查看彩信的UA/profile信息,当然可以通过打印log来看. 但还有一个比较简便的方法: 直接抓取一个MMS包,用网络包分析工具打开,找到承载该MMS信息的HTTP协议包即可查看:
- shell 提取字符串
记录一下: 我们可以用 ${ } 分别替换获得不同的值: file=/dir1/dir2/dir3/my.file.txt ${file#*/}:拿掉第一条 / 及其左边的字符串:dir1/d ...
- kvstore存储介质redis代码
<?php /** * ShopEx licence * * @copyright Copyright (c) 2005-2010 ShopEx Technologies Inc. (http: ...
- Git单独checkout子目录
http://schacon.github.io/git/git-read-tree.html#_sparse_checkout Existing Repository If you already ...
- mysql分页的问题
用过mysql的人肯定知道,mysql提供了原生的分页功能-----LIMIT关键字.LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数.LIMIT 接受一个或两个数字参数.参数必须是 ...
- 在命令提示符下,怎么查看windows开启了哪些服务?
net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" /user: ...
- jQuery的dataTables插件实现中文排序
最近在写Java web. 写JSP的时候发现一个很好玩的插件dataTables.分页.过滤.排序等等手到擒来. 哎哎哎,有点点可惜的是排序这个功能不支持中文.于是网上查查找找,现在把方法整理一下, ...
- OpenCV:二值图像连通区域分析与标记算法实现
http://blog.csdn.net/cooelf/article/details/26581539?utm_source=tuicool&utm_medium=referral Open ...