程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 3034 Whac-a-Mole

POJ 3034 Whac-a-Mole

編輯:C++入門知識

Whac-a-Mole
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 2993   Accepted: 904

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 integersn, d and m, where n and d are as described above, andm 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 integersx, 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 0Sample Output

4
2Source

Nordic 2006
這題我實際采用的動歸方程是dp[x1][y1][x2][y2][t] = max(dp[x3][y3][x1][y1][t2] t2<t)+(x1,y1)與(x2,y2)之間的時間為t的個數;dp前面兩個維度沒有用,就去掉了,根據後面看只需保留他的最大值,就形成了dp[x2][y2][t]=max(dp[x1][y1][t2] t2<t)+(x1,y1)與(x2,y2)之間時間為t的個數;
注意 他能走負坐標。
在tle和wa之間不斷的游蕩,最後終於卡過了。

#include <iostream>   
#include <algorithm>   
#include <cstring>   
#include <cstdio>   
#include <cmath>   
#define NUM 1100   
#define N 33   
#define M 11   
using namespace std;  
int dp[N][N][M],Max[N][N][M];  
int sum[N][N][N][N][M];  
struct num  
{  
    int x,y,t;  
}a[NUM];  
bool cmp(num p1,num p2)  
{  
    return p1.x<p2.x;  
}  
int main()  
{  
   // freopen("data.in","r",stdin);   
    int n,d,m;  
    while(scanf("%d %d %d",&n,&d,&m)!=EOF)  
    {  
        if(!n&&!m&&!d)  
        {  
            break;  
        }  
        for(int i=1;i<=m;i++)  
        {  
            scanf("%d %d %d",&a[i].x,&a[i].y,&a[i].t);  
            a[i].x+=5;  
            a[i].y+=5;  
        }  
        n=n+7;  
        sort(a+1,a+m+1,cmp);  
        for(int x1=0;x1<=n-1;x1++)  
        {  
            for(int y1=0;y1<=n-1;y1++)  
            {  
                for(int x2=0;x2<=n-1;x2++)  
                {  
                    for(int y2=0;y2<=n-1;y2++)  
                    {  
                        for(int t=1;t<=10;t++)  
                        {  
                            sum[x1][y1][x2][y2][t]=0;  
                        }  
                    }  
                }  
            }  
        }  
        for(int x1 = 0;x1<=n-1;x1++)  
        {  
            for(int y1=0;y1<=n-1;y1++)  
            {  
                for(int x2=x1;x2<=n-1&&x2<=x1+d;x2++)  
                {  
                    int y2=0;  
                    if(x2==x1)  
                    {  
                        y2 = y1;  
                    }  
                    for(;y2<=n-1&&y2<=y1+d;y2++)  
                    {  
                        if(((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))>d*d)  
                        {  
                            continue;  
                        }  
                        for(int i=1;i<=m;i++)  
                        {  
                            int x = a[i].x;  
                            int y = a[i].y;  
                            int t = a[i].t;  
                            if(x>x2)  
                            {  
                                break;  
                            }  
                            if(x1==x2&&y1==y2&&x==x1&&y==y1)  
                            {  
                                sum[x1][y1][x2][y2][t]++;  
                                continue;  
                            }else if(x1==x2&&y1==y2)  
                            {  
                                continue;  
                            }  
                            if(x==x1&&x==x2&&y>=y1&&y<=y2)  
                            {  
                                sum[x1][y1][x2][y2][t]++;  
                                sum[x2][y2][x1][y1][t]++;  
                                continue;  
                            }  
                            double k = (double)(y2-y1)/(double)(x2-x1);  
                            double D = (double)(y2) - k *(double)(x2);  
                            if(fabs(k*(double)(x)+D-(double)(y))<=1e-7&&x>=x1&&x<=x2)  
                            {  
                                sum[x1][y1][x2][y2][t]++;  
                                sum[x2][y2][x1][y1][t]++;  
                            }  
                        }  
                    }  
                }  
            }  
        }  
        memset(dp,0,sizeof(dp));  
        memset(Max,0,sizeof(Max));  
        for(int t=1;t<=10;t++)  
        {  
            for(int x1 =0;x1<=n-1;x1++)  
            {  
                for(int y1=0;y1<=n-1;y1++)  
                {  
                    for(int x2=0;x2<=n-1;x2++)  
                    {  
                        for(int y2=0;y2<=n-1;y2++)  
                        {  
                            //dp[x1][y1][x2][y2][t];   
                            if(((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))>d*d)  
                            {  
                                continue;  
                            }  
                            int mmax=0;  
                            for(int kb=1;kb<=t-1;kb++)  
                            {  
                                mmax=max(mmax,dp[x1][y1][kb]);  
                            }  
                            Max[x2][y2][t]=max(Max[x2][y2][t],mmax+sum[x1][y1][x2][y2][t]);  
                        }  
                    }  
                }  
            }  
            for(int x2=0;x2<=n-1;x2++)  
            {  
                for(int y2=0;y2<=n-1;y2++)  
                {  
                   dp[x2][y2][t] = Max[x2][y2][t];  
                }  
            }  
        }  
        int res = 0;  
        for(int i=0;i<=n-1;i++)  
        {  
            for(int j=0;j<=n-1;j++)  
            {  
                for(int t=1;t<=10;t++)  
                {  
                    res = max(res,dp[i][j][t]);  
                }  
            }  
        }  
        printf("%d\n",res);  
    }  
    return 0;  
}  

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#define NUM 1100
#define N 33
#define M 11
using namespace std;
int dp[N][N][M],Max[N][N][M];
int sum[N][N][N][N][M];
struct num
{
    int x,y,t;
}a[NUM];
bool cmp(num p1,num p2)
{
    return p1.x<p2.x;
}
int main()
{
   // freopen("data.in","r",stdin);
    int n,d,m;
    while(scanf("%d %d %d",&n,&d,&m)!=EOF)
    {
        if(!n&&!m&&!d)
        {
            break;
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d %d",&a[i].x,&a[i].y,&a[i].t);
            a[i].x+=5;
            a[i].y+=5;
        }
        n=n+7;
        sort(a+1,a+m+1,cmp);
        for(int x1=0;x1<=n-1;x1++)
        {
            for(int y1=0;y1<=n-1;y1++)
            {
                for(int x2=0;x2<=n-1;x2++)
                {
                    for(int y2=0;y2<=n-1;y2++)
                    {
                        for(int t=1;t<=10;t++)
                        {
                            sum[x1][y1][x2][y2][t]=0;
                        }
                    }
                }
            }
        }
        for(int x1 = 0;x1<=n-1;x1++)
        {
            for(int y1=0;y1<=n-1;y1++)
            {
                for(int x2=x1;x2<=n-1&&x2<=x1+d;x2++)
                {
                    int y2=0;
                    if(x2==x1)
                    {
                        y2 = y1;
                    }
                    for(;y2<=n-1&&y2<=y1+d;y2++)
                    {
                        if(((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))>d*d)
                        {
                            continue;
                        }
                        for(int i=1;i<=m;i++)
                        {
                            int x = a[i].x;
                            int y = a[i].y;
                            int t = a[i].t;
                            if(x>x2)
                            {
                                break;
                            }
                            if(x1==x2&&y1==y2&&x==x1&&y==y1)
                            {
                                sum[x1][y1][x2][y2][t]++;
                                continue;
                            }else if(x1==x2&&y1==y2)
                            {
                                continue;
                            }
                            if(x==x1&&x==x2&&y>=y1&&y<=y2)
                            {
                                sum[x1][y1][x2][y2][t]++;
                                sum[x2][y2][x1][y1][t]++;
                                continue;
                            }
                            double k = (double)(y2-y1)/(double)(x2-x1);
                            double D = (double)(y2) - k *(double)(x2);
                            if(fabs(k*(double)(x)+D-(double)(y))<=1e-7&&x>=x1&&x<=x2)
                            {
                                sum[x1][y1][x2][y2][t]++;
                                sum[x2][y2][x1][y1][t]++;
                            }
                        }
                    }
                }
            }
        }
        memset(dp,0,sizeof(dp));
        memset(Max,0,sizeof(Max));
        for(int t=1;t<=10;t++)
        {
            for(int x1 =0;x1<=n-1;x1++)
            {
                for(int y1=0;y1<=n-1;y1++)
                {
                    for(int x2=0;x2<=n-1;x2++)
                    {
                        for(int y2=0;y2<=n-1;y2++)
                        {
                            //dp[x1][y1][x2][y2][t];
                            if(((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))>d*d)
                            {
                                continue;
                            }
                            int mmax=0;
                            for(int kb=1;kb<=t-1;kb++)
                            {
                                mmax=max(mmax,dp[x1][y1][kb]);
                            }
                            Max[x2][y2][t]=max(Max[x2][y2][t],mmax+sum[x1][y1][x2][y2][t]);
                        }
                    }
                }
            }
            for(int x2=0;x2<=n-1;x2++)
            {
                for(int y2=0;y2<=n-1;y2++)
                {
                   dp[x2][y2][t] = Max[x2][y2][t];
                }
            }
        }
        int res = 0;
        for(int i=0;i<=n-1;i++)
        {
            for(int j=0;j<=n-1;j++)
            {
                for(int t=1;t<=10;t++)
                {
                    res = max(res,dp[i][j][t]);
                }
            }
        }
        printf("%d\n",res);
    }
    return 0;
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved