算法学习系列(四十二):最短路模型

本文介绍了如何使用宽度优先搜索(BFS)算法解决迷宫问题,寻找从左上角到右下角的最短路径,并扩展到类似“武士风度的牛”和“抓住那头牛”的问题,涉及抽象模型、图的构建和数轴上的路径优化。
摘要由CSDN通过智能技术生成

引言

关于这个最短路问题还是得要好好刷题见题才行啊,不然这个其实模板都是差不多的,就是可能怎么把它转化为 B F S BFS BFS 是个问题,以及能不能想到用也是一个问题,其实就是你做过这种题,那么你就会,就是这样,加油!


一、迷宫问题

标签:BFS

思路:就是普通的 B F S BFS BFS 模型,不一样的是该题需要输出路径,我们可以拿一个二维 p a i r pair pair 数组来存该点的上一个坐标,为了方便我们从后向前遍历,这样就可以直接从源点按顺序打印了。

题目描述:

给定一个 n×n 的二维数组,如下所示:

int maze[5][5] = {

0, 1, 0, 0, 0,

0, 1, 0, 1, 0,

0, 0, 0, 0, 0,

0, 1, 1, 1, 0,

0, 0, 0, 1, 0,

};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

数据保证至少存在一条从左上角走到右下角的路径。

输入格式
第一行包含整数 n。

接下来 n 行,每行包含 n 个整数 0 或 1,表示迷宫。

输出格式
输出从左上角到右下角的最短路线,如果答案不唯一,输出任意一条路径均可。

按顺序,每行输出一个路径中经过的单元格的坐标,左上角坐标为 (0,0),右下角坐标为 (n−1,n−1)。

数据范围
0≤n≤1000
输入样例:
5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
输出样例:
0 0
1 0
2 0
2 1
2 2
2 3
2 4
3 4
4 4

示例代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
#define x first
#define y second

const int N = 1010;

int n;
int g[N][N];
bool st[N][N];
PII pre[N][N];

int dir[4][2] = {0,1,0,-1,1,0,-1,0};

void bfs(PII S)
{
	st[S.x][S.y] = true;
	
	queue<PII> q; q.push(S);
	while(q.size())
	{
		auto t = q.front(); q.pop();
		
		for(int i = 0; i < 4; ++i)
		{
			int x = t.x + dir[i][0];
			int y = t.y + dir[i][1];
			if(x < 0 || x >= n || y < 0 || y >= n) continue;
			if(st[x][y] || g[x][y] == 1) continue;
			
			pre[x][y] = {t.x,t.y};
			st[x][y] = true;
			q.push({x,y});
		}
	}
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	
	cin >> n;
	for(int i = 0; i < n; ++i)
	{
		for(int j = 0; j < n; ++j)
		{
			cin >> g[i][j];
		}
	}
	
	bfs({n-1,n-1});
	
	PII end(0,0);
	while(true)
	{
		cout << end.x << " " << end.y << endl;
		if(end.x == n - 1 && end.y == n - 1) break;
		end = pre[end.x][end.y];
	}
	
	return 0;
}

二、武士风度的牛

标签:BFS

思路:这道题就是把方向改成需要的 “日” 字就行了,按对应的改变下标去做就行了,剩余的就是 B F S BFS BFS 模板了。

题目描述:

农民 John 有很多牛,他想交易其中一头被 Don 称为 The Knight 的牛。

这头牛有一个独一无二的超能力,在农场里像 Knight 一样地跳(就是我们熟悉的象棋中马的走法)。

虽然这头神奇的牛不能跳到树上和石头上,但是它可以在牧场上随意跳,我们把牧场用一个 x,y 的坐标图来表示。

这头神奇的牛像其它牛一样喜欢吃草,给你一张地图,上面标注了 The Knight 的开始位置,树、灌木、石头以及其它障碍的位置,除此之外还有一捆草。

现在你的任务是,确定 The Knight 要想吃到草,至少需要跳多少次。

The Knight 的位置用 K 来标记,障碍的位置用 * 来标记,草的位置用 H 来标记。

这里有一个地图的例子:

             11 | . . . . . . . . . .
             10 | . . . . * . . . . . 
              9 | . . . . . . . . . . 
              8 | . . . * . * . . . . 
              7 | . . . . . . . * . . 
              6 | . . * . . * . . . H 
              5 | * . . . . . . . . . 
              4 | . . . * . . . * . . 
              3 | . K . . . . . . . . 
              2 | . . . * . . . . . * 
              1 | . . * . . . . * . . 
              0 ----------------------
                                    1 
                0 1 2 3 4 5 6 7 8 9 0 
The Knight 可以按照下图中的 A,B,C,D… 这条路径用 5 次跳到草的地方(有可能其它路线的长度也是 5):

             11 | . . . . . . . . . .
             10 | . . . . * . . . . .
              9 | . . . . . . . . . .
              8 | . . . * . * . . . .
              7 | . . . . . . . * . .
              6 | . . * . . * . . . F<
              5 | * . B . . . . . . .
              4 | . . . * C . . * E .
              3 | .>A . . . . D . . .
              2 | . . . * . . . . . *
              1 | . . * . . . . * . .
              0 ----------------------
                                    1
                0 1 2 3 4 5 6 7 8 9 0
