POJ3034--Whac-a-Mole

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
/*
n<=20
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define maxn 38
bool vis[maxn][maxn][28];//判断某时刻
int dp[maxn][maxn][28];
int first[8080],nxt[1000080],vv[1000080];
int e = 0;
void addEdge(int u,int v)
{
	vv[e] = v;	nxt[e] = first[u];	first[u] = e++;
	vv[e] = u;	nxt[e] = first[v];		first[v] = e++;
}
bool Judge(int r1,int c1,int r2,int c2,int d)
{
	return (d*d >= (r2-r1)*(r2-r1) + (c2-c1)*(c2-c1));
}
int gcd(int a,int b)
{
	if(b == 0)	return a;
	return gcd(b,a%b);
}
inline int max(int a,int b)
{
	return a>b?a:b;
}
int getpath(int r1,int c1,int r2,int c2,int t)
{
	int sum = 0;
	int dr = r2 - r1;
	int dc = c2 - c1;
	int ggcd,ddr=0,ddc=0;
	if(dr && dc)
	{
		ggcd = gcd(abs(dr),abs(dc));
		ddr = dr/ggcd,ddc = dc/ggcd;
	}
	else
	{
		if(dr == 0)	ddc = dc/abs(dc);
		if(dc == 0) ddr = dr/abs(dr);
	}
	int nr = r1,nc = c1;
	while(!(nr == r2 && nc == c2))
	{
		sum += vis[nr][nc][t];
		nr += ddr,nc += ddc;
	}
	return sum+vis[nr][nc][t];
}
int main()
{
	//freopen("in.txt","r",stdin);
	int n,d,m;
	while(scanf("%d%d%d",&n,&d,&m)==3 && (n||d||m))
	{
		int add = 2*d;
		e  = 0 ;
		int max_t = 0;
		memset(first,-1,sizeof(first));
		memset(vis,0,sizeof(vis));
		memset(dp,0,sizeof(dp));
		for(int r1 = 1;r1 <= n+add;r1++)
		{
			for(int c1 = 1;c1 <= n+add;c1++)
			{
				for(int r2 = r1;r2 <= n+add;r2++)
				{
					for(int c2 = r2==r1?c1+1:1;c2 <= n+add;c2++)
					{
						if(Judge(r1,c1,r2,c2,d))
						{
							addEdge(r1*108+c1,r2*108+c2);
						}
					}
				}
			}
		}
		for(int i = 1;i <= m;i++)
		{
			int r,c,t;
			scanf("%d%d%d",&r,&c,&t);
			max_t = max(max_t,t);
			r+=add/2+1;c+=add/2+1;
			vis[r][c][t] = 1;
		}
		int ans = 0;
		for(int i = 1;i <= max_t;i++)
		{
			for(int j = 1;j <= n+add;j++)
			{
				for(int k = 1;k <= n+add;k++)
				{
					dp[j][k][i] = dp[j][k][i-1]+vis[j][k][i];
					for(int ii = first[j*108+k];ii != -1;ii = nxt[ii])
					{
						int v = vv[ii];
						dp[j][k][i] = max(dp[j][k][i],getpath(v/108,v%108,j,k,i)+dp[v/108][v%108][i-1]);
					}
				}
			}
		}
		for(int i = 1;i <= n+add;i++)
		{
			for(int j = 1;j <= n+add;j++)
			{
				ans = max(ans,dp[i][j][max_t]);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}
	

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值