[poj 3034] Whac-a-Mole  dp

Whac-a-Mole
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3623 Accepted: 1071

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 integers n, d 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 x, y and t 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 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
Nordic 2006

题目链接http://poj.org/problem?id=3034

题意:打地鼠游戏,n*n的矩阵,每一个整数点(x,y)都有一个鼠洞,某个老鼠在某个鼠洞出现的时间只有一秒。游戏者拿着锤子,锤子可以在任意一个整数点上(不一定在矩阵内)。游戏者每一秒可以将锤子从当前位置直线移动到下一位置(整数点)。两位置之间的距离不超过d。移动的直线路径穿过的(过整数点中心的)鼠洞如果有老鼠,都会被打晕。例如(0,0)移动到(0,3)。如果(0,1),(0,2)有老鼠出现就会被打晕。打晕一只老鼠得一分,求最大分数。

思路:dp[i][j][k]状态代表在第i时刻,到(j,k)点最大获利;参考了一下网上代码:http://www.w2bc.com/article/115472

代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<algorithm>
using namespace std;
int dp[12][31][31];
int vis[12][31][31];
int mt;
int tmp[31][31];
int ans;
int maxn;
int n,m,d;
int dix[4]={1,1,-1,-1};
int diy[4]={1,-1,1,-1};
void cal(int t,int x,int y)
{           
    int xx,yy;
        for(int i = 0; i <= d; ++i)
        for(int j = 0; j <= d; ++j)
        {
            if(!i && !j)
            {
                dp[t][x][y] += vis[t][x][y];
                tmp[x][y] = dp[t][x][y];
                dp[t+1][x][y] = max(dp[t+1][x][y],dp[t][x][y]);
                continue;
            }
            if(i*i+j*j > d*d) continue;
            for(int k = 0; k < 4; ++k)
            {               
                xx = x+i*dix[k];
                yy = y+j*diy[k];
                if(xx < 0 || xx >= n || yy < 0 || yy >= n) continue;
                int g = __gcd(abs(xx-x),abs(yy-y));
                tmp[xx][yy] = tmp[xx+(x-xx)/g][yy+(y-yy)/g]+vis[t][xx][yy];
                dp[t+1][xx][yy] =  max(dp[t+1][xx][yy],tmp[xx][yy]);
            }
        }

}
int main()
{
    while(scanf("%d%d%d",&n,&d,&m))
    {
        mt=0;
        ans=0;
        if(!n&&!d&&!m) return 0;
        n+=10;
        memset(dp,0,sizeof(dp));
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=m;i++)
        {
            int aa,bb,tt;
            scanf("%d%d%d",&aa,&bb,&tt);
            vis[tt][aa+5][bb+5]=1;
            mt=max(tt,mt);
        }
        for(int k=1;k<=mt;k++)
        for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        {
            cal(k,i,j);
        }   
        for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j)
        ans = max(ans,dp[mt][i][j]);        
        printf("%d\n",ans);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值