注意: 数据保证一定有解。

输入格式
第 1 行: 两个数,表示农场的列数 C 和行数 R。

第 2..R+1 行: 每行一个由 C 个字符组成的字符串,共同描绘出牧场地图。

输出格式
一个整数,表示跳跃的最小次数。

数据范围
1≤R,C≤150
输入样例:
10 11
..........
....*.....
..........
...*.*....
.......*..
..*..*...H
*.........
...*...*..
.K........
...*.....*
..*....*..
输出样例:
5

示例代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
#define x first
#define y second

const int N = 200;

PII S;
int n, m;
char g[N][N];
int dist[N][N];

int dir[8][2] = {-2,-1,-2,1,-1,-2,-1,2,1,-2,1,2,2,-1,2,1};

int bfs()
{
	memset(dist, -1, sizeof dist);
	dist[S.x][S.y] = 0;
	
	queue<PII> q; q.push(S);
	while(q.size())
	{
		auto t = q.front(); q.pop();
		
		if(g[t.x][t.y] == 'H') return dist[t.x][t.y];
		
		for(int i = 0; i < 8; ++i)
		{
			int x = t.x + dir[i][0];
			int y = t.y + dir[i][1];
			if(x < 0 || x >= n || y < 0 || y >= m) continue;
			if(dist[x][y] != -1 || g[x][y] == '*') continue;
			
			dist[x][y] = dist[t.x][t.y] + 1;
			q.push({x,y});
		}
	}
	
	return -1;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	
	cin >> m >> n;
	for(int i = 0; i < n; ++i) 
	{
		cin >> g[i];
		for(int j = 0; j < m; ++j)
		{
			if(g[i][j] == 'K') S = {i,j};
		}
	}
	
	cout << bfs() << endl;

	
	return 0;
}

三、抓住那头牛

标签:BFS

思路:这道题可以抽象出一个模型:一个点可以与三个点连边,权值为 1 1 1 ,这样可以建图来做,值得注意的是,最多可以向 2 ∗ 1 0 5 2 * 10 ^ 5 2105 的点连边,因为有可能先二倍再往回退,这样可能是最优的,然后再 B F S BFS BFS 就行了。第二种就是光改变方向就行了,就是三个方向而已,加三个判断即可。详情可见代码。

题目描述:

农夫知道一头牛的位置,想要抓住它。

农夫和牛都位于数轴上,农夫起始位于点 N,牛位于点 K。

农夫有两种移动方式:

从 X 移动到 X−1 或 X+1,每次移动花费一分钟从 X 移动到 2∗X,每次移动花费一分钟假设牛没有意识到农夫的行动,站在原地不动。

农夫最少要花多少时间才能抓住牛?

输入格式
共一行,包含两个整数N和K。

输出格式
输出一个整数,表示抓到牛所花费的最少时间。

数据范围
0≤N,K≤105
输入样例:
5 17
输出样例:
4

示例代码1:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
#define x first
#define y second

const int N = 2e5+10;

int a, b;
int dist[N];

int bfs()
{
	memset(dist, -1, sizeof dist);
	dist[a] = 0;
	
	queue<int> q; q.push(a);
	while(q.size())
	{
		int t = q.front(); q.pop();
		
		if(t == b) return dist[b];
		
		if(t - 1 >= 0 && dist[t-1] == -1)
		{
			dist[t-1] = dist[t] + 1;
			q.push(t-1);
		}
		if(t + 1 < N && dist[t+1] == -1)
		{
			dist[t+1] = dist[t] + 1;
			q.push(t+1);
		}
		if(t * 2 < N && dist[t*2] == -1)
		{
			dist[t*2] = dist[t] + 1;
			q.push(t*2);
		}
	}
	
	return -1;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	
	cin >> a >> b;
	
	cout << bfs() << endl;
	
	return 0;
}

示例代码2: 建图

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
#define x first
#define y second

const int N = 5e5+10, M = N * 3;

int a, b;
int h[N], e[M], ne[M], idx;
int dist[N];

void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

int bfs()
{
	memset(dist, -1, sizeof dist);
	dist[a] = 0;
	
	queue<int> q; q.push(a);
	while(q.size())
	{
		int t = q.front(); q.pop();
		
		if(t == b) return dist[b];
		
		for(int i = h[t]; i != -1; i = ne[i])
		{
			int j = e[i];
			if(dist[j] != -1) continue;
			
			dist[j] = dist[t] + 1;
			q.push(j);
		}
	}
	return -1;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	
	memset(h, -1, sizeof h);
	cin >> a >> b;
	
	for(int i = 0; i < 2e5+10; ++i)
	{
		add(i,i+1),add(i,i-1),add(i,i*2);
	}
	
	cout << bfs() << endl;
	
	return 0;
}
  • 32
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lijiachang030718

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值