广度优先搜索

广度优先搜索根本思想是一层一层的遍历

抓住那头牛 

描述
农夫知道一头牛的位置,想要抓住它。农夫和牛都位于数轴上,农夫起始位于点N(0<=N<=100000),牛位于点K(0<=K<=100000)。农夫有两种移动方式:

从X移动到X-1或X+1,每次移动花费一分钟
从X移动到2*X,每次移动花费一分钟
假设牛没有意识到农夫的行动,站在原地不动。农夫最少要花多少时间才能抓住牛?

 输入

5 17

输出

4

#include<iostream>
#include<queue>
using namespace std;
int n, k;
const int MAX = 100000;
int vis[MAX + 10];
struct node
{
	int x;//当前位置
	int steps;//步数
};
queue<node> q;
int main()
{
	cin >> n >> k;
	node a;
	a.x = n, a.steps = 0;
	q.push(a);
	vis[n] = 1;

	while (!q.empty())
	{
		node ans = q.front();
		if (ans.x == k)
		{
			cout << ans.steps;
			return 0;
		}
		else
		{
			if (ans.x - 1 > 0 && vis[ans.x - 1] == 0)
			{
				node a0;
				vis[ans.x - 1] = 1;
				a0.x = ans.x - 1, a0.steps = ans.steps + 1;
				q.push(a0);
			}
			if (ans.x + 1 < MAX && vis[ans.x + 1] == 0)
			{
				node a0;
				vis[ans.x + 1] = 1;
				a0.x = ans.x + 1, a0.steps = ans.steps + 1;
				q.push(a0);
			}
			if (ans.x * 2 < MAX && vis[ans.x * 2] == 0)
			{
				node a0;
				vis[ans.x * 2] = 1;
				a0.x = ans.x * 2, a0.steps = ans.steps + 1;
				q.push(a0);
			}
			q.pop();
		}
	}
	return 0;
}

 马的遍历 - 洛谷

#include<iostream>
#include<cmath>
#include<queue>
#include<iomanip>
#include<memory.h>
using namespace std;
const int MAX = 450;
int n, m, x, y;
int dirx[10] = { 1,-1,1,-1,2,-2,-2,2 };
int diry[10] = { 2,-2,-2,2 ,1,-1,1,-1 };
int ans[MAX][MAX];
int vis[MAX][MAX];
struct node
{
	int x, y;
	int steps = 0;
};
void bfs(int x, int y)
{
	queue<node> q;
	node now, next, temp;
	now.x = x, now.y = y, now.steps = 0;
	ans[now.x][now.y] = now.steps;
	vis[now.x][now.y] = 1;
	q.push(now);

	while (!q.empty())
	{
		temp = q.front();
		q.pop();
		//cout << "此时到达了" << temp.x << " " << temp.y << " 已经走了" << steps << "步" << endl;
		for (int i = 0; i < 8; i++)
		{
			next.x = temp.x + dirx[i];
			next.y = temp.y + diry[i];

			if (next.x<1 || next.x>n)
				continue;
			if (next.y<1 || next.y>m)
				continue;

			if (vis[next.x][next.y] == 0)
			{
				vis[next.x][next.y] = 1;
				next.steps = temp.steps + 1;
				ans[next.x][next.y] = next.steps;
				q.push(next);
			}
		}
	}
}
int main()
{
	cin >> n >> m >> x >> y;
	memset(ans, -1, sizeof(vis));
	bfs(x, y);
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			if (i == x && j == y)
				cout << setw(5) << left << 0;
			else
				cout << setw(5) << left << ans[i][j];
		}
		cout << endl;
	}
	return 0;
}

蓝桥杯:迷宫

#include <cstdio>
#include <string>
#include<queue>
#include <iostream>
using namespace std;
 
int m,n;
char mp[510][510];
bool vis[510][510] =  {false};
int dir[4][2] = {{1,0},{0,-1},{0,1},{-1,0}};//移动的方向,参照字典序
char dirc[4] = {'D','L','R','U'};//按照字典序,下、左、右、上排序
 
struct Point{
    int x,y,step;
    string str;
    Point(int _x, int _y, int _step, string _str):x(_x),y(_y),step(_step),str(_str){}
};
 
queue<Point> q;
void bfs(int x, int y){
    q.push(Point(x, y, 0, ""));
    vis[x][y] = true; 
    while(!q.empty()){
        Point now = q.front();
        q.pop();
        if(now.x == m - 1 && now.y == n - 1){
            printf("%d\n", now.step);
            cout << now.str << endl;
            break;
        }
        for (int i = 0; i < 4; ++i) {
            int dx = now.x + dir[i][0];
            int dy = now.y + dir[i][1];

            if(dx < 0 || dx >= m || dy < 0 || dy >= n || vis[dx][dy] == true || mp[dx][dy] == '1')
                continue;
            else{
                q.push(Point(dx, dy, now.step + 1,  now.str + dirc[i]));
                vis[dx][dy] = true;
            }
        }
    }
}
 
int main(){
    scanf("%d%d", &m, &n);
    for (int i = 0; i < m; ++i) {
        scanf("%s",mp[i]);
    }
    bfs(0,0);
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值