码迷,mamicode.com
首页 > 其他好文 > 详细

hdu-1548 A strange lift---BFS

时间:2018-04-21 23:38:07      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:strong   acm   题目   bsp   jks   bit   nbsp   names   turn   

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1548

题目大意:

有一天桐桐做了一个梦,梦见了一种很奇怪的电梯。大楼的每一层楼都可以停电梯,而且第i层楼(1≤i≤N)上有一个数字K;(0≤Ki≤N)。电梯只有四 个按钮:开,关,上,下。上下的层数等于当前楼层上的那个数字。当然,如果不能满足要求,相应的按钮就会失灵。例如:3 3 1 2 5代表了Ki (K1=3,K2=3,…),从一楼开始。在一楼,按“上,”可以到4楼,按“下”是不起作用的,因为没有-2楼。那么,从A楼到B楼至少要按几次按钮 呢?

解题思路:

直接BFS即可

这里dijkstra也可以

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1000 + 10;
 4 const int INF = 0x3f3f3f3f;
 5 int c[maxn];
 6 bool vis[maxn];
 7 struct node
 8 {
 9     int x, step;
10     node(int x, int step):x(x), step(step){}
11 };
12 int n;
13 void BFS(int s, int t)
14 {
15     memset(vis, 0, sizeof(vis));
16     vis[s] = 1;
17     queue<node>q;
18     q.push(node(s, 0));
19     while(!q.empty())
20     {
21         node now = q.front();
22         //cout<<now.x<<" "<< now.step<<endl;
23         q.pop();
24         if(now.x == t)
25         {
26             cout<<now.step<<endl;
27             return;
28         }
29         int x = now.x + c[now.x];
30         if(x <= n && !vis[x])q.push(node(x, now.step + 1)), vis[x] = 1;
31         x = now.x - c[now.x];
32         if(x > 0 && !vis[x])q.push(node(x, now.step + 1)), vis[x]  =1;
33     }
34     cout<<"-1"<<endl;
35 }
36 int main()
37 {
38     while(cin >> n && n)
39     {
40         int a, b;
41         cin >> a >> b;
42         for(int i = 1; i <= n; i++)cin >> c[i];
43         BFS(a, b);
44     }
45     return 0;
46 }

 

hdu-1548 A strange lift---BFS

标签:strong   acm   题目   bsp   jks   bit   nbsp   names   turn   

原文地址:https://www.cnblogs.com/fzl194/p/8904158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!