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 ...
随机推荐
- 检测zookeeper和kafka是否正常
cd $(dirname $) source ~/.bash_profile count_zoo=`ps -ef | grep "config/zookeeper.properties&qu ...
- nginx 配置.json文件直接访问
不要弹出下载 配置nginx.conf http { include mime.types; default_type application/json;
- angular factory Services provider 自定义服务 工厂
转载于 作者:海底苍鹰地址:http://blog.51yip.com/jsjquery/1602.html 1.在app.js 中声明了模块的依赖 var phonecatApp = angular ...
- 无法完成安装:'unsupported configuration: hda-duplex not supported in this QEMU binary'
Linux 有问必答:如何修复"hda-duplex not supported in this QEMU binary" 编译自:http://ask.xmodulo.com/h ...
- solr5.5教程-solrconfig.xml,加载schema.xml
布署完成后,接下来要更深入的研究solr的原理和使用. 首先进入testcore这个文件夹下面,发现这个core的conf里并没有schema.xml.那么数据格式是在哪里定义的呢? 打开 solr_ ...
- 图解Javascript上下文与作用域
原文网址:http://blog.rainy.im/2015/07/04/scope-chain-and-prototype-chain-in-js/ 本文尝试阐述Javascript中的上下文与作用 ...
- wordpress代理设置
打开wp-config.php,在页首加上以下语句: define('WP_PROXY_HOST', '192.168.84.101'); define('WP_PROXY_PORT', '8080' ...
- 在HTML文件中加载js
js加载只分为两种: 1.全局js,放在<head>标签里面,整个页面很多都用到的,它是优先加载的. 2.局部js,放在</html>结束标签以内的任何位置,它是第二加载的.
- datagridview下拉框
下面介绍Winform中DataGridView的DataGridViewComboBoxColumn的使用方法: //首先获取数据源 //自己建立的静态数据源,你也可以从数据库读取 DataTabl ...
- ip的正则表达式 完美版
IP地址的长度为32位2进制,分为4段,每段8位,用十进制数字表示,每段数字范围为0~255,段与段之间用英文句点“.”隔开.例如:IP地址为10.0.0.100. 分析IP地址的每组数特点:百位,十 ...