Whac-a-Mole
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3070 Accepted: 922

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

Source

Nordic 2006

可以从任意位置开始,移动到距离小于等于d的整数点上,打掉在这条直线上的地鼠。。。
锤子可以移动到格子外面。。。。。坑爹啊。。。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int map[40][40][20],dp[40][40][20],n,m,d;

int ABS(int x)
{
    if(x<0) return -x;
    return x;
}

int GCD(int a,int b)
{
    if(a==0) return b;
    return GCD(b%a,a);
}

int getsum(int x1,int y1,int x2,int y2,int t)
{
    int dx=x2-x1,dy=y2-y1;
    if(dy*dy+dx*dx>d*d) return 0;
    int sum=0,gc;
    if(dx==0&&dy==0) return map[x1][y1][t];
    if(ABS(dx)>ABS(dy))
        gc=GCD(ABS(dy),ABS(dx));
    else
        gc=GCD(ABS(dx),ABS(dy));
    dx=dx/gc; dy=dy/gc;///GCD的妙用。。。。
    for(int i=0;i<=gc;i++)
        sum+=map[x1+i*dx][y1+i*dy][t];
    return sum;
}

int main()
{
    while(scanf("%d%d%d",&n,&d,&m)!=EOF&&(n||d||m))
    {
        memset(map,0,sizeof(map));
        memset(dp,0,sizeof(dp));
        int ans=0,mt=1;
        for(int i=1;i<=m;i++)
        {
            int x,y,t;
            scanf("%d%d%d",&x,&y,&t);
            map[x+d][y+d][t]++;
            mt=max(mt,t);
        }
        n=n+2*d;
        for(int t=1;t<=mt;t++)
        {
            for(int x=0;x<n;x++)
            {
                for(int y=0;y<n;y++)
                {
                    int sx=(x-d)>0?x-d:0;
                    int tx=(x+d)<n?x+d:n-1;
                    int sy=(y-d)>0?y-d:0;
                    int ty=(y+d)<0?y+d:n-1;
                    for(int i=sx;i<=tx;i++)
                    {
                        for(int j=sy;j<=ty;j++)
                        {
                            if(((i-x)*(i-x)+(j-y)*(j-y))-d*d>0) continue;
                            dp[x][y][t]=max(dp[x][y][t],getsum(i,j,x,y,t)+dp[j][t-1]);
                        }
                    }
                if(t==mt)
                    ans=max(ans,dp[x][y][t]);
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

POJ 3034 Whac-a-Mole的更多相关文章

  1. (中等) POJ 3034 Whac-a-Mole,DP。

    Description While visiting a traveling fun fair you suddenly have an urge to break the high score in ...

  2. POJ 3034 Whac-a-Mole(DP)

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

  3. POJ 3034 Whac-a-Mole(DP)

    题目链接 题意 : 在一个二维直角坐标系中,有n×n个洞,每个洞的坐标为(x,y), 0 ≤ x, y < n,给你一把锤子可以打到地鼠,最开始的时候,你可以把锤子放在任何地方,如果你上一秒在( ...

  4. poj 3034 动态规划

    思路:这是一道坑爹的动态规划,思路很容易想到,就是细节. 用dp[t][i][j],表示在第t时间,锤子停在(i,j)位置能获得的最大数量.那么只要找到一个点转移到(i,j)收益最大即可. #incl ...

  5. poj很好很有层次感(转)

    OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 30 ...

  6. POJ题目分类推荐 (很好很有层次感)

    著名题单,最初来源不详.直接来源:http://blog.csdn.net/a1dark/article/details/11714009 OJ上的一些水题(可用来练手和增加自信) (POJ 3299 ...

  7. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  8. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  9. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

随机推荐

  1. 分布式存储数据库的Key的随机分布(RP)和顺序分布(OPP)

    在分布式存储数据库的世界中,无论是基于Key/Value的数据库还是Column Base(比如HBase)的数据库,都有一个重要的因子------Key,或者叫RowKey.我们总是根据Key来快速 ...

  2. Sublime Text以及Package Control安装方法

    官方下载:Sublime Text 中国论坛:Sublime 论坛 Sublime Text 是一个代码编辑器,具有漂亮的用户界面和强大的功能,并且它还是一个跨平台的编辑器,同时支持Windows.L ...

  3. PHP-权限控制类

    http://blog.csdn.net/painsonline/article/details/7183679 <?php /** * 权限控制类 */ class include_purvi ...

  4. 实现BPEL4WS演示:教程

    http://www.ibm.com/developerworks/cn/education/webservices/ws-bpelws/bpel_tutorial_cn.html 开始 什么是Bus ...

  5. Linux下开发常用 模拟 Http get和post请求

    1.get请求 curl "http://www.baidu.com"      如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 curl -i "htt ...

  6. C# vs C++ Performance

    http://www.codeproject.com/Articles/212856/Head-to-head-benchmark-Csharp-vs-NET

  7. 关于MarshalByRefObject的解释

    http://www.cnblogs.com/webfpc/archive/2010/03/10/1667101.html 首先了解一下不同应用程序域中的对象的通信方式有两种: 一种是跨应用程序域边界 ...

  8. centos 7.0 安装vim

    用的最小化 安装 看看跟VI命令有何区别 后面会记录使用经验 [root@localhost conf]# yum -y install vim 已加载插件:fastestmirror base | ...

  9. Java并发编程核心方法与框架-CompletionService的使用

    接口CompletionService的功能是以异步的方式一边生产新的任务,一边处理已完成任务的结果,这样可以将执行任务与处理任务分离.使用submit()执行任务,使用take取得已完成的任务,并按 ...

  10. 修改ubuntu DNS的步骤/wget url报错: unable to resolve host address的解决方法

    wget url 报错:unable to resolve host address ‘url’,显然是无法解析主机地址,这就能看出是DNS解析的问题.解决办法就是配置可用的dns 一般是修改成为谷歌 ...