农民 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
queue队列写法:
#include <iostream>
#include <queue>
#include <cstring>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N=200;
int n,m,step[N][N];
char g[N][N];
bool st[N][N];
void bfs(int sx,int sy)
{
memset(step,-1,sizeof step);
queue<PII> q;
q.push({sx,sy});
st[sx][sy]=true;
step[sx][sy]=0;
while(q.size())
{
auto t=q.front();
q.pop();
int dx[10]={-1,-2,-2,-1,1,2,2,1},dy[10]={-2,-1,1,2,2,1,-1,-2};
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(g[a][b]=='*') continue;
if(st[a][b]) continue;
q.push({a,b});
st[a][b]=true;
step[a][b]=step[t.x][t.y]+1;
}
}
}
int main()
{
int sx,sy,ex,ey;
cin>>m>>n;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
cin>>g[i][j];
if(g[i][j]=='K')
{
sx=i,sy=j;
}
if(g[i][j]=='H')
{
ex=i,ey=j;
}
}
bfs(sx,sy);
cout<<step[ex][ey]<<endl;
return 0;
}
模拟队列写法:
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
#define Bug cout<<"---------------------"<<endl
using namespace std;
#define x first
#define y second
const int N = 160, M = N * N;
int n, m;
int sx, sy;
typedef pair<int, int>pii;
char mp[N][N];
pii q[M];
int dis[N][N];
const int dir[8][2] = { {2,1},{-2,1},{2,-1},{-2,-1},{-1,2},{1,2},{-1,-2},{1,-2} };//方向数组
bool in(int x, int y) {
return x >= 0 && x < n&& y >= 0 && y < m;
}
void bfs(int x, int y) {
memset(dis, -1, sizeof dis);//可以用于判断是否到过这个点
int hh = 0, tt = 0;
dis[x][y] = 0;
q[0] = { x,y };//初始状态;
while (hh <= tt) {
pii now = q[hh++];
for (int i = 0;i < 8;i++) {
int tx = now.x + dir[i][0];
int ty = now.y + dir[i][1];
if (mp[tx][ty] == 'H') {//到达终点
cout << dis[now.x][now.y] + 1;
return;
}
if (dis[tx][ty]==-1&&in(tx, ty) && mp[tx][ty] != '*') {
q[++tt] = { tx,ty };
dis[tx][ty] = dis[now.x][now.y] + 1;//距离+1;
}
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> m >> n;
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
cin >> mp[i][j];
if (mp[i][j] == 'K') {
sx = i;
sy = j;
}
}
}
bfs(sx,sy);
return 0;
}