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 ...
随机推荐
- CSS3的chapter6
CSS布局 div标签: 在css布局方式中,div 是这种布局方式的核心对象,我们的页面排版不再依赖于表格, 仅从div的使用上说,做一个简单的布局只需要两样东西:div 与 cs ...
- Windows Internals学习笔记(七)Image Loader
参考资料: 1. <Windows Internals> 2. Fibers 知识点: ● 当一个进程在系统上启动时,内核将创建一个进程对象来代表它,并执行各种内核相关的初始化任务.然而, ...
- Response.End()在Webform和ASP.NET MVC下的表现差异
前几天在博问中看到一个问题--Response.End()后,是否停止执行?MVC与WebForm不一致.看到LZ的描述后,虽然奇怪于为何用Response.End()而不用return方式去控制流程 ...
- Html5 Canvas核心技术(图形,动画,游戏开发)--基础知识
基础知识 canvas 元素可以说是HTML5元素中最强大的一个,他真正的能力是通过canvas的context对象表现出来的.该环境对象可以从canvas元素身上获得. <body> & ...
- nodejs 访问mysql
安装 $ npm install mysql 简介 这个一个mysql的nodejs版本的驱动,是用JavaScript来编写的.不需要编译 这儿有个例子来示范如何使用: var mysql = re ...
- Python简单时间日期处理
import datetime #日期初始化: d1 = datetime.datetime(2005, 2, 16) d2 = datetime.datetime(2004, 12, 31) #日期 ...
- matlab各格式数据读取与保存函数
数据处理及matlab的初学者,可能最一开始接触的就是数据的读取与保存: %matlab数据保存与读入 function datepro clear all; %产生随机数据 mat = rand(, ...
- 下载zip格式文件(压缩Excel文件为zip格式)
Mongodb配置文件参考这一篇:http://www.cnblogs.com/byteworld/p/5913061.html package util; import java.io.Buffer ...
- 有关于psExec的使用
psExec是微软pstools工具包中最常用的一个工具,也是在内网渗透中的免杀渗透利器. psExec能够在命令行下在对方没有开启telnet服务的时候返回一个半交互的命令行,像telnet客户端一 ...
- 字符串与Objec之间互相转换
字符串与Objec之间互相转换可通过json实现. JSON.parse(str);// 字符串转Json Object JSON.stringify(obj);// Obj转字符串