POJ 3034 DP

来源:互联网 发布:网络云台摄像机价格 编辑:程序博客网 时间:2024/05/05 02:40

Whac-a-Mole
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3590 Accepted: 1065
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

题意:

打地鼠,每个时刻地鼠可能出现在一些位置,你的锤子可能放在区域内也可能放在区域外。然后没一个时刻,你的锤子能从当前的位置沿着一条直线移动到另外一个位置(移动长度不能超过d),沿途如果有地鼠则地鼠被打倒。问最后最多能最多打到多少个地鼠。

题解:

定义dp[i][j][t]为t时刻锤子在(i,j)位置时能打到的最多地鼠。则dp[i][j][t]应该是从t-1所有的情况中推到t时最大的一个。注意是锤子是可以移动到区域外的。坐标系得扩大。

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <stack>#include <string>#include <set>#include <cmath>#include <map>#include <queue>#include <sstream>#include <vector>#include <iomanip>#define m0(a) memset(a,0,sizeof(a))#define mm(a) memset(a,0x3f,sizeof(a))#define m_1(a) memset(a,-1,sizeof(a))#define f(i,a,b) for(i = a;i<=b;i++)#define fi(i,a,b) for(i = a;i>=b;i--)#define lowbit(a) ((a)&(-a))#define FFR freopen("data.in","r",stdin)#define FFW freopen("data.out","w",stdout)#define INF 0x3f3f3f3ftypedef long long ll;typedef long double ld;const ld PI = acos(-1.0);using namespace std;#define SIZE (5)int G[23+2*SIZE][23 + 2*SIZE][15];int dp[23 + 2*SIZE][23 +2* SIZE][15];int gcd(int a, int b) {    if (!b) {        return a;    }    else {        return gcd(b, a%b);    }}int cnt(int fx, int fy, int tx, int ty,int t) {    int dx = tx - fx;    int dy = ty - fy;    if (dx == 0 && dy == 0) {        if (G[tx][ty][t] == 1)            return 1;        else            return 0;    }    if (dx == 0) {        if(dy>0)            dy = 1;        if (dy < 0)            dy = -1;    }    else if (dy == 0) {        if(dx>0)            dx = 1;        if (dx < 0)            dx = -1;    }    else {        int k = gcd(abs(dx), abs(dy));        dx /= k; dy /= k;    }    int sum = 0;    while (fx!=tx||fy!=ty)    {        if(G[fx][fy][t]==1)            sum++;        fx += dx;        fy += dy;    }    if (G[fx][fy][t] == 1)        sum++;    return sum;}int main(){    //FFW;    //ios_base::sync_with_stdio(false); cin.tie(0);    int n, d, m;    while (scanf("%d%d%d",&n,&d,&m)&&(n||d||m))    {        m0(G);        int i, j, k, t;        int T = 0;        f(i, 1, m) {            int x, y;            scanf("%d%d%d", &x, &y, &t);            G[x+SIZE][y+SIZE][t] = 1;            T = max(T, t);        }        m0(dp);        f(t, 1, T) {            f(i,0,n + 2*SIZE)                f(j, 0, n + 2*SIZE) {                    int tx, ty;                        f(tx, 0, n + 2*SIZE)                            f(ty, 0, n + 2*SIZE) {                                if ((i - tx)*(i - tx) + (j - ty)*(j - ty) <= d*d) {                                    k = cnt(tx,ty,i,j,t);                                    dp[i][j][t] = max(dp[i][j][t], dp[tx][ty][t - 1] + k);                                }                            }                }        }        int ans = -INF;        f(i, 0, n + 2 * SIZE)            f(j, 0, n + 2 * SIZE)                ans = max(ans, dp[i][j][T]);        printf("%d\n", ans);    }    return 0;}
0 0
原创粉丝点击