T - Whac-a-Mole POJ - 3034 (动态规划)

本文探讨了在Whac-a-Mole游戏中,利用动态规划算法最大化得分的策略。通过预处理移动距离信息,优化状态转移方程,实现对游戏出现模式的精确响应,旨在帮助玩家在有限时间内达到最高分数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

T - Whac-a-Mole

 POJ - 3034 

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 < nin 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 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

题意:第1秒前,锤子可以放在任何地方,假设为pos(x,y),那么第一秒锤子移到距离pos(x,y)不远于d的位置(相当于半径),假设移到位置pos(x1,y1),那么线段pos(x,y)--pos(x1,y1)上所有第一秒出现的老鼠都可以被击中,击中一个老鼠加1分,同样的操作,到最后一秒,问最后可以得到的最大分值是多少

思路:用动态规划思想,我们定义dp[i][j][k]为第k秒前,第k-1秒末到达位置pos(i,j)所得的最大分值,从第1秒开始到最后一秒,枚举第一秒可以从每一个位置出发的情况,可以得到一个状态转移方程

dp[i][j][k]=min(dp[i][j][k],dp[i][j][k-1]+ans),其中ans为第k秒中砸中的老鼠数(即得分)

这里有一个地方是要优化的,即每次的移动距离不是1个单位1个单位的加,然后进行判断,而是可以根据三角形的相似关系进行大距离加上;

举个例子,假设从位置pos(x1,y1)到位置pos(x2,y2),两点的距离为ds,rx=x2-x1=6,ry=y2-y1=8,那么如果有一个点pos(x0,y0)在这两点的连线上,由于这点的坐标是整数,那么只有符合(x0-x1)/(y0-x1)=(x2-x1)/(y2-y1) 的条件才行(三角形相似关系),转一下思路,即只要求出rx,ry的最大公约数gcd,移动跨度rx=rx/gcd,ry=ry/gcd,然后从pos(x1,y1)出发,主要x1+rx<=x2且x2+ry<=y2,那么此次移动所有在直线数的都不多不少的遍历到,对于上面的例子,只有pos(x1,y1),pos(x1+3,y1+4),pos(x1+6,y1+8)在该线段上
 

#include<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=15;
const int nmax=44;
const double esp = 1e-9;
const double PI=3.1415926;
int dp[nmax][nmax][N],has[nmax][nmax][N];
struct node
{
    int x,y,ds;
} dir[nmax*nmax];
int n,m,d,sum;
bool cmp(node n1,node n2)
{
    return n1.ds<n2.ds;
}
void init()
{
    //预处理移动的距离信息
    sum=0;
    memset(dir,0,sizeof(dir));
    for(int x1=-5; x1<=5; x1++)
        for(int y1=-5; y1<=5; y1++)
        {
            if(x1*x1+y1*y1<=25)
            {
                dir[sum].x=x1;
                dir[sum].y=y1;
                dir[sum++].ds=x1*x1+y1*y1;
            }
        }
    sort(dir,dir+sum,cmp);  
}
int gcd(int i,int j)  //最大公约数
{
    return j==0?i:gcd(j,i%j);
}
int solve(int x1,int y1,int x2,int y2,int dx,int dy,int tt)
{
    int ans=0;
    while(1)
    {
        ans+=has[x1][y1][tt];  
        if(x1==x2&&y1==y2)
            break;
        x1+=dx;
        y1+=dy;
    }
    return ans;
}
int main()
{
    init();
    while(scanf("%d%d%d",&n,&d,&m)!=EOF)
    {
        if(n==0&&d==0&&m==0)
            break;
        memset(dp,0,sizeof(dp));
        memset(has,0,sizeof(has));
        int xx,yy,t;
        int tmax=0;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&xx,&yy,&t);
            has[xx+d][yy+d][t]=1;
            tmax=max(tmax,t);
        }
        int ans=0;
        tmax++;
        /***************核心代码**********************************/
        for(int k=1; k<tmax; k++)
        {
            for(int i=0; i<n+2*d; i++)
            {
                for(int j=0; j<n+2*d; j++)
                {
                    for(int ad=0; ad<sum&&d*d>=dir[ad].ds; ad++)
                    {   //遍历经过的点
                        int dx=i+dir[ad].x;
                        int dy=j+dir[ad].y;
                        if(dx<0||dx>=n+2*d||dy<0||dy>=n+2*d)
                            continue; 
                        if(ad==0) //移动距离为0的情况下
                            ans=has[dx][dy][k];
                        else
                        {
                            int r=gcd(abs(dir[ad].x),abs(dir[ad].y));
                            int a=dir[ad].x/r;
                            int b=dir[ad].y/r;
                            ans=solve(i,j,dx,dy,a,b,k);
                        }
                        dp[dx][dy][k+1]=max(dp[dx][dy][k+1],dp[i][j][k]+ans);
                    }
                }
            }
        }
        /***************核心代码**********************************/
        int maxl=0;
        for(int i=d; i<d+n; i++)
            for(int j=d; j<d+n; j++)
                maxl=max(maxl,dp[i][j][tmax]);
        printf("%d\n",maxl);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值