HDU_1548_A strange lift
题意:一部电梯(共top层),每一楼有一个数字k,在该层只能上k层或者下k层(up和down按钮),问从当前层到目标层按按钮的最小次数。
分析:广度优先搜索。
总结:初写BFS,仿照别人的代码,这方面要继续加强。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int k[],vis[];
int top,now,aim;
struct node
{
int floor,step;;
}; bool judge(int a)
{
if(a>=&&a<=top)
return ;
return ;
}
int BFS(node sta)
{
queue<node>q;
node now,next;
vis[sta.floor]=;
q.push(sta);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.floor==aim)
return now.step;
for(int i=-; i<=; i+=)
{
if(!vis[now.floor+i*k[now.floor]]&&judge(now.floor+i*k[now.floor]))
{
next.floor=now.floor+i*k[now.floor];
next.step=now.step+;
vis[now.floor+i*k[now.floor]]=;
q.push(next);
}
}
}
return -;
} int main()
{ while(scanf("%d",&top)!=EOF&&top)
{
scanf("%d%d",&now,&aim);
for(int i=; i<=top; i++)
scanf("%d",&k[i]);
node a;
a.floor=now;
a.step=;
memset(vis,,sizeof(vis));
int ans=BFS(a);
printf("%d\n",ans);
}
return ;
}
HDU_1548_A strange lift的更多相关文章
- 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 ...
- 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 ...
- 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 搜索
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- hdu oj 1285 确定比赛名次
hdu oj 1285 确定比赛名次 题目: 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- python批量删除文件
敲代码測试时总会碰到要删除日志目录下的日志或者删除一些历史文件.每次都会生成,再測试的时候为了查找错误原因方便总是要在測试前删除这些文件.手动删除比較麻烦.所以写一个批量删除脚本 import os ...
- P1198 [JSOI2008]最大数(线段树)
P1198 [JSOI2008]最大数(线段树) 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值 ...
- JFreeChart生成柱形图(2) (转自 JSP开发技术大全)
JFreeChart生成柱形图(2) (转自 JSP开发技术大全) 14.2 利用JFreeChart生成柱形图14.2.1 利用DefaultCategoryDataset数据集绘制柱形图 通过JF ...
- 国产手机没有google services 和google play崩溃,判断google services是否存在
public static boolean isGooglePlayServiceAvailable (Context context) { int status = GooglePlayServic ...
- UVa10288 Coupons 分数模版
UVa10288 题目非常简单, 答案就是 n/n+n/(n-1)+...+n/1; 要求分数输出.套用分数模板.. #include <cstdio> #include <cstr ...
- bzoj2763 [JLOI2011]飞行路线——分层图
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2763 构建分层图. 代码如下: 写法1(空间略大)(时间很慢): #include<i ...
- JAVA线程同步 (二)notify()与notifyAll()-***
编写多线程程序需要进行线程协作,前面介绍的利用互斥来防止线程竞速是来解决线程协作的衍生危害的.编写线程协作程序的关键是解决线程之间的协调问题,在这些任务中,某些可以并行执行,但是某些步骤需要所有的任务 ...
- mybaties中,模糊查询的几种写法
模糊查询: 工作中用到,写三种用法吧,第四种为大小写匹配查询 1. sql中字符串拼接 SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('% ...
- openStack Aio 环境的neutron agent-list和cluster 环境 CLI结果对比