
2026-05-20:最好可到达的塔。用go语言,给定一个二维整数数组 towers,其中每个元素 towers[i] = [x_i, y_i, q_i] 表示第 i 座塔的坐标与质量因子。
再给定一个整数数组 center = [c_x, c_y] 表示你的所在位置,以及一个整数 radius。
判断规则:当某座塔与 center 的曼哈顿距离满足
|x_i - c_x| + |y_i - c_y| <= radius
时,这座塔被认为是“可到达”。
目标:在所有可到达的塔里,选择:
1.质量因子 q_i 最大的塔;
2.如果有多个塔的 q_i 相同,则在它们中选择坐标按字典序最小的那一个(先比较 x,x 更小者更优;若 x 相同,再比较 y,y 更小者更优)。
如果没有任何塔可到达,则返回 [-1, -1];否则返回该选中塔的坐标 [x_i, y_i]。
1 <= towers.length <= 100000。
towers[i] = [xi, yi, qi]。
center = [cx, cy]。
0 <= xi, yi, qi, cx, cy <= 100000。
0 <= radius <= 100000。
输入: towers = [[1,2,5], [2,1,7], [3,1,9]], center = [1,1], radius = 2。
输出: [3,1]。
解释:
塔 [1, 2, 5]:曼哈顿距离 = |1 - 1| + |2 - 1| = 1,可到达。
塔 [2, 1, 7]:曼哈顿距离 = |2 - 1| + |1 - 1| = 1,可到达。
塔 [3, 1, 9]:曼哈顿距离 = |3 - 1| + |1 - 1| = 2,可到达。
所有塔都是可到达的。最大质量因子为 9,对应塔 [3, 1]。
题目来自力扣3809。
程序从 main 函数开始运行,首先准备好题目给出的所有输入数据:
程序把所有输入数据传入 bestTower 函数,正式开始计算。
函数一开始会创建 3 个关键变量,用来记录当前最优塔的信息:
maxQ:记录当前找到的最大质量因子,初始值设为 -1(因为质量因子最小是 0,-1 代表还没找到任何可到达塔)minX:记录最优塔的 x 坐标,初始 -1minY:记录最优塔的 y 坐标,初始 -1这三个变量会在遍历过程中不断更新,最终保存最优塔的信息。
函数会依次检查每一座塔,对每一座塔执行以下判断流程:
所有塔检查完毕后:
maxQ = 9(不是 -1,说明找到可到达塔)[3, 1]main 函数拿到结果 [3,1],输出到控制台,程序结束。
遍历每一座可到达塔时,只有满足以下任一条件,才会更新最优塔:
如果没有任何塔可到达,最终返回 [-1, -1]。
.
package main
import (
"fmt"
)
func bestTower(towers [][]int, center []int, radius int) []int {
cx, cy := center[0], center[1]
maxQ, minX, minY := -1, -1, -1
for _, t := range towers {
x, y, q := t[0], t[1], t[2]
if abs(x-cx)+abs(y-cy) <= radius &&
(q > maxQ || q == maxQ && (x < minX || x == minX && y < minY)) {
maxQ, minX, minY = q, x, y
}
}
return []int{minX, minY}
}
func abs(x int)int {
if x < 0 {
return -x
}
return x
}
func main() {
towers := [][]int{{1, 2, 5}, {2, 1, 7}, {3, 1, 9}}
center := []int{1, 1}
radius := 2
result := bestTower(towers, center, radius)
fmt.Println(result)
}

.
# -*-coding:utf-8-*-
def best_tower(towers, center, radius):
cx, cy = center[0], center[1]
max_q, min_x, min_y = -1, -1, -1
for x, y, q in towers:
if abs(x - cx) + abs(y - cy) <= radius:
if (q > max_q or
q == max_q and (x < min_x or (x == min_x and y < min_y))):
max_q, min_x, min_y = q, x, y
return [min_x, min_y]
def main():
towers = [[1, 2, 5], [2, 1, 7], [3, 1, 9]]
center = [1, 1]
radius = 2
result = best_tower(towers, center, radius)
print(result)
if __name__ == "__main__":
main()
.
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
vector<int> bestTower(vector<vector<int>>& towers, vector<int>& center, int radius) {
int cx = center[0];
int cy = center[1];
int maxQ = -1;
int minX = -1;
int minY = -1;
for (const auto& tower : towers) {
int x = tower[0];
int y = tower[1];
int q = tower[2];
int manhattanDistance = abs(x - cx) + abs(y - cy);
if (manhattanDistance <= radius) {
if (q > maxQ) {
maxQ = q;
minX = x;
minY = y;
} elseif (q == maxQ) {
if (x < minX) {
minX = x;
minY = y;
} elseif (x == minX && y < minY) {
minY = y;
}
}
}
}
return {minX, minY};
}
int main() {
vector<vector<int>> towers = {{1, 2, 5}, {2, 1, 7}, {3, 1, 9}};
vector<int> center = {1, 1};
int radius = 2;
vector<int> result = bestTower(towers, center, radius);
cout << "[" << result[0] << ", " << result[1] << "]" << endl;
return0;
}
