# 初识BFS优先宽度搜索

1 篇文章 0 订阅

bfs(node source,code target)  //数据类型为node的初始节点source，目标节点target
{
memset(vis,0,sizeof(vis));  //数组vis标记该节点是否经过
Queue<node> Q;  //创建一个队列
Q.push(source);  //将初始节点入队
vis[source] = 1;
while (!Q.empty())  //当队列不为空时
{
Node cur = Q.front();  //记录当前节点
Q.pop();  //将队首节点出队
if (cur == target) {
return cur;
}
if (对于cur所有的后继节点nex)
if(vis[nex]==0){
Q.push(nex);  //把后续节点入队
vis[nex] = 1;  //标记为已读
}
}
return NULL;
}


hdu 1548 A strage lift

Problem Description

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

#include<bits/stdc++.h>
using namespace std;
int N,Start,End;
int visit[202];
int k[202];
//定义节点
struct pos{
int level;  //节点值
int step;  //节点所在的层次
};

void bfs()
{

pos cur,next;  //当前节点与下一节点
queue<pos> Q;
cur.level = Start;  //根节点赋值起始值
cur.step = 0;  //根节点所在层次为0，即未进行操作
Q.push(cur);
visit[Start] = 1;  //该节点已经过，标记

while (!Q.empty())  //层次遍历
{
cur = Q.front();
Q.pop();
if (cur.level == End) {
printf("%d\n",cur.step);
return ;
}
//左节点电梯向上升
next.level = cur.level + k[cur.level];
next.step = cur.step + 1;
if (next.level<=N)
{
if (visit[next.level]==0){
visit[next.level] = 1;
Q.push(next);
}
}
//右节点电梯下降
next.level = cur.level - k[cur.level];
next.step = cur.step + 1;
if (next.level>=1)
{
if (visit[next.level]==0){
visit[next.level] = 1;
Q.push(next);
}
}
}
printf("-1\n");
return ;
}
int main()
{
int i,j;
while (scanf("%d",&N)&&N)
{
memset(visit,0,202*sizeof(int));
scanf("%d%d",&Start,&End);
for (i=1;i<=N;i++){
scanf("%d",&k[i]);
}
bfs();
}
return 0;
}
*********************************************************

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

03-10 91
03-01 61
08-23 779
08-30 1286
04-16 335
10-30 2455
08-22 72
03-29 7754
01-25 87
10-14 33
©️2021 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。