Rat Attack POJ - 1916

本文介绍了一种通过放置智能气体炸弹来消灭城市下水道中老鼠的方法。利用炸弹的特定扩散范围,通过计算不同位置放置炸弹所能覆盖的老鼠巢穴数量,确定最佳放置位置。
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

题目大意:

输入炸弹的强度,然后让你求在哪个位置放炸弹能杀死的老鼠最多。

思路:

一开始我遍历了每一个格子,发现这题的范围太大了,一定会超,然后发现只用从有老鼠的格子按炸弹强度开始遍历就行了,因为每一次遍历的交集自然是放炸弹的最好位置(好吧,我承认是别人提示的)

下面是ac代码:

#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std;
int mp[1025][1025],t;
int main()
{
    int power,cnt,n,m,ans,sum,mxx,mxy;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&power);
        sum=0;
        memset(mp,0,sizeof(mp));
        scanf("%d",&cnt);
        while(cnt--)
        {
            scanf("%d%d%d",&n,&m,&ans);
            for(int i=n-power;i<=n+power;i++)
            {
                for(int j=m-power;j<=m+power;j++)
                {
                    if(i<=0||j<=0||i>=1025||j>=1025)
                        continue;
                    mp[i][j]+=ans;
                    if(mp[i][j]>sum)
                    {
                        sum=mp[i][j];
                        mxx=i;
                        mxy=j;
                    }
                }
            }
        }
        printf("%d %d %d\n",mxx,mxy,sum);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值