题意:
要从这边岸,踩着石头跳到那边岸;
给出对岸的距离y0,石头的数量n ,和一次最多跳多远d;
然后给出n块石头的横纵坐标;
问能不能跳到对岸,能输出YES和最少跳几次;
不能输出NO和离对岸最近的距离是多少;
思路:
bfs,先把第一次能跳到的放进队列,再把第二次能跳到的放进队列,一直找到最后;
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int N = 1005;
struct point {
int x,y;
int step;
}p[N];
int n,y0,d;
int getdis(point a, point b) {
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
void bfs() {
queue<point> q;
int dis = y0;
while(!q.empty())
q.pop();
for(int i = 0; i < n; i++) {
if(p[i].y <= d) {
dis = min(dis, y0 - p[i].y);
p[i].step = 1;
q.push(p[i]);
}
}
while(!q.empty()) {
point a = q.front();
q.pop();
if(y0 - a.y <= d) {
printf("YES\n%d\n",a.step + 1);
return;
}
for(int i = 0; i < n; i++) {
if(getdis(a,p[i]) <= d * d && p[i].step == -1) {
dis = min(dis, y0 - p[i].y);
p[i].step = a.step + 1;
q.push(p[i]);
}
}
}
printf("NO\n%d\n",dis);
return;
}
int main() {
int t;
scanf("%d",&t);
while(t--) {
scanf("%d%d%d",&y0, &n, &d);
for(int i = 0; i < n; i++) {
scanf("%d%d",&p[i].x, &p[i].y);
p[i].step = -1;
}
if(y0 <= d) {
printf("YES\n1\n");
}else {
bfs();
}
}
}