HDU

来源:互联网 发布:三维设计软件 编辑:程序博客网 时间:2024/06/15 20:34

Jack's struggle


Problem Description
A team of airborne troops are ready to complete some missions.
The battlefield was divided into a grid of n*n, this team can be air-dropped at any place on time 0. In every time unit after landing, they can go to the grid left, right, up or down to the current grid, or they can just stay.
On their mission list, each mission is described as three integers: t, r and c, represents a task that must be completed exactly at time t on the grid (r, c).
Obviously, with limits of time, not all missions can be done.
The captain, Jack, struggling making decisions, wants to know how many missions they can complete at most.
 

Input
The input contains serveral cases:

For each case:

* The first line contains two integers n and m, 1<=n<=1000, 1<=m<=10000, n represents the size of the battlefield and m represents the number of missions on the list.

* Following m lines, each one describes a mission using three integers, t, r and c.


No two missions have the same t, r and c.

The input is terminated by n=m=0.
 

Output
One integer in one line represents the maximum number of mission that can be completed.
 

Sample Input
2 21 1 12 2 20 0
 

Sample Output
1
 


题意:给你一堆任务,和一个n*n大小的棋盘,每个任务规定在特定时间完成,然后每分钟可以走一格,要你求最多能完成多少个任务。


解题思路:终于自己想到怎么做了……稍微分析就会发现其实是最长上升子序列,只是判断条件改一下,改成走的路程要小于时间。在这之前要先按时间排序。


#include<iostream>#include<memory.h>#include<string>#include<algorithm>using namespace std;const int MAXN=10005;int N,M;int dp[MAXN];struct mission{    int t;    int x;    int y;    mission(int a=0,int b=0,int c=0){        t=a;        x=b;        y=c;    }}list[MAXN];bool cmp(mission a,mission b){    return a.t<b.t;}bool judge(mission a,mission b){    int len=abs(b.x-a.x)+abs(b.y-a.y);    if(len>abs(b.t-a.t))        return false;    return true;}int main(){            while(~scanf("%d%d",&N,&M)){        if(N==0&&M==0)            break;                for(int i=0;i<M;i++)            scanf("%d%d%d",&list[i].t,&list[i].x,&list[i].y);                memset(dp,0,sizeof(dp));        sort(list,list+M,cmp);                        int ans=0;        for(int i=0;i<M;i++){            dp[i]=1;            for(int j=0;j<i;j++){                if(judge(list[j],list[i]))                    dp[i]=max(dp[i],dp[j]+1);            }            if(dp[i]>ans)ans=dp[i];                    }                        printf("%d\n",ans);    }            return 0;}







原创粉丝点击