HDU 1548 A strange lift 奇怪的电梯(BFS,水)
题意:
有一座电梯,其中楼层从1~n,每层都有一个数字k,当处于某一层时,只能往上走k层,或者下走k层。楼主在a层,问是否能到达第b层?
思路:
在起点时只能往上走和往下走两个选择,之后的每层都是这样,那么就类似于二叉树。每个节点就是对应的层,因为有可能碰到循环的层,比如1跳到3,3跳回1,这样使得无限循环,所以加个vis数组标记是否遍历过即可。
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <deque>
using namespace std;
const int N=;
int n, a, b;
int q[N];
deque<int> que;
bool vis[N]; int cal()
{
if(a==b) return ;
que.clear(); int cnt=;
que.push_back(a);
while(!que.empty())
{
int siz=que.size();
for(int i=; i<siz; i++)
{
int cur=que.front();
que.pop_front();
if(cur+q[cur]==b||cur-q[cur]==b)
return cnt+; if(cur+q[cur]<=n && !vis[cur+q[cur]] ) //没有遍历过的,小于上限的才考虑
{
que.push_back(cur+q[cur]);
vis[cur+q[cur]]=;
}
if(cur-q[cur]> && !vis[cur-q[cur]] ) //没有遍历过的,大于0的才考虑
{
que.push_back(cur-q[cur]);
vis[cur-q[cur]]=;
}
}
cnt++;
}
return -;
} int main()
{
//freopen("input.txt", "r", stdin); while(cin>>n,n)
{
memset(q,,sizeof(q));
memset(vis,,sizeof(vis)); cin>>a>>b;
for(int i=; i<=n; i++)
cin>>q[i]; printf("%d\n",cal());
} return ;
}
AC代码
HDU 1548 A strange lift 奇怪的电梯(BFS,水)的更多相关文章
- 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 ...
- HDU 1548 A strange lift (广搜)
题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...
- hdu 1548 A strange lift(迪杰斯特拉,邻接表)
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 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 ...
- hdu 1548 A strange lift (bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 1548 A strange lift(最短路&&bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
随机推荐
- super用法
Person类: public class Person { String _name; int _age; public Person(String name,int age) { _name= n ...
- Android的事件处理机制(一)------基于回调机制的事件处理
Android平台的事件处理机制有两种,一种是基于回调机制的,一种是基于监听接口的,现介绍第一种:基于回调机制的事件处理.Android平台中,每个View都有自己的处理事件的回调方法,开发人员可以通 ...
- Javascript 正则表达式笔记2
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- javascript console
javascript console console.log(object[, object, ...])在控制台输出一条消息.如果有多个参数,输出时会用空格隔开这些参数. 第一个参数可以是一个包含格 ...
- const以及入栈出栈
#include "stdafx.h"#include <iostream>using namespace std; class StringStack{ enum{s ...
- SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-001-Spring对原始JDBC的封装
1.spring扩展的jdbc异常 2.Template的运行机制 Spring separates the fixed and variable parts of the data-access p ...
- IntelliJ IDEA:Getting Started with Spring MVC, Hibernate and JSON实践
原文:IntelliJ IDEA:Getting Started with Spring MVC, Hibernate and JSON实践 最近把编辑器换成IntelliJ IDEA,主要是Ecli ...
- 原生javascript-常用的函数
[一]添加监听事件 addHandler:function(node,type,fn){if(node.addEventListener){ node.addEventListener(type,fn ...
- 总结JavaScript(Iframe、window.open、window.showModalDialog)父窗口与子窗口之间的操作
一.Iframe 篇 //&&&&&&&&&&&&&&&&&&a ...
- Maven+Spring+MVC结构中,jetty/tomcat是如何启动项目的[转]
针对maven配置的Spring+MVC项目,我们用Maven自带的jetty和tomcat插件进行调试,这很方便.但是调试时,这些插件所启动的web服务器,是如何来将我们的工程作为一个web项目启动 ...