HDU1548:A strange lift(Dijkstra或BFS)
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1548
题意:电梯每层有一个数,例如第n层有个数k,
那么这一层只能上k层或下k层,但是不能低于一层或高于n层,
给定起点与终点,要求出最少要按几次键
我的思路:这题可以用bfs或者用最短路(dijsktra)
bfs
AC代码
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int n,a,b,dis[210],vis[210];
struct node
{
int now;
int step;
};
int main(void)
{
int bfs();
int i;
while(scanf("%d",&n)==1&&n)
{
memset(vis,0,sizeof(vis));
scanf("%d%d",&a,&b);
for(i=1;i<=n;i++)
scanf("%d",dis+i);
printf("%d\n",bfs());
}
return 0;
}
int bfs()
{
int i;
queue<node> q;
vis[a]=1;
node start,next;
start.now=a;
start.step=0;
q.push(start);
while(!q.empty())
{
node s=q.front();
q.pop();
if(s.now==b)
return s.step;
for(i=0;i<2;i++)
{
if(0==i)
next.now=s.now+dis[s.now];
else
next.now=s.now-dis[s.now];
if(!vis[next.now]&&next.now>=1&&next.now<=n)
{
if(next.now==b)
return s.step+1;
vis[next.now]=1;
next.step=s.step+1;
q.push(next);
}
}
}
return -1;
}
最短路算法
#include<stdio.h>
#include<string.h>
int n,a,b,dis[210],vis[210],map[210][210];
const int Max = 0x3f3f3f3f;
int main(void)
{
int i,j,k,l;
while(scanf("%d",&n)==1&&n)
{
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i!=j)
map[i][j]=Max;
else
map[i][j]=0;
}
}
scanf("%d%d",&a,&b);
for(i=1;i<=n;i++)
{
scanf("%d",dis+i);
if(i-dis[i]>=1&&i+dis[i]<=n)
map[i][i-dis[i]]=map[i][i+dis[i]]=1;
else if(i-dis[i]>=1)
map[i][i-dis[i]]=1;
else
map[i][i+dis[i]]=1;
}
for(i=1;i<=n;i++)
dis[i]=map[a][i];
for(i=1;i<=n;i++)
{
l=Max;
for(j=1;j<=n;j++)
{
if(!vis[j]&&l>dis[j])
{
k=j;
l=dis[j];
}
}
if(l==Max) break;
vis[k]=1;
for(j=1;j<=n;j++)
{
if(!vis[j]&&dis[j]>dis[k]+map[k][j])
dis[j]=dis[k]+map[k][j];
}
}
if(dis[b]==Max)
printf("-1\n");
else
printf("%d\n",dis[b]);
}
return 0;
}
HDU1548:A strange lift(Dijkstra或BFS)的更多相关文章
- HDU 1548 A strange lift(Dijkstra,简单BFS)
题目大意: 电梯有两个选项向上或向下,每层楼有一个参数ki,代表电梯可以再该楼层的基础上向上或向下移动ki层,限制条件是向上不能超过楼层总数n,向下不能少于一.输入总层数n和当前所在层数以及目标层数, ...
- 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
A strange lift Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tota ...
- 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 ...
- 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 ...
- HDU 1548 A strange lift(最短路&&bfs)
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 http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...
- hdu1548 A strange lift(bfs 或Dijkstra最短路径)
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #d ...
随机推荐
- Tree of Life (easy)
Tree of Life (easy) Heidi has finally found the mythical Tree of Life – a legendary combinatorial st ...
- unbtun python tab补全
在使用python的时候有时候总是忘记很多代码,这个是作为程序袁最头疼的事情,本人也是刚刚接触python,这几天也是用到这块,所以记录下来,已被需要时能够找到. 我的系统是: w@w:~$ una ...
- 首页布局时div的宽度设置要注意
- 转 Android HttpClient post MultipartEntity - Android 上传文件
转自 http://blog.csdn.net/hellohaifei/article/details/9707089 在Android 中使用HttpClient,MultipartEntity ...
- 我想操作的是利用SqlDataAdapter的几个Command属性(InsertCommand,UpdateCommand,DeleteCommand)来更新数据库
我想操作的是利用SqlDataAdapter的几个Command属性(InsertCommand,UpdateCommand,DeleteCommand)来更新数据库代码:SqlConnection ...
- ural2062 Ambitious Experiment
Ambitious Experiment Time limit: 3.0 secondMemory limit: 128 MB During several decades, scientists f ...
- Android JNI入门第六篇——C调用Java
本篇将介绍在JNI编程中C调用Java实现. 源码下载地址:http://download.csdn.net/detail/xyz_lmn/4868265 关键代码: java: public cla ...
- Ext中包含了几个以get开头的方法
Ext中包含了几个以get开头的方法,这些方法可以用来得到文档中DOM.得到当前文档中的组件.得到Ext元素等,在使用中要注意区别使用.1.get方法get方法用来得到一个Ext元素,也就是类型为Ex ...
- xtrabackup数据库备份
xtrabackup备份 一.Xtrabackup概述 1.1.简介 Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Ho ...
- jQuery中的attr()和prop()使用
总结:除了checked.seleted这样的属性推荐用prop()外,其他的随意都可以. 原因如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML ...