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 ...
随机推荐
- tip浮动提示框
今天工作中碰到要弹出tip浮动提示框,如服务器控件的ToolTip属性. 通过GOOGLE搜到了一个很好用的tip浮动提示框:TipTip jQuery Plugin. 例子如下: <!DOCT ...
- php添加环境变量
在php的安装目录中添加,如/usr/php-5.6.16添加env.php文件,在文件中设置环境变量: 如:<?php$_SERVER['ENV'] = 'production'; 再到配置文 ...
- 关于MPEG2中的图像序列和图像组头GOP
图像序列 图像序列是由图像组构成的,是随机存取段落. sequence_header_code – The sequence_header_code is the bit string ‘000001 ...
- 在ax中怎么对enum类型循环取其中每一个值
static void test(Args _args) { DictEnum dictEnum; int i,nextPos; EnumId enumId; ; enumId = EnumNum(S ...
- oracle11g ORA-12505
对于这个问题我前一天还可以用的,第二天打开就报这个错误了. 我在网上找了很多方法,大多为修改/listener.ora这个文件里的SID,修改后也没弄好. 我的解决方法很简单, 打开Oracle Ne ...
- C++多态性——函数的覆盖和隐藏
1.函数的覆盖 覆盖的条件: 基类函数必须是虚函数(使用Virtual关键字进行声明): 发生覆盖的两个函数必须分别位于派生类和基类中: 函数名称与参数列表必须完全一样: 2.函数的隐藏 隐藏,是指派 ...
- 通过Migration在EF6中用多个DbContext
通过Migration在EF6中用多个DbContext EF EF6 C# Migration 通过Migration在EF6中用多个DbContext 前言 实现目标 设置多数据上下文 更新数据脚 ...
- SpringMVC与HTML页面
springMVC返回html页面 spring-mvc.xml配置: <bean id="viewResolver" class="org.springfram ...
- Oracle一些常用的查询命令总结(持续更新)
更新于:2015年1月28日 17:08:13 -------------------------表空间 --------------------------------------- ----- 查 ...
- PeopleSoft Object Types Definitions
PeopleSoft stores object definitions types such as Record, Field and SQL definitions as numbers in ...