hdu 1548 A strange lift
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=1548
A strange lift
Description
There is a strange lift.The lift can stop can at every floor as you want, and there is a number $K_i(0 \leq K_i \leq N)$ on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the $i-K_i$ th floor. Of course, the lift can't go up high than $N$,and can't go down lower than 1. For example, there is a buliding with 5 floors, and $k_1 = 3, k_2 = 3,k_3 = 1,k_4 = 2, k_5 = 5$. Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as you know ,the -2 th floor isn't exist.
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"?
Input
The input consists of several test cases.,Each test case contains two lines.
The first line contains three integers $N,\ A,\ B( 1 \leq N,A,B \leq 200)$ which describe above,The second line consist $N$ integers $k_1,k_2,....k_n$.
A single 0 indicate the end of the input.
Output
For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can't reach floor B,printf "-1".
Sample Input
5 5 5
3 3 1 2 5
0
Sample Output
3
简单的bfs。。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::map;
using std::pair;
using std::vector;
using std::queue;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int Max_N = ;
const int dx[] = { -, };
typedef unsigned long long ull;
bool vis[Max_N];
int N, A, B, arr[Max_N];
struct Node {
int x, s;
Node(int i = , int j = ) :x(i), s(j) {}
};
void bfs() {
cls(vis, );
queue<Node> que;
que.push(Node(A, ));
vis[A] = true;
while (!que.empty()) {
Node tp = que.front(); que.pop();
if (tp.x == B) { printf("%d\n", tp.s); return; }
rep(i, ) {
int nx = dx[i] * arr[tp.x] + tp.x;
if (nx < || nx > N || vis[nx]) continue;
que.push(Node(nx, tp.s + ));
vis[nx] = true;
}
}
puts("-1");
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
while (~scanf("%d", &N) && N) {
scanf("%d %d", &A, &B);
rep(i, N) scanf("%d", &arr[i + ]);
bfs();
}
return ;
}
hdu 1548 A strange lift的更多相关文章
- 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 (最短路/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 搜索
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) T ...
- HDU 1548 A strange lift(BFS)
Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...
- 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(最短路&&bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
随机推荐
- python基础set
1.set set是一个无序的不重复的集合 li=[11,22,33,11] s=set(li) print(s) {11,22,33} set提供的方法 1.add(self, *args, ** ...
- 查看Eclipse版本号的方法
查看Eclipse版本号的方法如下所示: 1:假设Eclipse已打开Eclipse的菜单栏: Help-->About Eclipse弹出框中会显示一排logo,点击eclipse的那个log ...
- Are Landing Pages Killing Your Conversion Rate?
http://searchenginewatch.com/sew/how-to/2411253/are-landing-pages-killing-your-conversion-rate
- java中byte转换int时为何与0xff进行与运算
在剖析该问题前请看如下代码 public static String bytes2HexString(byte[] b) { String ret = ""; for (int ...
- ios开发错误笔记
今天的奇葩错误,最后解决方式是删除了手机上面的快捷方式,然后再clean,然后再重启了xcode.无语了,xcode也经常出些奇葩问题,真无语啊. ios技术交流群:378501081..期待你加入. ...
- centos7 docker1.12 私有仓库
docker1.12私有仓库 参考:http://www.cnblogs.com/xcloudbiz/articles/5497037.html 参考:http://www.07net01.com/2 ...
- 【MySQL】MySQL回滚工具
1.mysqlbinlog把事务从binlog中导出 2.从导出的binlog中找到要回滚的事务,去掉第一个DML语句前和最后一个DML语句后与DML无关的binlog信息 3.在目录中新建一个tab ...
- SQL Server 开发指南
SQL Server 数据库设计 一.数据库设计的必要性 二.什么是数据库设计 三.数据库设计的重要 四.数据模型 实体-关系(E-R)数据模型 ...
- HTTP MIME类型即HttpResponse.ContentType属性值列表
MIME-Typ Dateiendung(en) Bedeutung application/acad *.dwg AutoCAD-Dateien (nach NCSA) application/ap ...
- maven auto-config 多环境自动打包
摘自:http://doc.okbase.net/bjhecwq/archive/118121.html maven有许多优秀的插件,最近在研究打包中替换多重环境的配置,同事介绍使用阿里的auto-c ...