算法:对于边权值是1的最短路径问题,采用BFS
迷宫路径
给定一个 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≤10000≤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
AC代码:
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N = 1010;
int n;
int g[N][N];
bool st[N][N];
PII per[N][N];
stack<PII> ans;
int dx[4] = {0,1,0,-1},dy[4] = {1,0,-1,0};
void bfs()
{
queue<PII> q;
q.push({0,0});
st[0][0] = true;
while(q.size())
{
PII t = q.front();
q.pop();
for(int i = 0 ; i< 4 ; i++)
{
int a = t.x+dx[i],b = t.y+dy[i];
if(a<0||a >= n || b < 0 || b>=n || st[a][b]) continue;
if(g[a][b]==1) continue;
st[a][b] = true;
q.push({a,b});
per[a][b] = {t.x,t.y};
}
}
int p = n-1 ;
int d = n-1;
ans.push({p,d});
while(p||d)
{
PII t = per[p][d];
ans.push(t);
p = t.x,d = t.y;
}
}
int main()
{
cin >> n;
for(int i = 0 ; i<n;i++)
for(int j= 0 ; j< n ; j++)
cin >> g[i][j];
bfs();
while(ans.size())
{
PII t = ans.top();
cout << t.x << " " << t.y << endl;
ans.pop();
}
return 0;
}
对于常见其他路径:
4方向偏移量:int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
8方向偏移量:int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
8方向日字形偏移量:int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
对于打印路径:
使用一个 PII pre[N][M] 数组 来记录每个点(a,b)是从哪里转移过来的 pre[a][b] = t
武士风度的牛
农民 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
AC代码
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 155;
int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
char g[N][N];
int dis[N][N];
int n,m;
int s1,s2;
int e1,e2;
int bfs()
{
memset(dis,-1,sizeof dis);
queue<PII> q;
q.push({s1,s2});
dis[s1][s2] = 0;
while(q.size())
{
PII t = q.front();
q.pop();
for(int i = 0 ; i<8;i++)
{
int a = t.x + dx[i],b = t.y + dy[i];
if(a<0||a>=n || b <0||b>=m) continue;
if(dis[a][b]!=-1) continue;
if(g[a][b]=='*') continue;
q.push({a,b});
dis[a][b] = dis[t.x][t.y] + 1;
}
if(dis[e1][e2] != -1) break;
}
return dis[e1][e2];
}
int main()
{
cin >> m >> n;
for(int i = 0 ; i < n ; i++) cin >> g[i];
for(int i = 0 ; i < n ; i++)
for(int j = 0 ; j < m ; j++)
{
if(g[i][j]=='K') s1 = i , s2 = j;
if(g[i][j]=='H') e1 = i , e2 = j;
}
//cout << s1 << " " << s2 << endl;
//cout << e1 << " " << e2 << endl;
cout << bfs();
return 0;
}
抓住那头牛
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n,k;
int dis[N];
int bfs()
{
memset(dis,-1,sizeof dis);
queue<int> q;
q.push(n);
dis[n] = 0;
while(q.size())
{
int t = q.front();
q.pop();
if(t==k) return dis[k];
int a = t * 2;
if( a < N && dis[a]==-1) {q.push(a);dis[a] = dis[t] + 1;}
/*if(t*2 < N && dis[t*2]==-1)
{
dis[t*2] = dis[t] + 1;
q.push(t*2);
}*/
int b = t - 1;
int c = t + 1;
if(b >= 0 && dis[b]==-1 && b < 2e5 ) q.push(b),dis[b] = dis[t] + 1;
if(c >= 0 && dis[c]==-1 && c < 2e5 ) q.push(c),dis[c] = dis[t] + 1;
}
return -1;
}
int main()
{
cin >> n >> k;
cout << bfs();
return 0;
}