HDU_1548
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= 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-Ki 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 k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 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 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
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 1 5
3 3 1 2 5
0
Sample Output
3 1.dijkstra解法
#include <iostream>
#include <string.h>
using namespace std;
#define MAXN 250
const int INF = ;
int g[MAXN][MAXN];
int dis[MAXN];
int vis[MAXN];
int n;
// 5 1 5
// 3 3 1 2 5
//
void init(){
memset(vis,,sizeof(vis));
for(int i = ; i <= n;i++){
for(int j = ; j <= n; j++){
if(i == j){
g[i][j] = ;
}
else g[i][j] = INF;
}
}
}
void dij(int v0){
int pos = v0;
for(int i = ; i <= n; i++){
dis[i] = g[v0][i];
}
vis[pos] = ;
for(int i = ; i < n; i++){
int mins = INF;
for(int j = ; j <= n; j++){
if(!vis[j] && dis[j] < mins){
pos = j;
mins = dis[j];
}
}
vis[pos] = ;
for(int j = ; j <= n; j++){
if(!vis[j] && dis[j] > dis[pos] + g[pos][j])
dis[j] = dis[pos] + g[pos][j];
}
}
}
int main(){
while(cin >> n && n){
init();
int from, to;
cin >> from >> to;
for(int i = ; i <= n; i++){
int w;
cin >> w;
if(w + i <= n)
g[i][w + i] = ;
if(i - w >= )
g[i][i - w] = ;
}
dij(from);
if(dis[to] != INF)
cout << dis[to] << endl;
else cout << - << endl;
}
}
2. bfs解法
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int MAXN = ;
int n;
int from, to;
int k[MAXN];
int vis[MAXN];
// 5 1 5
// 3 3 1 2 5
//
struct Node{
int x, step;
}pos,q; void bfs(int start){
memset(vis,,sizeof(vis));
queue<Node> que;
pos.x = start;
pos.step = ;
que.push(pos);
vis[start] = ;
while(!que.empty()){
pos = que.front();
que.pop();
if(pos.x == to){
cout << pos.step << endl;
return ;
}
q.x = pos.x - k[pos.x];
if(q.x >= && vis[q.x] == ){
q.step = pos.step + ;
que.push(q);
vis[q.x] = ;
}
q.x = pos.x + k[pos.x];
if(q.x <= n && vis[q.x] == ){
q.step = pos.step + ;
que.push(q);
vis[q.x] = ;
}
}
cout << - << endl;
}
int main(){
while(cin >> n && n){
cin >> from >> to;
for(int i = ; i <= n; i++){
cin >> k[i];
}
bfs(from);
}
return ;
}
HDU_1548的更多相关文章
随机推荐
- java的Timer和TimerTask
java中Timer类使用的方法是如下的: Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() ...
- Python中的转换函数
https://www.cnblogs.com/wuxiangli/p/6046800.html python中的字符数字之间的转换函数 int(x [,base ]) 将x转换为 ...
- redis异常Redis:java.util.NoSuchElementException: Unable to validate object at
前两天项目上线的时候遇到了redis的一个问题,在测试环境的时候项目运行正常,项目一上线redis便开始抛异常. redis.clients.jedis.exceptions.JedisConnect ...
- java.lang.IllegalAccessError: tried to access method
java.lang.IllegalAccessException: access to method denied 06-23 16:12:39.128 1253-1253/com.hbjyjt.oa ...
- Pandas基本功能之算术运算、排序和排名
算术运算和数据对齐 Series和DataFrame中行运算和列运算有种特征叫做广播 在将对象相加时,如果存在不同的索引对,则结果的索引就是该索引对的并集.自动的数据对齐操作在不重叠的索引处引入了NA ...
- 关于OPEN_MAX宏undeclared的问题
最近在看unp时,I/O复用-poll一章的代码使用到了OPEN_MAX.据书中描述,这一宏定义在limits.h头文件中,指代一个进程在任意时刻能打开的最大描述符数目.但在代码编译时遇到了错误,提示 ...
- modal template
<div class="modal fade" id="tmp_order_modal" tabindex="-1" role=&qu ...
- tf.layers.dense()
tf.layers.dense用法 2018年05月30日 19:09:58 o0haidee0o 阅读数:20426 dense:全连接层 相当于添加一个层,即初学的add_layer()函数 ...
- 网页请求get方式
方法都是博客中的大神写的,谢谢各路大神. 方法一:(亲测有效) //Get请求方式 private string RequestGet(string Url) { string PageStr = s ...
- vue 路由参数变化,页面不更新的问题
监控$route 在vue项目中,假使我们在同一个路由下,只是改变路由后面的参数值,如果不监听路由参数值的变化,页面无数据刷新,需手动刷新浏览器,这样做就不是我们的预期效果. 举例:当前路由为 /p ...