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 ...
随机推荐
- SharePoint 入门书籍推荐 转载来源http://www.cnblogs.com/jianyus/p/3513238.html
最近,总有人说刚入门SharePoint,没有好的资料或者电子书,资料推荐大家多看看博客园和CSDN的博客.对于看博客,我一般是两个思路,要么找一个人的从头到尾看一遍,觉得有意义的,就把地址加收藏:或 ...
- 使用Idea作为go的IDE
Idea是一款非常不错的IDE,只可惜是收费的.Idea可以使用很多的插件,当然里面就包含我们需要的go语言的插件. 1.我们打开file -> Settings -> Plugins - ...
- app/desktop/view/index.html 显示授权标识
app/desktop/view/index.html 显示授权标识
- css格式布局
一.position:fixed 锁定位置(相对于浏览器的位置),例如有些网站的右下角的弹出窗口. 示例 : 二.position:absolute 1.外层没有position:absolute(或 ...
- Jquery实现表格的分页
功能描述 该分页功能不是一次将所有记录都加载出来,然后在点击按钮翻页的时候,通过设置每一条记录的display属性展示或隐藏实现分页的效果.由于后台的获取数据的接口已实现分页的功能,所以在点击翻页按钮 ...
- C语言语法tips(不断更新)
语法虽然琐碎,犯了低级错误代价可就大了,列出一些容易混淆概念,避免以后出错 sizeof和strlen的区别在于:1.sizeof是运算符,语言支持的,strlen可以求malloc出来的字符串的长度 ...
- jquery各版本区别
jquery版本区别: 1.3一般功能够 1.4.2一般功能够而且稳定 1.7+比较新特性 2不支持老IE 兼容的话最好选 1.x.稳定性就用1.7或者1.4,其中1.4的体积相对 ...
- numpy中的matrix矩阵处理
numpy模块中的矩阵对象为numpy.matrix,包括矩阵数据的处理,矩阵的计算,以及基本的统计功能,转置,可逆性等等,包括对复数的处理,均在matrix对象中. class numpy.matr ...
- HDU 5804 Price List
只需和总和比较即可. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...
- simHash 简介以及java实现
http://gemantic.iteye.com/blog/1701101 simHash 简介以及java实现 博客分类: 算法 simHash java 去重 传统的hash 算法只负责将原始 ...