初识BFS优先宽度搜索

算法 专栏收录该内容
1 篇文章 0 订阅

 

预备知识(写的可能不太好)

二叉树的基本知识

队列及其常见操作

树的层次遍历

所谓“层次遍历”就是从上到下,从左到右对树进行遍历,但是考虑到重复的问题,遍历时要去掉已经遍历过的节点,所以加入一个标记数组来优化

伪代码

这里队列的操作用到了c++的queue库

优化后的伪代码

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

题目意思

有一个奇怪的电梯,他可以停在任何一层,并且在每个楼层有一个Ki(0 <= Ki <= N)。电梯只有两个按钮:上下。当你在第i层,如果你按下“UP”按钮,你将上升Ki层,也就是说,你将会到达第i+Ki层,如果你按下“DOWN”按钮,你会下降 Ki层,即您将前往第i-Ki层。当然,电梯不能高于N不能低于1

例如,有5层的建筑物,并且k1=3,k2=3,k3=1,k4=2,k5=5。从1楼开始,你可以按下“UP”按钮,你会到4楼,如果你按下“DOWN”按钮,电梯不做处理,因为它不能下到-2楼。问题出在:当你在A楼,你想去B楼,他至少要按多少次按钮"UP"或"DOWN"?

输入由多个测试用例组成。
第一行包含三个整数 N、A、B(1 <= N、A、B <= 200),第二行由 N 整数 k1,k2 组成,....kn.
单个 0 表示输入的结束。

输出最小次数,没有输出-1

#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
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值