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 ...
随机推荐
- 智能社区--HI3516C可视门禁研发出来咯
铝壳.非常大气的外壳. 200W像素,HI3516C,携带server.创新的产品.欢迎交流:QQ237753582 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5u ...
- SP2-0734: 未知的命令开头 "imp scott/..." - 忽略了剩余的行。
Oracle数据导入报错:SP2-0734: 未知的命令开头 "imp scott/..." - 忽略了剩余的行. 原因:进入sqlplus里是不能运行imp的(sqlplus不认 ...
- Android系统优化
这些事实上就是优化rom 的一些实用小技巧. 认为非常多还是实用的. Build.prop (编辑 /system/build.prop 文件(须要root, 能够用文件管理器或者其它root exp ...
- servlet container:tomcat jetty and undertow
1 spring boot内嵌容器支持tomcat.jetty和undertow 但是undertow性能最好,详见: https://examples.javacodegeeks.com/enter ...
- T3 最短路 line
T3 最短路 line [问题描述] 给定一个 n 个点,m 条边的有向图,每个点有一个权值 a[i],表示这个点要到达多少次,1 为起始点,从 1 到 i 的距离为 d[i],请你输出∑a[i]*d ...
- [LeetCode] Construct Binary Tree from Inorder and Pretorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【高德地图API】地理编码与逆地理编码
一.地理编码 该功能实现地理编码服务,即地址匹配,从已知的地址描述到对应的经纬度坐标的转换,即根据地址信息,查询该地址所对应的点坐标等,地址(address) 为必选项,城市(city)为可选项. & ...
- foxmail地址簿导入thunderbird的乱码问题 (转载)
转自:http://blog.csdn.net/gexueyuan/article/details/9032595 由于foxmail的地址簿格式和thunderbird的格式不一样,另外也存在编码问 ...
- bzoj 1574: [Usaco2009 Jan]地震损坏Damage【dfs】
和March的那道不一样,只是非常单纯的带着贪心的dfs 首先一个点被隔断,与它相邻的所有点也会被隔断,打上删除标记,从1dfs即可 #include<iostream> #include ...
- SpringCloud(Finchley版本)中Zull过滤器ResponseBoby返回中文乱码解决方案
Spring Cloud带有"Cloud"的字样,但它并不是云计算解决方案,而是在Spring Boot基础上构建的,用于快速构建分布式系统的通用模式的工具集.使用Spring C ...