zwjason

V1

2022/11/02阅读：20主题：默认主题

# 代码

``class Solution {public:    // 二维空间内欧几里得距离    // 即两点之间的直线距离    double distance(int x1, int y1, int x2, int y2) {        return sqrt(pow(x1 - x2, 2.0) + pow(y1 - y2, 2.0));    }    vector<int> bestCoordinate(vector<vector<int>>& towers, int radius) {        // 求出所有可能点的边界        int xLimit = INT_MIN, yLimit = INT_MIN;        for (vector<int> &tower : towers) {            int x = tower[0], y = tower[1];            xLimit = max(xLimit, x);            yLimit = max(yLimit, y);        }        // 遍历范围内所有点        int maxSignal = 0, xMax = 0, yMax = 0;        for (int i = 0; i <= xLimit; ++i) {            for (int j = 0; j <= yLimit; ++j) {                int signal = 0;     // 点(i, j)的信号强度                // 计算所有塔对该点信号强度的贡献                for (vector<int> &tower : towers) {                    int x = tower[0], y = tower[1], q = tower[2];                    double d = distance(i, j, x, y);                    if (d <= (double)radius) {                        signal += floor(q / (1.0 + d));                    }                }                // 取最大                // 因为按照字典序升序遍历所有点                // 所以不用考虑相等的情况                if (maxSignal < signal) {                    xMax = i;                    yMax = j;                    maxSignal = signal;                }            }        }        return vector<int>{xMax, yMax};    }};``

V1