UVA10360 POJ1916 Rat Attack枚举

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10360 POJ1916 Rat Attack枚举相关的知识,希望对你有一定的参考价值。

Rat Attack
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 1106 Accepted: 365

Description

Background
Baaaam! Another deadly gas bomb explodes in Manhattan’s underworld. Rats have taken over the sewerage and the city council is doing everything to get the rat population under control.
As you know, Manhattan is organized in a regular fashion with streets and avenues arranged like a rectangular grid. Waste water drains run beneath the streets in the same arrangement and the rats have always set up their nests below street intersections. The only viable method to extinguish them is to use gas bombs like the one which has just exploded. However, gas bombs are not only dangerous for rats. The skyscrapers above the explosion point have to be evacuated in advance and so the point of rat attack must be chosen very carefully.
The gas bombs used are built by a company called American Catastrophe Management (ACM) and they are sold under the heading of “smart rat gas”. They are smart because – when fired – the gas spreads in a rectangular fashion through the understreet canals. The strength of a gas bomb is given by a number d which specifies the rectangular “radius” of the gas diffusion area. For example, Figure 2 shows what happens when a bomb with d = 1 explodes.

The Problem
The area of interest consists of a discrete grid of 1025 * 1025 fields. Rat exterminator scouts have given a detailed report on where rat populations of different sizes have built their nests. You are given a gas bomb with strength d and your task is to find an explosion location for this gas bomb which extinguishes the largest number of rats.
The best position is determined by the following criteria:
The sum of all rat population sizes within the diffusion area of the gas bomb (given by d) is maximal.
If there is more than one of these best positions then the location with the “minimal” position will be chosen. Positions are ordered first by their x coordinate and second by their y coordinate.

Formally, given a location (x1, y1) on the grid, a point (x2, y2) is within the diffusion area of a gas bomb with strength d if the following equation holds:
max(abs(x2 - x1), abs(y2 - y1)) <= d

Input

The first line contains the number of scenarios in the input.
For each scenario the first line contains the strength d of the gas bomb in the scenario (1 <= d <= 50). The second line contains the number n (1 <= n <= 20000) of rat populations. Then for every rat population follows a line containing three integers separated by spaces for the position (x, y) and “size” i of the population (1 <= i <= 255). It is guaranteed that position coordinates are valid (i.e., in the range between 0 and 1024) and no position is given more than once.

Output

For every problem print a line containing the x and y coordinate of the choosen location for the gas bomb,followed by the sum of the rat population sizes which will be extinguished. The three numbers must be separated by a space.

Sample Input

1
1
2
4 4 10
6 6 20

Sample Output

5 5 30

Source

TUD Programming Contest 2001, Darmstadt, Germany

问题链接UVA10360 POJ1916 Rat Attack
问题简述:(略)
问题分析:求矩阵的子矩阵元素和最大问题,用枚举来实现。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10360 POJ1916 Rat Attack */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 1025;
int g[N][N];

int main()
{
    int t, d, n;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &d, &n);

        memset(g, 0, sizeof g);
        int x, y, size;
        while (n--) {
            scanf("%d%d%d", &x, &y, &size);
            int xl = max(0, x - d);
            int xr = min(x + d, N - 1);
            int yl = max(0, y - d);
            int yr = min(y + d, N - 1);
            for(int i = xl; i <= xr; i++)
                for(int j = yl; j <= yr; j++)
                    g[i][j] += size;
        }

        int mx = -1, rx, ry;
        for(int i = 0; i < N; i++)
            for(int j = 0; j < N; j++)
                if(g[i][j] > mx)
                    mx = g[i][j], rx = i, ry = j;

        printf("%d %d %d\\n", rx, ry, mx);
    }

    return 0;
}

以上是关于UVA10360 POJ1916 Rat Attack枚举的主要内容,如果未能解决你的问题,请参考以下文章

UVA 322 ships (POJ 1138)

2018 Spring Single Training B (uva 572,HihoCoder 1632,POJ 2387,POJ 2236,UVA 10054,HDU 2141)

2019年7月做题记录

POJ 1015 / UVA 323 Jury Compromise(01背包,打印路径)

UVA11115 POJ3199 Uncle Jack大数

UVA10998 POJ2857 Flipping ColorsDFS