NC15726. F. Flower Road
描述
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
C++(g++ 7.5.0) 解法, 执行用时: 1261ms, 内存消耗: 9176K, 提交时间: 2022-09-09 08:24:08
#include<cstdio> #include<functional> int dp[1503][1503]; int main() { int n,m;scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)scanf("%d",dp[i]+j); while(m--) { int x,y,L,t;scanf("%d%d%d",&x,&y,&L); for(int i=x;i<x+L;i++)for(int j=y;j<y+L;j++) t=dp[i][j],dp[i][j]=dp[i+L][j],dp[i+L][j]=dp[i+L][j+L],dp[i+L][j+L]=dp[i][j+L],dp[i][j+L]=t; } for(int i=1;i<=n;i++)for(int j=1;j<=n;j++) dp[i][j]+=std::max(dp[i][j-1],dp[i-1][j]); printf("%d",dp[n][n]); }