Title link: https://acm.zcmu.edu.cn/JudgeOnline/problem.php?id=1578
Topic
A seat table with a length of n and a width of m has k noise sources, which produces noise with a noise value of s. Its influence on the surroundings decreases sharply according to the distance. The distance between two points is the sum of the absolute values of the abscissa and ordinate differences. Ask where the noise value is the smallest, the same small output i is small, and the same output j is small
范围:(0<n,m<100,0<=k<=n*m,0<=i<n,0<=j<m,0<s<10)
Ideas
Run bfs directly on the noise source! At first I felt that the depth was up to 10, not big, each bfs could not run much, and then only 1e4 bfs at most, and in the end several shots, woo woo woo (the vis array must be cleared each time, 1e4 1e4 operations, of course t).
In fact, you can directly enumerate O(n^2) without using bfs, run where the current noise source can affect, and then directly modify the noise value at that point.
ac code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ok(x, y) x >= 0 && x < n && y >= 0 && y < m //宏定义比调用函数快一点
int a[105][105];
int n, m;
int main(){
int k;
while(~scanf("%d%d%d", &n, &m, &k)){
for(int i = 0; i < n; i ++){
for(int j = 0; j < m; j ++){
a[i][j] = 0;
}
}
int x, y, s, dx, dy;
while(k --){
scanf("%d%d%d", &x, &y, &s);
a[x][y] += s;
for(int i = 1; i < s; i ++){ //这里要分开处理,不能放在下面的循环里面,因为a[x+0][y+j]和a[x-0][y+j]一样,会重复累加
if(ok(x, y + i)) a[x][y + i] += s - i;
if(ok(x, y - i)) a[x][y - i] += s - i;
if(ok(x + i, y)) a[x + i][y] += s - i;
if(ok(x - i, y)) a[x - i][y] += s - i;
}
for(int i = 1; i < s; i ++){
for(int j = 1; j < s; j ++){
if(i + j >= s) continue; //距离不能超过s,当然等于s的时候加0,也就不跑了
dx = x + i, dy = y + j;
if(ok(dx, dy)) a[dx][dy] += s - i - j;
dx = x + i, dy = y - j;
if(ok(dx, dy)) a[dx][dy] += s - i - j;
dx = x - i, dy = y + j;
if(ok(dx, dy)) a[dx][dy] += s - i - j;
dx = x - i, dy = y - j;
if(ok(dx, dy)) a[dx][dy] += s - i - j;
}
}
}
int ans = 0x3f3f3f3f, ansx, ansy;
for(int i = 0; i < n; i ++){
for(int j = 0; j < m; j ++){
if(ans > a[i][j]){
ansx = i, ansy = j;
ans = a[i][j];
}
}
}
printf("%d %d %d\n", ansx, ansy, ans);
}
return 0;
}