P2905 [USACO08OPEN] farm crisis Crisis on the Farm (dp + simple trouble "backtracking")

Practice, simplification meaning of the questions (see the length of the decision of difficulty)

There are two points a (tentatively called a and b), the coordinates of each, now all simultaneously at any point in a westward direction moving a unit, when a point coincident with the point b, the answer increase the number of overlapped on the lawn , the maximum seek answers and find this command sequence

solution&&thinking

First, dp no doubt (can not be greedy, then hack series. )

Second, the equation is not a good push giant

f [i] [j] [k] represents the step i, were gone east j, k North taking a total maximum value of four transfer, after most comparison value, plus the number of pre-treatment, most green difficulty.

So, the second question to ask is this the reason becomes blue.

Requirements sequences! Even the smallest lexicographically.

Yzy pressing the argument, so that each point move at the same time, seeking sequences, but does not seem feasible

After considering obtaining the optimal solution, optimal solution from the start dfs, minus the state, trying to restore it to the original state, forced to enumerate the sequence (also a dp233)

Since this is a dp, then you can iterate wrote? Haoyahaoya ....

After E (east) in order to enumerate enumeration direction, once a state before we can add the number to find out the pretreatment can reach the current state (in fact, this is the simulation dfs), then the output of this character.

Then ... then there is no

Pit:

Here it is necessary to criticize the topic and people simply have no conscience. Really is a mapping sentence zym Gangster

The intentions of the topic, foot-made data

That good is greater than zero it? Say good less than 1000 do?

Dog ate it?

But also anti-negative? ? ?

wtf.......

Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=5005;
const int T=31;
const int dx[4]={1,0,0,-1};
const int dy[4]={0,1,-1,0};
const int dd[4]={69,78,83,87};
int n,m,K;
int a[maxn],b[maxn],c[maxn],d[maxn];
int f[T<<1][T<<1][T<<1];
int g[T<<1][T<<1];
int main()
{
    scanf("%d%d%d",&n,&m,&K);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&a[i],&b[i]);
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&c[i],&d[i]);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(abs(c[j]-a[i])+abs(d[j]-b[i])<=K)
            g[c[j]-a[i]+T][d[j]-b[i]+T]++;
        }
    }
    for(int i=K;i>=0;i--)
    {
        for(int j=T-i;j<=T+i;j++)
        {
            for(int k=T-i;k<=T+i;k++)
            {
                for(int l=0;l<4;l++)
                {
                    f[i][j][k]=max(f[i+1][j+dx[l]][k+dy[l]],f[i][j][k]);
                }
                f[i][j][k]+=g[j][k];
            }
        }
    }
    printf("%d\n",f[0][T][T]);
    int u=T,v=T,j;
    for(int i=0;i<K;i++)
    {
        for(j=0;j<4;j++)
        if(f[i][u][v]==f[i+1][u+dx[j]][v+dy[j]]+g[u][v])
        break;
        u+=dx[j];
        v+=dy[j];
        printf("%c",dd[j]);    
    }
    return 0;    
}

(完)

Guess you like

Origin www.cnblogs.com/ajmddzp/p/11374716.html