提示:
1 <= towers.length <= 50
towers[i].length == 3
0 <= xi, yi, qi <= 50
1 <= radius <= 50
输入:towers = [[1,2,5],[2,1,7],[3,1,9]], radius = 2
输出:[2,1]
解释:
坐标 (2, 1) 信号强度的计算示例
- 塔 (2, 1) 强度参数为 7 ,在该点强度为 ⌊7 / (1 + sqrt(0)⌋ = ⌊7⌋ = 7
- 塔 (1, 2) 强度参数为 5 ,在该点强度为 ⌊5 / (1 + sqrt(2)⌋ = ⌊2.07⌋ = 2
- 塔 (3, 1) 强度参数为 9 ,在该点强度为 ⌊9 / (1 + sqrt(1)⌋ = ⌊4.5⌋ = 4其他点的计算类似。
经计算易知:坐标(2,1)的信号强度7+2+4=13是最大的。
思路:
直接暴力枚举就可以了。
定义map[51][51],存储每个点的强度。
遍历towers中的每一个点,计算towers[i]能到达距离内的点的强度,累加点(x,y)处的强度。
最后遍历map,找到最大值对应的坐标。(这里是从横纵坐标都是从0开始遍历的,遇到相等的值,不会进行操作,因此找到的坐标就是按字典序的)
class Solution {
public:
vector<int> bestCoordinate(vector<vector<int>>& towers, int radius) {
int map[51][51];
for(int i=0;i<=50;i++){
for(int j=0;j<=50;j++){
map[i][j]=0;
}
}
for(int i=0;i<towers.size();i++){
for(int m=0;m<=50;m++){
for(int n=0;n<=50;n++){
double d = sqrt((towers[i][0]-m)*(towers[i][0]-m)+(towers[i][1]-n)*(towers[i][1]-n));
if(d<=radius){
map[m][n]+=(int)((towers[i][2])/(1+d));
}
}
}
}
vector<int>a(2);
int max=-99;
for(int m=0;m<=50;m++){
for(int n=0;n<=50;n++){
if(map[m][n]>max){
max=map[m][n];
a[0]=m;
a[1]=n;
}
}
}
return a;
}
};