Whac-a-Mole(动态规划)

Whac-a-Mole
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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






#include<cstdio>  
#include<cstring>  
#include<cstdlib>
#include<iostream>
//避免出现负数下标 构图时 n+d+d?? 
#define MAXN 40
using namespace std;  
struct Point
{
    int x,y;
}p[MAXN][1010];//记录每个时间点,有哪些老鼠

int lenth[MAXN];//每个时间点老鼠的个数 
int n,d,m;
int x,y,t,ans;
int dp[2][MAXN][MAXN];//因为当前的t与前几秒无关,故用滚动数组记录状态,节省空间 
int t_map[MAXN][MAXN];//当前时间点图上有的老鼠 

void init()
{
	freopen("D - Whac-a-Mole.in","r",stdin);
	freopen("D - Whac-a-Mole.out","w",stdout);
}

int gcd(int a,int b)
{
	if(a==0)return b;
	return gcd(b%a,a);
}

int find_mice(int x,int y,int tx,int ty)//计算从(x,y)到(tx,ty)能打到的老鼠 
{  
    int ret=0,dx,dy,tp;
    
    //因为只有整数点才有老鼠 
    //所以通过求两点距离的最大公因数可减少枚举的次数
    
    
    
    tp=gcd(abs(tx-x),abs(ty-y));
    
    dx=(tx-x)/tp;
    dy=(ty-y)/tp; 
    
    //记录个数
    for(int i=x,j=y;i!=tx||j!=ty;i+=dx,j+=dy)
        ret+=t_map[i][j];
    
	/*
	//不能直接枚举
	///导致无法退出或溢出 
	for(int i=x,j=y;i!=tx||j!=ty;i++,j++)
		ret+=t_map[i][j]; 
	*/
	
    ret+=t_map[tx][ty];

    return ret;
}
 

void readdate()
{
    memset(lenth,0,sizeof(lenth));
    for(int i=1;i<=m;i++)//读入老鼠 
	{
        scanf("%d%d%d",&x,&y,&t);
        p[t][lenth[t]].x=x+d;
        p[t][lenth[t]].y=y+d;
        lenth[t]++; 
	}
	
	n+=d+1;//WA——??? 
	 //n+=d*2;//AC——??? 
	
    memset(dp,0,sizeof(dp));
    
    for(int i=0;i<lenth[0];i++)
        dp[0][p[0][i].x][p[0][i].y]++;
}



void work()
{
	for(int t=1;t<=10;t++)
	{
        for(int i=0;i<lenth[t];i++)
            t_map[p[t][i].x][p[t][i].y]++;//初始当前地图上的老鼠 
        
        for(int i=0;i<n;i++)
		{
            for(int j=0;j<n;j++)
			{
                dp[t&1][i][j]=dp[!(t&1)][i][j]+t_map[i][j];//由上一个状态更新到下一秒的初状态
				
                for(int dx=-d;dx<=d;dx++)
				{
                    for(int dy=-d;dy<=d;dy++)
					{
                        if(dx*dx+dy*dy>d*d || !dx && !dy) continue;
                        
                        x=i+dx;
						y=j+dy;
						
                        if(x<0 || x>=n || y<0 || y>=n) continue;
                        
                        dp[t&1][i][j]=max(dp[t&1][i][j],dp[!(t&1)][x][y]+find_mice(i,j,x,y));//更新状态 
                    }
                }
                
                
            }
        }

        for(int i=0;i<lenth[t];i++)
            t_map[p[t][i].x][p[t][i].y]--;//清除当前地图上的老鼠,时间增加,老鼠消失 
            
	}
}
int solve()
{
	int ans=0;
    for(int i=0;i<n;i++)
	{
        for(int j=0;j<n;j++)
        
            if(dp[0][i][j]>ans)  
				 ans=dp[0][i][j];
    }
    return ans;
}
int main()  
{
	//init();
    while(scanf("%d%d%d",&n,&d,&m))
	{
		if(n==0&&m==0&&d==0)break;
		
		readdate();
		
		work();
		
		ans=solve();
		
        printf("%d\n",ans);
    }  
    return 0;  
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值