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 ;
}
上一篇:class A where T:class 这个泛型类中的Where T:class什么意思


下一篇:Bootstrap关于表单控件(按扭)