poj 3034 Whac-a-Mole(dp)

来源:互联网 发布:淘宝 修改差评 编辑:程序博客网 时间:2024/05/18 18:14
Whac-a-Mole
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 2897 Accepted: 886

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 (x,y) satisfying 0 ≤x,y <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 (x2, y2) that is at distance at most d from your current position (x1,y1). 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 (x1,y1) and (x2,y2) (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 integersn,d andm, wheren and d are as described above, andm is the total number of moles that will appear (1 ≤n ≤ 20, 1 ≤d ≤ 5, and 1 ≤m ≤ 1000). Then followm lines, each containing three integersx,y andt giving the position and time of the appearance of a mole (0 ≤x,y <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 60 0 13 1 30 1 20 2 21 0 22 0 25 4 30 0 11 2 12 4 10 0 0

Sample Output

42
题意:有一个二维坐标系,有m个鼬鼠,给出每个鼬鼠出现的坐标和出现的时间。人用锤子打鼬鼠,每打中一个得一分。开始时锤子的位置任意的,每个时间锤子可以打到半径范围在d内的所有鼬鼠,问结束时最多能得多少分数。
思路:DP。注意锤子的位置可以超出所给出的坐标系,我们可以扩展坐标系为n+=2*d。设dp[x][y][t]表示t时刻锤子在坐标(x,y)时已经得到的最高分数。状态转移方程:
dp[x][y][t]=max(dp[x][y][t],dp[i][j][t-1]+sum(点(i,j)与点(x,y)所连成的线段中整数点的出现个数)),其中i,j都在半径为d的圆的范围内
AC代码:
#include <cstring>#include <string>#include <cstdio>#include <algorithm>#include <queue>#include <cmath>#include <vector>#include <cstdlib>#include <iostream>using namespace std;int n,m,d;int dp[35][35][15];int map[35][35][15];int gcd(int a,int b){    return b==0?a:gcd(b,a%b);}int get_sum(int x1,int y1,int x2,int y2,int t){    int dx=x2-x1,dy=y2-y1;    if(dx==0&&dy==0)        return map[x2][y2][t];    int sum=0;    if(dx==0)        //两点的横坐标相等    {        if(y1>y2) swap(y1,y2);        for(int i=y1; i<=y2; i++)            sum+=map[x2][i][t];        return sum;    }    if(dy==0)       //两点的纵坐标相等    {        if(x1>x2) swap(x1,x2);        for(int i=x1; i<=x2; i++)            sum+=map[i][y2][t];        return sum;    }    int g=gcd(abs(dx),abs(dy));  //求最大公约数    dx/=g;                       //约分    dy/=g;    for(int i=0; i<=g; i++)       //求得同一直线上坐标为整数的点        sum+=map[x1+i*dx][y1+i*dy][t];    return sum;}int main(){    int x,y,t,max_t;    while(scanf("%d%d%d",&n,&d,&m),n||d||m)    {        memset(dp,0,sizeof(dp));        memset(map,0,sizeof(map));        max_t=0;        for(int i=0; i<m; i++)        {            scanf("%d%d%d",&x,&y,&t);            max_t=max(max_t,t);            map[x+d][y+d][t]=1;        }        n+=2*d;        int ans=0;        for(t=1; t<=max_t; t++)     //枚举每个坐标以及每个时间            for(x=0; x<n; x++)                for(y=0; y<n; y++)                {                    int sx=x-d<0?0:x-d;                    int sy=y-d<0?0:y-d;                    int ex=x+d>=n?n-1:x+d;                    int ey=y+d>=n?n-1:y+d;                    for(int i=sx; i<=ex; i++)                        for(int j=sy; j<=ey; j++)                            if((i-x)*(i-x)+(j-y)*(j-y)<=d*d)                            {                                dp[x][y][t]=max(dp[x][y][t],dp[i][j][t-1]+get_sum(i,j,x,y,t));                            }                    if(t==max_t) ans=max(ans,dp[x][y][t]);                }        printf("%d\n",ans);    }    return 0;}