POJ 3034 DP
2016-03-30 16:21
330 查看
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
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 0x3f3f3f3f typedef 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; }
相关文章推荐
- 详解Android应用中屏幕尺寸的获取及dp和px值的转换
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- POJ-1695-Magazine Delivery-dp
- nyoj-1216-整理图书-dp
- TYVJ1193 括号序列解题报告
- 对DP的一点感想
- TYVJ上一些DP的解题报告
- soj1005. Roll Playing Games
- 01背包问题
- LeetCode之Maximum Product Subarray