思路
因为离山顶距离越远高度越小,两维均有单调性,所以原图其实可以以山顶划分为四个杨表,于是直接对于每一个杨表 查找对应的高度即可。
杨表的食用方式
对于一个 行 列的杨表 ,不妨设其对于任意位置 :
- 若 :;
- 若 :。
首先初始位置应为 ,原因是这样子 更小值就会变大, 更大值就会更大,可以通过与查找值的大小关系确定应该调整 还是 。
设要查找的值为 ,当前的位置为 。
- 若 ,显然的肯定要将 加一;
- 若 ,已经找到目标;
- 若 ,大了,于是将 减 使值更小。
Code
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void Rescue(int R, int C, int RS, int CS, int X);
int Measure(int RM, int CM);
void Pinpoint(int RP, int CP);
void Rescue(int R, int C, int RS, int CS, int X) {
auto check = [&](int sr, int sc) {
// r = RS + sr * x
// c = CS + sc * y
int nr = sr == 1 ? R - RS : RS - 1;
int nc = sc == 1 ? C - CS : CS - 1;
int x = 0, y = nc;
while (x <= nr && y >= 0) {
int r = RS + sr * x;
int c = CS + sc * y;
int h = Measure(r, c);
if (h == X) {
Pinpoint(r, c);
return true;
}
if (h > X) ++x;
else --y;
}
return false;
};
for (int sr : {-1, 1}) {
for (int sc : {-1, 1}) {
if (check(sr, sc))
return;
}
}
}