TYVJ P3522 &&洛谷 P1135 奇怪的电梯 Label:bfs
题目描述
呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯。大楼的每一层楼都可以停电梯,而且第i层楼(1<=i<=N)上有一个数字Ki(0<=Ki<=N)。电梯只有四个按钮:开,关,上,下。上下的层数等于当前楼层上的那个数字。当然,如果不能满足要求,相应的按钮就会失灵。例如:3 3 1 2 5代表了Ki(K1=3,K2=3,……),从一楼开始。在一楼,按“上”可以到4楼,按“下”是不起作用的,因为没有-2楼。那么,从A楼到B楼至少要按几次按钮呢?
输入输出格式
输入格式:
输入文件共有二行,第一行为三个用空格隔开的正整数,表示N,A,B(1≤N≤200, 1≤A,B≤N),第二行为N个用空格隔开的正整数,表示Ki。
输出格式:
输出文件仅一行,即最少按键次数,若无法到达,则输出-1。
输入输出样例
LIFT.IN
5 1 5
3 3 1 2 5
LIFT.OUT
3
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
struct cc{
int num,step;
}; cc make_cc(int num,int step){
cc a;
a.num=num;a.step=step;
return a;
} int a[],vis[],N,A,B,cnt;
queue<cc> que;
int main(){
// freopen("01.in","r",stdin);
// freopen("01.out","w",stdout); scanf("%d%d%d",&N,&A,&B);
for(int i=;i<=N;i++){
scanf("%d",&a[i]);
} que.push(make_cc(A,));
vis[A]=;
while(!que.empty()){
cc tmp=que.front();que.pop(); if(tmp.num==B){
printf("%d\n",tmp.step);
return ;
} int low=tmp.num-a[tmp.num],high=tmp.num+a[tmp.num]; if(low>= && !vis[low]){
que.push(make_cc(low,tmp.step+));vis[low]=;
}
if(high<=N && !vis[high]){
que.push(make_cc(high,tmp.step+));vis[high]=;
}
} puts("-1"); // fclose(stdin);
// fclose(stdout);
return ;
}正解
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
int a[],vis[],N,A,B,cnt;
queue<int> que;
int main(){
// freopen("01.in","r",stdin);
// freopen("01.out","w",stdout); scanf("%d%d%d",&N,&A,&B);
for(int i=;i<=N;i++){
scanf("%d",&a[i]);
} que.push(A);
vis[A]=;
while(!que.empty()){
int tmp=que.front();que.pop();
int low=tmp-a[tmp],high=tmp+a[tmp]; if(tmp==B){
printf("%d\n",cnt);
return ;
} if(low>&&!vis[low]){
que.push(low);vis[low]=;
}
if(high<=N&&!vis[high]){
que.push(high);vis[high]=;
}
cnt++;
} puts("-1"); // fclose(stdin);
// fclose(stdout);
return ;
}错误代码
错误代码是没有考虑到cnt可能不符合当前的步数
因为没理解好bfs导致错误,cnt一直增加,而步数是随bfs深度增加的
TYVJ P3522 &&洛谷 P1135 奇怪的电梯 Label:bfs的更多相关文章
- 洛谷P1135 奇怪的电梯【bfs】
题目:https://www.luogu.org/problemnew/show/P1135 题意: 一共有n层楼,在第i层可以往上或往下$k_i$层. 问从$a$层到$b$层至少需要多少乘多少次电梯 ...
- 【DFS与BFS】洛谷 P1135 奇怪的电梯
题目:奇怪的电梯 - 洛谷 (luogu.com.cn) 因为此题数据范围较小,有dfs及bfs等多种做法. DFS 比较正常的dfs,注意vis数组一定要回溯,不然会漏情况 例如这个数据 11 1 ...
- 洛谷 P1135 奇怪的电梯 【基础BFS】
题目链接:https://www.luogu.org/problemnew/show/P1135 题目描述 呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯.大楼的每一层楼都可以停电梯,而且第 i 层 ...
- 洛谷 P1135 奇怪的电梯
题目描述 呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯.大楼的每一层楼都可以停电梯,而且第i层楼(1<=i<=N)上有一个数字Ki(0<=Ki<=N).电梯只有四个按钮:开 ...
- 洛谷P1135 奇怪的电梯
题目描述 呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯.大楼的每一层楼都可以停电梯,而且第i层楼 (1<=i<=N)上有一个数字Ki(0<=Ki<=N).电梯只有四个按钮: ...
- 洛谷 P1135 奇怪的电梯 (dfs)
题目描述 呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯.大楼的每一层楼都可以停电梯,而且第i层楼(1<=i<=N)上有一个数字Ki(0<=Ki<=N).电梯只有四个按钮:开 ...
- 洛谷P1135 奇怪的电梯 BFS例题
好,这是一道黄题.几个月前(2017.10.29)的我拿了可怜的20分. 这是当年的蒟蒻代码 #include <cstdio> #include <iostream> #in ...
- 洛谷 P1135 奇怪的电梯 (DFS)
题意:有一\(n\)层高的大楼,每层楼都只能在合法情况下上\(x\)层或者下\(x\)层,问你是否能从\(a\)层楼坐电梯到\(b\)层楼,输出最小步数. 题解:直接dfs搜,\(flo\)表示层数, ...
- 集训作业 洛谷P1135 奇怪的电梯
这个题我见过!!! 我之前在石油大学的网站上做练习赛,提高了很多,这个题是我第一次在比赛里见到深搜. 当时蒙蔽的一批,现在发现好简单…… 这个题和普通的深搜没什么区别,甚至可以说简单了,因为这个是1维 ...
随机推荐
- Jquery.Datatables 导出excel
按钮(Buttons) BUttons v1.1.2 下载地址:http://pan.baidu.com/s/1c0Jhckg JSZip v2.5.0-21-g4fd4fc1 下载地址:http:/ ...
- sdut1598 周游列国【简单模拟题】
周游列国 Time Limit: 1000ms Memory limit: 32768K 有疑问?点这里^_^ 题目描述 题目链接:http://acm.sdut.edu.cn/sdutoj/p ...
- <转>SQL语句执行顺序说明
原文地址:http://www.cnblogs.com/summer_adai/archive/2011/10/28/2227605.html SQL 不同于与其他编程语言的最明显特征是处理代码的顺序 ...
- 获取当前IP地址,跳转到对应城市网站。
index.php文件 1,通过获取当前IP地址, 2,调用新浪AIP,获取到当前城市. 3,将中文转换为拼音后跳转. <?php include './pinyin.php'; //获取当前i ...
- ZLL网关程序分析
主机接口 zllSocCmd.h(ZLL的Socket主机接口) 此模块包含ZLL的Socket主机接口API.其包含的函数方法在zllSocCmd.c中实现 ZLL Soc Types 定义了描述设 ...
- 一个json字符串
{ "area": [{ "flag": "Y", "ishot": "N", "lag& ...
- Sp EF输出 临时表
-- ============================================= -- Author: <Author,,Name> -- Create date: < ...
- loj1011 状态压缩
题目链接:http://lightoj.com/volume_showproblem.php?problem=1011 思路:最近的开始做dp了...很明显的一道状态压缩题,dp[n][state]表 ...
- yaml官方介绍
官方网站 http://yaml.org/ 数据结构类型说明 http://yaml.org/type/ YAML Specification http://yaml.org/spec/
- 记录linux /bin被误删除的解决过程
1.事因: 执行shell测试时,shell中rm -rf $path/* 变量$path为空,结果执行的命令是rm -rf / 事发时及时ctrl+c中断,导致只有/bin /boot目录删除 2. ...