A strange lift_BFS
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"?
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.
3 3 1 2 5
0
【题意】给出n层,要从s到t,给出每层只能下或者上a[i],问至少经过多少次才能从s到t
【思路】经典BFS
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int N=;
int a[N];
int vis[N];
int n,s,t;
struct node
{
int x;
int step;
};
bool go(int x)
{
if(x<=||x>n) return false;
return true;
}
int bfs()
{
node now,next;
queue<node>qu;
now.x=s;
now.step=;
qu.push(now);
while(!qu.empty())
{
now=qu.front();
qu.pop();
if(now.x==t)
return now.step;
for(int i=-;i<=;i+=)
{
next=now;
next.x+=a[next.x]*i;
if(go(next.x)&&vis[next.x]==)
{
vis[next.x]=;
next.step++;
qu.push(next); }
}
}
return -;
}
int main()
{
while(~scanf("%d",&n),n)
{
scanf("%d%d",&s,&t);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
memset(vis,,sizeof(vis));
int ans=bfs();
printf("%d\n",ans);
}
return ;
}
A strange lift_BFS的更多相关文章
- timus 1175. Strange Sequence 解题报告
1.题目描述: 1175. Strange Sequence Time limit: 1.0 secondMemory limit: 2 MB You have been asked to disco ...
- CF719C. Efim and Strange Grade[DP]
C. Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input sta ...
- HDU 1548 A strange lift (最短路/Dijkstra)
题目链接: 传送门 A strange lift Time Limit: 1000MS Memory Limit: 32768 K Description There is a strange ...
- ACM : HDU 2899 Strange fuction 解题报告 -二分、三分
Strange fuction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- 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 ...
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- strange error encountered today in ROS
I reinstalled my ubuntu system and also ROS. I tested slam_karto package when some strange error cam ...
随机推荐
- selenium高亮显示操作步骤方法
package com.allin.pc;import java.util.List;import org.openqa.selenium.WebElement;import org.openqa.s ...
- 内核编译选配(VMware篇)
出现这个错误的原因是相应的驱动程序没有编译进内核,所以在内核启动时,不认识分区. 一.磁盘驱动没编译进内核 VMware5.5.3 的磁盘有两种,一种是IDE的,一种是SCSI的:VMware 你在新 ...
- Bootstrap_导航
一.标签形tab导航 标签形导航,也称为选项卡导航. 标签形导航是通过“.nav-tabs”样式来实现.在制作标签形导航时需要在原导航“.nav”上追加此类名. <ul class=" ...
- web服务器工作原理
Web服务器工作原理概述 转载自http://www.importnew.com/15020.html 很多时候我们都想知道,web容器或web服务器(比如Tomcat或者jboss)是怎样工作的?它 ...
- 浅谈用ModelSim+Synplify+Quartus来实现Altera FPGA的仿真
浅谈用ModelSim+Synplify+Quartus来实现Altera FPGA的仿真 工作内容: Mentor公司的ModelSim是业界最优秀的HDL语言仿真软件,它能提供友好的仿真环境,是业 ...
- Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED
Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED 问题描述 使用pip按照virtualenv报错,如下: pip install virtua ...
- SQL Server 2012 管理新特性:AlwaysOn【转】
http://jimshu.blog.51cto.com/3171847/871169 见超链接
- BZOJ 2600: [Ioi2011]ricehub
2600: [Ioi2011]ricehub Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 628 Solved: 325[Submit][Stat ...
- 封装对MongoDB数据库的增删改查访问方法(基于MongoDB官方发布的C#驱动)
本文利用MongoDB官方发布的C#驱动,封装了对MongoDB数据库的增删改查访问方法.先用官方提供的mongo-csharp-driver ,当前版本为1.7.0.4714 编写数据库访问帮助类 ...
- sqlAlchemy 按DateTime字段的年或月进行group_by查询
一.根据”create_date“查询每天的数据 1.查询2016年5月每天的数据 session.query(extract('day', User.create_date).label('day' ...