“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)F. Flower Road(十字链表)

链接: https://www.nowcoder.com/acm/contest/104/E
来源:牛客网

题目描述

(受限于评测机,此题数据范围与现场赛不一致,请谅解)

Once upon a time, there was a beautiful princess named TQM, and a handsome prince named GSS. One day, the prince would like to visit the princess. While in front of the princess' house, there was a flower-beds with blooming flowers. The prince was supposed to go through the flower-beds choosing the best ``Flower Road''.

Here is the task. The flower-beds is presented by a matrix with integers on each grid representing the princess' satisfaction of the flowers on it. Now, the prince was on the top left point (1, 1) and the princess was on the bottom right point (N, N). While the princess didn't want this to be so easy, she operated M times ``rotation'' on the flower-beds according to the order. Each time, she would choose a matrix whose top left point was . Then, four disjoint parts of the matrix whose length of size was rotated clockwise. Here is an example to make the ``rotation'' clearly.


Then, your task is to help the prince to choose a best ``Flower Road'' after these operations with the largest sum of the satisfaction. By the way, the prince will take the shortest way, which means he will only go down (from point (x, y) to point (x+1, y)) and right (from point (x, y) to point (x, y+1)).

输入描述:

The first line of input contains two integers, N () and M (), indicating the numbers N and M described above. Then N lines follow, and each line N integers, representing the matrix. Then M lines follow, each line has three integers , where xi and yi are coordinates of the top right point of i-th rotation matrix, th - side length of the matrix.

输出描述:

Output the max sum of the satisfaction.

#include<bits/stdc++.h>
using namespace std;
const int maxn=1555;
const int Maxn=3250005;
int a[maxn][maxn],n,m,t[maxn][maxn];
int L[Maxn],R[Maxn],U[Maxn],D[Maxn];
int d[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
int c[maxn][maxn];
long long dp[maxn][maxn];
int main()
{
    scanf("%d%d",&n,&m);
    int k=0;
    memset(t,-1,sizeof(t));
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            scanf("%d",&a[i][j]);
            t[i][j]=k++;
        }
    }
    int s;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            int id=t[i][j];
            U[id]=t[i-1][j];
            D[id]=t[i+1][j];
            L[id]=t[i][j-1];
            R[id]=t[i][j+1];
            if(U[id]==-1&&L[id]==-1)
                s=id;
        }
    }
    while(m--)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        int s1=s;
        for(int i=1; i<y; i++)
            s1=R[s1];
        for(int i=1; i<x; i++)
            s1=D[s1];
        int s2=s1;
        for(int i=1; i<=z; i++)
            s2=R[s2];
        for(int i=1; i<=z; i++)
            s2=D[s2];
        int s3=s1;
        for(int i=1; i<=z; i++)
            s3=D[s3];
        int s4=s1;
        for(int i=1; i<=z; i++)
            s4=R[s4];
        int s5=s4;
        for(int i=1; i<z; i++)
            s5=R[s5];
        int s6=s5;
        for(int i=1; i<=z; i++)
            s6=D[s6];
        int s7=s3;
        for(int i=1; i<z; i++)
            s7=D[s7];
        int s8=s2;
        for(int i=1; i<z; i++)
            s8=D[s8];
        int s9=s6;
        for(int i=1; i<z; i++)
            s9=D[s9];
        int q1[1505],q2[1505];
        int t1=L[s2],t2=L[s4],t3=U[s3],t4=U[s2];
        int x1,x2;
        x1=s8;
        for(int i=1; i<=z; i++)
            q1[i]=D[x1],x1=R[x1];
        x1=s6;
        for(int i=1; i<=z; i++)
            q2[i]=R[x1],x1=D[x1];
        x1=s2,x2=s7;
        for(int i=1; i<=z; i++)
        {
            U[x1]=x2;
            x1=R[x1];
            x2=R[x2];
        }
        x1=s2,x2=s3;
        for(int i=1; i<=z; i++)
        {
            L[x1]=L[x2];
            if(L[x1]!=-1)
                R[L[x1]]=x1;
            x1=D[x1];
            x2=D[x2];
        }
        x1=s8,x2=s7;
        for(int i=1; i<=z; i++)
        {
            D[x1]=D[x2];
            if(D[x1]!=-1)
                U[D[x1]]=x1;
            x1=R[x1];
            x2=R[x2];
        }
        x1=s6;
        x2=s4;
        for(int i=1; i<=z; i++)
        {
            R[x1]=x2;
            x1=D[x1];
            x2=D[x2];
        }
        x1=s3,x2=s1;
        for(int i=1; i<=z; i++)
        {
            U[x1]=U[x2];
            if(U[x1]!=-1)
                D[U[x1]]=x1;
            x1=R[x1];
            x2=R[x2];
        }
        x1=s3,x2=s1;
        for(int i=1; i<=z; i++)
        {
            L[x1]=L[x2];
            if(L[x1]!=-1)
                R[L[x1]]=x1;
            if(L[x1]==-1&&U[x1]==-1)
                s=x1;
            x1=D[x1];
            x2=D[x2];
        }
        x1=s7,x2=s2;
        for(int i=1; i<=z; i++)
        {
            D[x1]=x2;
            x1=R[x1];
            x2=R[x2];
        }
        x1=t1;
        x2=s1;
        for(int i=1; i<=z; i++)
        {
            R[x1]=x2;
            x1=D[x1];
            x2=D[x2];
        }
        x1=s1,x2=s4;
        for(int i=1; i<=z; i++)
        {
            U[x1]=U[x2];
            if(U[x1]!=-1)
                D[U[x1]]=x1;
            x1=R[x1];
            x2=R[x2];
        }
        x1=s1,x2=t1;
        for(int i=1; i<=z; i++)
        {
            L[x1]=x2;
            x1=D[x1];
            x2=D[x2];
        }
        x1=t2,x2=s5;
        for(int i=1; i<=z; i++)
        {
            R[x1]=R[x2];
            if(R[x1]!=-1)
                L[R[x1]]=x1;
            x1=D[x1];
            x2=D[x2];
        }
        x1=t3;
        x2=s4;
        for(int i=1; i<=z; i++)
        {
            D[x1]=x2;
            x1=R[x1];
            x2=R[x2];
        }
        x1=s4,x2=s6;
        for(int i=1; i<=z; i++)
        {
            L[x1]=x2;
            x1=D[x1];
            x2=D[x2];
        }
        x1=s4,x2=t3;
        for(int i=1; i<=z; i++)
        {
            U[x1]=x2;
            x1=R[x1];
            x2=R[x2];
        }
        x1=t4;
        for(int i=1; i<=z; i++)
        {
            D[x1]=q1[i];
            if(D[x1]!=-1)
                U[D[x1]]=x1;
            x1=R[x1];
        }
        x1=s5;
        for(int i=1; i<=z; i++)
        {
            R[x1]=q2[i];
            if(R[x1]!=-1)
                L[R[x1]]=x1;
            x1=D[x1];
        }     
    }
    for(int i=1;i<=n;i++)
    {
        int s0=s;
        for(int j=1;j<=n;j++)
        {
            c[i][j]=a[s0/n+1][s0%n+1];
            s0=R[s0];
        }
        s=D[s];
    }
    memset(dp,0,sizeof(dp));
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            dp[i][j]=max(dp[i-1][j],dp[i][j-1])+c[i][j];
        }
    }
    printf("%lld\n",dp[n][n]);
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值