poj 3034 Whac-a-Mole

Whac-a-Mole
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3945 Accepted: 1158

Description

While visiting a traveling fun fair you suddenly have an urge to break the high score in the Whac-a-Mole game. The goal of the Whac-a-Mole game is to… well… whack moles. With a hammer. To make the job easier you have first consulted the fortune teller and now you know the exact appearance patterns of the moles.

The moles appear out of holes occupying the n2 integer points (xy) satisfying 0 ≤ xy < n in a two-dimensional coordinate system. At each time step, some moles will appear and then disappear again before the next time step. After the moles appear but before they disappear, you are able to move your hammer in a straight line to any position (x2y2) that is at distance at most d from your current position (x1y1). For simplicity, we assume that you can only move your hammer to a point having integer coordinates. A mole is whacked if the center of the hole it appears out of is located on the line between (x1y1) and (x2y2) (including the two endpoints). Every mole whacked earns you a point. When the game starts, before the first time step, you are able to place your hammer anywhere you see fit.

Input

The input consists of several test cases. Each test case starts with a line containing three integers nd and m, where n and d are as described above, and m is the total number of moles that will appear (1 ≤ n ≤ 20, 1 ≤ d ≤ 5, and 1 ≤ m ≤ 1000). Then follow m lines, each containing three integers xy and t giving the position and time of the appearance of a mole (0 ≤ xy < n and 1 ≤ t ≤ 10). No two moles will appear at the same place at the same time.

The input is ended with a test case where n = d = m = 0. This case should not be processed.

Output

For each test case output a single line containing a single integer, the maximum possible score achievable.

Sample Input

4 2 6
0 0 1
3 1 3
0 1 2
0 2 2
1 0 2
2 0 2
5 4 3
0 0 1
1 2 1
2 4 1
0 0 0

Sample Output

4
2

Source

提示

题意:

打地鼠游戏,游戏台面是边长为n(1<=n<=20)的正方形,有m(1<=m<=1000)只地鼠会出现,地鼠出现的位置以二维坐标x,y(0<=x,y<n)来表示,它们出现的时间以t(1<=t<=20)来表示。开始时,你可以把锤子放在任何位置,后面你只能按直线移动且每秒最大移动距离为d(1<=d<=5)位置必须在整数点上,但也可以移出台面外,锤子经过的路线都能打中地鼠,求出最大能打中多少地鼠。

思路:

我们设dp[i][j][k]为锤子在第k秒时坐标(i,j)所能获得的最大收益,由dp[i'][j'][k-1]+num来递推,num表示从(i',j')位置移动过来所打到的地鼠数量。这点不难理解,关键是如何求出数量,因为题目的特殊性锤子停下的位子为整点数且保证在题目允许的条件下可以任意移动,所以我们可以与2d*2d的矩阵中枚举其中的点组成线段,之后进行延伸看看是否还有满足题目条件且在线段上的点,对其进行dp。对于锤子可以任意移动我们必须要将所有点坐标的x,y加上d,游戏台面扩大为(n+2*d)^2,因为会出现锤子移动到台面边缘递推下去的值,要比锤子移动到台面外递推下去的值还要小的情况,平移坐标以后方便我们进行计算。

需要注意判断是否满足不超过最大移动距离可以用两点间距离平方和d^2进行比较,这样就避免了由于开方产生的精度误差。若当前秒没有地鼠时仍然要进行dp,因为这一秒就相当于可以多移动一次,会影响最终结果。

示例程序

Source Code

Problem: 3034		Code Length: 1551B
Memory: 460K		Time: 360MS
Language: GCC		Result: Accepted
#include <stdio.h>
#include <string.h>
int n,d,m,dp[30][30][11],map[30][30][11];
int max(int x,int y)
{
    if(x>y)
    {
        return x;
    }
    else
    {
        return y;
    }
}
int dis(int x,int y)
{
    return x*x+y*y;
}
int f(int x,int y,int t)
{
    int maxdp=0,i,num,dx,dy;
    for(dx=-d;d>=dx;dx++)				//在2d*2d的正方形范围内枚举
    {
        for(dy=-d;d>=dy;dy++)
        {
            if(dx==0&&dy==0)
            {
                continue;
            }
            num=0;
            i=0;
            while(x+i*dx>=0&&x+i*dx<n&&y+i*dy>=0&&y+i*dy<n&&dis(i*dx,i*dy)<=d*d)
            {
                if(map[x+i*dx][y+i*dy][t]==1)			//该位置出现地鼠+1
                {
                    num++;
                }
                maxdp=max(maxdp,dp[x+i*dx][y+i*dy][t-1]+num);//把这步放到里面是因为会出现从该点的后一秒所得的分数要比沿这条线走到底的点的分数多
                i++;
            }
        }
    }
    return maxdp;
}
int main()
{
    int i,i1,i2,x,y,t,ans,maxt;
    scanf("%d %d %d",&n,&d,&m);
    while(n!=0||d!=0||m!=0)
    {
        maxt=0;
        ans=0;
        memset(map,0,sizeof(map));
        memset(dp,0,sizeof(dp));
        for(i=1;m>=i;i++)
        {
            scanf("%d %d %d",&x,&y,&t);
            map[x+d][y+d][t]=1;				//坐标加大d个位置
            maxt=max(maxt,t);
        }
        n=n+2*d;				//边长扩大2*d个位置
        for(i=1;maxt>=i;i++)
        {
            for(i1=0;n>i1;i1++)
            {
                for(i2=0;n>i2;i2++)
                {
                    dp[i1][i2][i]=f(i1,i2,i);
                    ans=max(ans,dp[i1][i2][i]);
                }
            }
        }
        printf("%d\n",ans);
        scanf("%d %d %d",&n,&d,&m);
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值