【POJ 3034】 Whac-a-Mole(DP)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 3621 | Accepted: 1070 |
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
to any position
any position
any
好了,如果wa的话就去做吧……
附一组数据:
20 5 4 1 0 1 0 1 1 0 5 2 1 6 2(DISCUSS借的
PS:不光可以负向超出 正向也可以超过n!!!
处理方式就是把0~n移动到5~n+5
然后0~5表示-5~0 n+5~n+10表示正向爆出
代码如下:
#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <stack> #include <list> #include <algorithm> #include <map> #include <set> #define LL long long #define Pr pair<int,int> #define fread() freopen("in.in","r",stdin) #define fwrite() freopen("out.out","w",stdout) using namespace std; const int INF = 0x3f3f3f3f; const int msz = 10000; const int mod = 1e9+7; const double eps = 1e-8; int mp[12][33][33]; int dp[12][33][33]; int tmp[33][33]; int dirx[] = { 1, 1,-1,-1}; int diry[] = { 1,-1, 1,-1}; int d,n; int dis(int x1,int y1,int x2,int y2) { return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); } 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] += mp[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]); //printf("%d:%d,%d %d\n",t,x,y,tmp[x][y]); continue; } if(dis(x,y,x+i,y+j) > d*d) continue; for(int k = 0; k < 4; ++k) { if(!i && dirx[k] < 0) continue; xx = x+i*dirx[k]; if(!j && diry[k] < 0) continue; yy = y+j*diry[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]+mp[t][xx][yy]; dp[t+1][xx][yy] = max(dp[t+1][xx][yy],tmp[xx][yy]); //printf("%d,%d->%d,%d: %d->%d get:%d\n",x,y,xx,yy,tmp[xx+(x-xx)/g][yy+(y-yy)/g],dp[t+1][xx][yy],mp[t][xx][yy]); } } } int main() { //fread(); //fwrite(); int m,x,y,t; while(~scanf("%d%d%d",&n,&d,&m) && (n+d+m)) { memset(mp,0,sizeof(mp)); memset(dp,0,sizeof(dp)); int tim = 0; n += 10; while(m--) { scanf("%d%d%d",&x,&y,&t); mp[t][x+5][y+5] = 1; tim = max(tim,t); } for(int i = 1; i <= tim; ++i) for(int j = 0; j < n; ++j) for(int k = 0; k < n; ++k) cal(i,j,k); int mx = 0; for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) mx = max(mx,dp[tim+1][i][j]); printf("%d\n",mx); } return 0; }