http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1301
由于格子数远大于鼠窝,所以以鼠窝为出发点,增加鼠窝周围d范围内的“格子的值”,最后扫描每个格子输出最大格子值即可。
完整代码:
/*0.078s*/ #include<bits/stdc++.h> using namespace std; const int MAXN = 1030; int cnt[MAXN][MAXN]; int main() { int T, d, n, i, j, p, q; int x, y, num, lmax; int x_min, x_max, y_min, y_max; scanf("%d", &T); while (T--) { scanf("%d%d", &d, &n); memset(cnt, 0, sizeof(cnt)); for (i = 0; i < n; ++i) { scanf("%d%d%d", &x, &y, &num); x_min = max(0, x - d), x_max = min(1024, x + d); y_min = max(0, y - d), y_max = min(1024, y + d); for (p = x_min; p <= x_max; ++p) for (q = y_min; q <= y_max; ++q) cnt[p][q] += num; } lmax = x = y = 0; for (i = 0; i < 1025; ++i) for (j = 0; j < 1025; ++j) if (cnt[i][j] > lmax) x = i, y = j, lmax = cnt[i][j]; printf("%d %d %d\n", x, y, lmax); } return 0; }