搜索与图论:最短路模型

搜索与图论:最短路模型


迷宫问题

给定一个 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
typedef pair<int,int> PII;
PII end(0,0);

以上代码表示初始化PII类型的end的值为{0,0},相当于PII end={0,0}.

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 1010, M = N * N;
int n;
int g[N][N];
PII pre[N][N]; // 记录前一个位置 
void bfs(int sx, int sy)
{
	int dx[4] = {-1, 0, 1, 0};
	int dy[4] = {0, 1, 0, -1};
	
	queue<PII> q;
	q.push({sx, sy});
	
	memset(pre, -1, sizeof pre);
	pre[sx][sy] = {0, 0};
	
	while (q.size())
	{
		PII t = q.front();
		q.pop();
		for (int i = 0; i < 4; i ++ )
		{
			int x = t.x + dx[i];
			int y = t.y + dy[i];
			if (x >= 0 && x < n && y >= 0 && y < n && g[x][y] == 0 && pre[x][y].x == -1 && pre[x][y].y == -1)
			{
				q.push({x, y});
				pre[x][y] = t;	
			}	
		} 
	}
}
int main()
{
	cin >> n;
	for (int i = 0; i < n; i ++ )
		for (int j = 0; j < n; j ++ )
			cin >> g[i][j];
			
	// 倒着BFS一遍 
	bfs(n - 1, n - 1);
	
	PII end(0, 0); // PII end = {0, 0};
	
	while (true)
	{
		printf("%d %d\n", end.x, end.y);
		if (end.x == n - 1 && end.y == n - 1) break;
		end = pre[end.x][end.y]; 
	}
	
	return 0;
}

武士风度的牛

农民 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 <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 200;
int n, m;
int dist[N][N];
char g[N][N];
int bfs(int sx, int sy, int ex, int ey)
{
	int dx[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
	int dy[8] = {1, -1, 2, -2, 2, -2, 1, -1};
	
	queue<PII> q;
	q.push({sx, sy});

	memset(dist, -1, sizeof dist);
	dist[sx][sy] = 0;
	
	while (q.size())
	{
		PII t = q.front();
		q.pop();
		
		for (int i = 0; i < 8; i ++ )
		{
			int x = t.x + dx[i];
			int y = t.y + dy[i];
//			if (g[x][y] == 'H') return dist[t.x][t.y] + 1; 
			if (x >= 0 && x < m && y >= 0 && y < n && (g[x][y] == '.' || g[x][y] == 'H') && dist[x][y] == -1)
			{
				dist[x][y] = dist[t.x][t.y] + 1;
				q.push({x, y});
			}
		}
	}
	
	return dist[ex][ey];
}
int main()
{
	PII beg, en;
	cin >> n >> m;
	for (int i = 0; i < m; i ++ )
		for (int j = 0; j < n; j ++ )
		{
			cin >> g[i][j];
			if (g[i][j] == 'K') beg = {i, j};
			if (g[i][j] == 'H') en = {i, j};
		}	
		
	cout << bfs(beg.x, beg.y, en.x, en.y) << endl;
	
	return 0;
}

抓住那头牛

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

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

农夫有两种移动方式:

  1. 从 X 移动到 X−1 或 X+1,每次移动花费一分钟
  2. 从 X 移动到 2∗X,每次移动花费一分钟

假设牛没有意识到农夫的行动,站在原地不动。

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

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

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

数据范围
0 ≤ N,K ≤ 105

输入样例:

5 17

输出样例:

4
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1e5 + 10;
int n, m;
int dist[N];
int bfs()
{
	queue<int> q;
	q.push(n);
	
	memset(dist, -1, sizeof dist);
	dist[n] = 0;
	
	while (q.size())
	{
		int t = q.front();
		q.pop();
			
		if (t == m) return dist[t];
		
		if (t + 1 < N && dist[t + 1] == -1)
		{
			dist[t + 1] = dist[t] + 1;
			q.push(t + 1);
		}
		
		if (t - 1 >= 0 && 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()
{
	cin >> n >> m;
	cout << bfs() << endl;
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值