来源:牛客网
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
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.
示例1
输入
4 1 1 2 5 6 3 4 7 8 13 14 9 10 15 16 11 12 1 1 2
输出
81
让你在xi,yi点,以li为边长进行2li*2li中四块的顺时针变换
变换m次后,输出1,1->n,n的最大和,每次只能向右或向下走
相当于
一、二 变 四、一
四、三 成 三、二
得到每个节点的上下左右,并且最开始初始化,这样可以得到左顶点的节点
每次通过它找到13个点,进行变化
以1,1,2做样例
s1 t1 s2 s6
dot2 dot4
s3 dot3 s4 s5
s7 s8 s9
变换第三块、第四块、第一块、第二块的上、左、下、右 不知道为什么顺序有关
最聪明的想法是:得到最后节点的实际存数(详情见putting函数)
极其感谢我的大巨轮!!!!!!!
爱你,么么哒!
@还是太年轻!
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string.h>
using namespace std;
const int maxn=3250000;
const int maxx=2500;
int n,m;
int x1,x2;
int D[maxn],U[maxn],R[maxn],L[maxn];
int finding[maxx][maxx];
int cc[maxx][maxx];
int store[maxx][maxx];
int dp[maxx][maxx];
int q1[maxn],q2[maxn];
int dot1,dot2,dot3,dot4;
int s,s1,s2,s3,s4,s5,s6,s7,s8,s9;
void init(int n)
{
memset(finding,-1,sizeof(finding));
int k=0;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
scanf("%d",&store[i][j]);
finding[i][j]=k++;
}
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
int id=finding[i][j];
U[id]=finding[i-1][j];
D[id]=finding[i+1][j];
L[id]=finding[i][j-1];
R[id]=finding[i][j+1];
if(U[id]==-1&&L[id]==-1)
s=id;
}
}
}
void right(int &x)
{
x=R[x];
}
void down(int &x)
{
x=D[x];
}
void left(int &x)
{
x=L[x];
}
void up(int &x)
{
x=U[x];
}
void looking(int x,int y,int h)
{
s1=s;
for(int i=1;i<y;i++)
{
right(s1);
}
for(int i=1;i<x;i++)
{
down(s1);
}
s2=s1;
for(int i=1;i<=h;i++)
{
right(s2);
}
s3=s1;
for(int i=1;i<=h;i++)
{
down(s3);
}
s4=s2;
for(int i=1;i<=h;i++)
{
down(s4);
}
s5=s4;
for(int i=1;i<h;i++)
{
right(s5);
}
s6=s2;
for(int i=1;i<h;i++)
{
right(s6);
}
s7=s3;
for(int i=1;i<h;i++)
{
down(s7);
}
s8=s4;
for(int i=1;i<h;i++)
{
down(s8);
}
s9=s5;
for(int i=1;i<h;i++)
{
down(s9);
}
dot1=L[s2];
dot2=U[s3];
dot3=L[s4];
dot4=U[s4];
x1=s8;
for(int i=1; i<=h; i++)
{
q1[i]=D[x1];
right(x1);
}
x1=s5;
for(int i=1; i<=h; i++)
{
q2[i]=R[x1];
down(x1);
}
}
void revering(int x,int y,int h)
{
//第三块
x1=s4;
x2=s7;
for(int i=1;i<=h;i++)
{
U[x1]=x2;
right(x1);
right(x2);
}
x1=s4;
x2=s3;
for(int i=1;i<=h;i++)
{
L[x1]=L[x2];
if(L[x1]!=-1)
R[L[x1]]=x1;
down(x1);
down(x2);
}
x1=s8;
x2=s7;
for(int i=1;i<=h;i++)
{
D[x1]=D[x2];
if(D[x1]!=-1)
U[D[x1]]=x1;
right(x1);
right(x2);
}
x1=s5;
x2=s2;
for(int i=1;i<=h;i++)
{
R[x1]=x2;
down(x1);
down(x2);
}
//第四块
x1=s3;
x2=s1;
for(int i=1;i<=h;i++)
{
U[x1]=U[x2];
if(U[x1]!=-1)
D[U[x1]]=x1;
right(x1);
right(x2);
}
x1=s3;
x2=s1;
for(int i=1;i<=h;i++)
{
L[x1]=L[x2];
if(L[x1]!=-1)
R[L[x1]]=x1;
if(L[x1]==-1&&U[x1]==-1)
s=x1;
down(x1);
down(x2);
}
x1=s7;
x2=s4;
for(int i=1;i<=h;i++)
{
D[x1]=x2;
right(x1);
right(x2);
}
x1=dot3;
x2=s1;
for(int i=1;i<=h;i++)
{
R[x1]=x2;
down(x1);
down(x2);
}
//第一块
x1=s1;
x2=s2;
for(int i=1;i<=h;i++)
{
U[x1]=U[x2];
if(U[x1]!=-1)
D[U[x1]]=x1;
right(x1);
right(x2);
}
x1=s1;
x2=dot3;
for(int i=1;i<=h;i++)
{
L[x1]=x2;
down(x1);
down(x2);
}
x1=dot2;
x2=s2;
for(int i=1;i<=h;i++)
{
D[x1]=x2;
right(x1);
right(x2);
}
x1=dot1;
x2=s6;
for(int i=1;i<=h;i++)
{
R[x1]=R[x2];
if(R[x1]!=-1)
L[R[x1]]=x1;
down(x1);
down(x2);
}
//第二块
x1=s2;
x2=dot2;
for(int i=1;i<=h;i++)
{
U[x1]=x2;
right(x1);
right(x2);
}
x1=s2;
x2=s5;
for(int i=1;i<=h;i++)
{
L[x1]=x2;
down(x1);
down(x2);
}
x1=dot4;
for(int i=1; i<=h; i++)
{
D[x1]=q1[i];
if(D[x1]!=-1)
U[D[x1]]=x1;
right(x1);
}
x1=s6;
for(int i=1; i<=h; i++)
{
R[x1]=q2[i];
if(R[x1]!=-1)
L[R[x1]]=x1;
down(x1);
}
}
void putting(int n)
{
int s0;
int ss=s;
for(int i=1;i<=n;i++)
{
s0=ss;
for(int j=1;j<=n;j++)
{
cc[i][j]=store[s0/n+1][s0%n+1];
right(s0);
}
down(ss);
}
}
void print(int n)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout<<setw(5)<<setfill(' ')<<cc[i][j];
}
cout<<endl;
}
}
int main()
{
scanf("%d%d",&n,&m);
init(n);
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
looking(a,b,c);
revering(a,b,c);
//print(n);
}
putting(n);
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])+cc[i][j];
}
}
cout<<dp[n][n]<<endl;
return 0;
}