NYOJ 61 傳紙條(一)

來源:互聯網
上載者:User

標籤:color   一個   ring   class   座標   std   for   span   ret   

雙線DP

#include<iostream>#include<algorithm>#include<ctype.h>#include<string>#include<string.h>#include<vector>#include<queue> using namespace std;int dp[110][55][55];    //壓縮空間   dp[x1+y1][x1][x2];                         //已知x1+y1 、x1和x2的值 ,結合x1 + y1 = x2 + y2 可以確定x1,y1,x2,y2四個值 int mp[55][55];int max(int a,int b,int c,int d){    if(a>=b && a>=c && a>=d)    return a;    else if(b>=a && b>=c && b>=d)    return b;    else if(c>=a && c>=b && c>=d)    return c;    else if(d>=a && d>=c && d>=b)    return d;}int main(){    int t;    cin>>t;    while(t--)    {        int m,n;        cin>>m>>n;        for(int i=1;i<=m;i++)        {            for(int j=1;j<=n;j++)            {                cin>>mp[i][j];            }        }        int k,x1,y1,x2,y2;        for(k=0;k<=m+n;k++)            for(x1=0;x1<=m;x1++)                for(x2=0;x2<=m;x2++)                    dp[k][x1][x2] = 0;        for(k=2;k<=m+n;k++)        {            for(x1=1;x1<=m;x1++)        //(x,y)看作 其中一個座標,結合k可求出對應的另一個點 ,遍曆k和所有的(x,y)  就可以得到dp[k][x][y]的所有值             {                for(x2=1;x2<=m;x2++)                {                    y1 = k - x1;                    y2 = k - x2;                                                    if(  y1<=n && y2<=n && y1>=1 && y2>=1)    //不越界                     {                        if(x1 != x2 && y1 != y2)        //不相交                         {                            int v1 = dp[k-1][x1-1][x2-1] + mp[x1][y1] + mp[x2][y2];                            int v2 = dp[k-1][x1][x2] + mp[x1][y1] + mp[x2][y2];                            int v3 = dp[k-1][x1-1][x2] + mp[x1][y1] + mp[x2][y2];                            int v4 = dp[k-1][x1][x2-1] + mp[x1][y1] + mp[x2][y2];                            dp[k][x1][x2] = max(v1,v2,v3,v4);                        }                    }                }                }            }                cout<<dp[m+n-1][m][m-1]<<endl;        }        return 0;} 

 

NYOJ 61 傳紙條(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.