POJ 3034 Whac-a-Mole(DP)

题目链接 

理解了题意之后,这个题感觉状态转移还是挺好想的,实现起来确实有点繁琐,代码能力还有待加强,经过很长时间才发现bug。注意坐标可能是负的。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <cmath>
 5 #include <algorithm>
 6 using namespace std;
 7 int dp[12][40][41];
 8 bool o[12][41][41];
 9 int n,ans,d;
10 int gcd(int a,int b)
11 {
12     return b == 0?a:gcd(b,a%b);
13 }
14 int fun(int t,int x,int y,int tx,int ty,int ax,int ay)
15 {
16     int temp = 0;
17     while(x != ax||y != ay)//开始这里写成&&
18     {
19         if(o[t+1][x][y])
20         temp ++;
21         x += tx;
22         y += ty;
23     }
24     return temp+o[t+1][x][y];
25 }
26 void judge(int t,int x,int y)
27 {
28     int i,j,g;
29     for(i = x-d;i <= x+d;i ++)
30     {
31         for(j = y-d;j <= y+d;j ++)
32         {
33            if((i-x)*(i-x)+(j-y)*(j-y) > d*d) continue;
34            if(i < 0||j < 0) continue;
35            if(i > n+10||j > n+10) continue;
36            g = gcd(abs(i-x),abs(j-y));
37            if(g == 0) g = 1;
38            int tx,ty;
39            tx = (i-x)/g;
40            ty = (j-y)/g;
41            dp[t+1][i][j] = max(dp[t+1][i][j],dp[t][x][y]+fun(t,x,y,tx,ty,i,j));
42            ans = max(dp[t+1][i][j],ans);
43         }
44     }
45 }
46 int main()
47 {
48     int m,i,j,k,y,x,t;
49     while(scanf("%d%d%d",&n,&d,&m)!=EOF)
50     {
51         if(n == 0&&d == 0&&m == 0) break;
52         memset(dp,0,sizeof(dp));
53         memset(o,0,sizeof(o));
54         for(i = 1; i <= m; i ++)
55         {
56             scanf("%d%d%d",&x,&y,&t);
57             o[t][x+5][y+5] = 1;
58         }
59         ans = 0;
60         for(i = 0; i <= 10; i ++)
61         {
62             for(j = 0; j < n+10; j ++)
63             {
64                 for(k = 0; k < n+10; k ++)
65                 {
66                     judge(i,j,k);
67                 }
68             }
69         }
70         printf("%d\n",ans);
71     }
72     return 0;
73 }

 

posted @ 2013-07-13 20:41  Naix_x  阅读(241)  评论(0编辑  收藏  举报