橙名之后第一篇题解!
思路
题目的信息很明确,也十分简单,跑个 bfs 就行了。
可是,我们需要求出每一个点到最近的树的曼哈顿距离。
根据曼哈顿距离的定义,其实就是一个点到另一个点的最短路。所以,把所有树的位置放入队列,再用 bfs 求出最短路即可。
code
#include<bits/stdc++.h>
using namespace std;
const int maxn=505;
int mhd[maxn][maxn],n,m;
bool danger[maxn][maxn],vis[maxn][maxn];
struct wolf{
int x;
int y;
int Manhattan_distance;//Vjekoslav 在逃回窝的途中离它最近的树的距离的最小值
bool operator < (const wolf &tmp) const {
//重载一下运算符
return Manhattan_distance<tmp.Manhattan_distance;
}
};
struct node{
int x,y,h;
}e,s;
int dx[4]={
0,0,1,-1};//方向函数
int dy