【洛谷P1004 方格取数】dp

P1004
题意 两个人从 1,1 点 走到 n ,n 点
只能向右和向下 每个点都有权值
问这两个人最多能收集到多少权值
权值被收集清零
做法
因为我们知道一个人要从1,1走到n,n 一共需要2*n步
所以我们定义 dp[k][i][j] 代表当前走了 k步 第一个人往下走 i 步 第二个人向下走 j 步 这样我们就分别知道他向右走了多少步

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<#define dbg3(x1,x2,x3) cout<<#x1<<" = "<#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define lc (rt<<1)
#define rc (rt<<11)
#define mid ((l+r)>>1)

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
int mp[11][11];
int dp[20][11][11];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,x,y,z;scanf("%d",&n);
    while(scanf("%d%d%d",&x,&y,&z))
    {
        if(!(x+y+z)) break;
        mp[x][y] = z;
    }
    for(int k = 1;k<=2*n;++k)
    {
        for(int i = 1;i<=min(k,n);++i)
        {
            for(int j = 1;j<=min(k,n);++j)
            {
                int tmp = k - i,tmp_ = k - j;
                dp[k][i][j] = max(max(dp[k-1][i-1][j],dp[k-1][i][j]),max(dp[k-1][i-1][j-1],dp[k-1][i][j-1]));
                dp[k][i][j] += mp[i][tmp];
                if(i!=j) dp[k][i][j] += mp[j][tmp_];
            }
        }
    }
    printf("%d\n",dp[2*n][n][n]);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

你可能感兴趣的:(ACM,DP)