“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛) 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.


这道题目困了我很久很久,不过说实话也学到了很多,这道题是一道十字双向链题目,题目我不翻译了,对于之前没有学过十字链表的人我觉得有必要做一做,哪怕和我一样做了20h以上,我也觉得比较值得(我还学会了对拍程序)

代码很长,但是很多是重复的,注释的代码我没删去因为是中途用来调试的,如果你也看我的代码,我觉得对你有帮助,

注意从主函数开始看,我的建图个人习惯,周边有一圈“-1”圈,表示边界。

upup

#include<bits/stdc++.h>
using namespace std;
int n,m;
struct point
{
	int left;
	int right;
	int up;
	int down;
	int val;
	int num;
}a[6250000];
int c[20];
point head;
void creat()
{
	point p;
	int t;
	for(int i=0;i<n+2;i++)
	{
		a[i].val=a[(n+1)*(n+2)+i].val=-1;
		a[i*(n+2)].val=a[i*(n+2)+n+1].val=-1;
	}
	a[1].down=n+3;
	int pii=1;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			cin>>t;
		//	t = pii++;
		//	cout << t << " ";
			int tt=i*(n+2)+j;
			a[tt].up=(i-1)*(n+2)+j;
			a[tt].down=(i+1)*(n+2)+j;
			a[tt].left=i*(n+2)+j-1;
			a[tt].right=i*(n+2)+j+1;
			a[tt].val=t;
		}
	//	cout << endl;
	}
}
int dp[2500][2500]; 
void xz(int x,int y,int l)
{
	point temp;
	int t;
	int top=a[1].down,q1,q2,q3,q4;
	for(int i=1;i<x;i++) top=a[top].down;
	for(int i=1;i<y;i++) top=a[top].right;
	c[1]=top;
	for(int i=1;i<l;i++) top=a[top].right;
	c[2]=top;c[3]=a[top].right;
	for(int i=1;i<=l;i++) top=a[top].right;
	c[4]=top;
	for(int i=1;i<l;i++) top=a[top].down;
	c[8]=top;c[12]=a[top].down;
	for(int i=1;i<l;i++) top=a[top].left;
	c[7]=top;c[11]=a[top].down;c[6]=a[top].left;c[10]=a[c[6]].down;
	for(int i=1;i<=l;i++) top=a[top].left;
	c[5]=top;c[9]=a[top].down;
	for(int i=1;i<=l;i++) top=a[top].down;
	c[13]=top;
	for(int i=1;i<l;i++) top=a[top].right;
	c[14]=top;c[15]=a[top].right;
	for(int i=1;i<=l;i++) top=a[top].right;
	c[16]=top;
//	for(int i=1;i<=16;i++)	cout<<c[i]<<endl;
/*	while(a[top].val!=-1||a[top].val!=-1)
		{
			if(a[top].val==-1)
			{
				t=a[top].down;
			}
			while(a[top].val!=-1)
			{
				if(top.) 	
				head=a[head.right]; 
			}
			if(a[head.right].val==-1)
				cout<<head.val<<" ";
			cout<<endl;
			if(t==-1)
				break;
			head=a[t];
		}*/
	temp=a[c[1]];
	q1=c[1],q2=c[3];
	for(int i=1;i<=l;i++)	
	{
		a[q1].up=a[q2].up;a[q2].up=temp.up;
		a[a[q1].up].down=q1;a[a[q2].up].down=q2;
		q1=a[q1].right;q2=a[q2].right;
		temp=a[temp.right];
	}
	temp=a[c[1]];
	a[c[1]].left=c[4];a[c[2]].right=a[c[4]].right;a[c[3]].left=temp.left;a[c[4]].right=c[1];
	a[a[c[1]].left].right=c[1];a[a[c[2]].right].left=c[2];a[a[c[3]].left].right=c[3];a[a[c[4]].right].left=c[4];//边1 
	q1=a[c[1]].down;q2=a[c[2]].down;q3=a[c[3]].down;q4=a[c[4]].down;
	for(int i=1;i<=l-2;i++)
	{
		a[q2].right=a[q4].right;
		a[q4].right=q1;
		a[q3].left=a[q1].left;
		a[q1].left=q4;
		a[a[q2].right].left=q2;
		a[a[q4].right].left=q4;
		a[a[q3].left].right=q3;
		a[a[q1].left].right=q1;
		q1=a[q1].down;q2=a[q2].down;q3=a[q3].down;q4=a[q4].down;
	}
	temp=a[c[5]];
	q1=c[5],q2=c[7];
	for(int i=1;i<=l;i++)	
	{
		a[q1].down=a[q2].down;a[q2].down=temp.down;
		a[a[q1].down].up=q1;a[a[q2].down].up=q2;
		q1=a[q1].right;q2=a[q2].right;
		temp=a[temp.right];
	}
	temp=a[c[5]];
	a[c[5]].left=c[8];a[c[6]].right=a[c[8]].right;a[c[7]].left=temp.left;a[c[8]].right=c[5];
	a[a[c[5]].left].right=c[5];a[a[c[6]].right].left=c[6];a[a[c[7]].left].right=c[7];a[a[c[8]].right].left=c[8];
	/*head=a[a[1].down];
	while(a[head.down].val!=-1||a[head.right].val!=-1)
		{
			if(a[head.left].val==-1)
			{   
				t=head.down;
			}      
			while(a[head.right].val!=-1)                                                       
			{
				cout<<head.val<<" "; 	                                                                                
				head=a[head.right]; 
			}
			if(a[head.right].val==-1)
				cout<<head.val<<" ";
			cout<<endl;
			if(t==-1)
				break;
			head=a[t];
		}*///第一次 
	temp=a[c[9]];
	q1=c[9],q2=c[11];
	for(int i=1;i<=l;i++)	
	{
		a[q1].up=a[q2].up;a[q2].up=temp.up;
		a[a[q1].up].down=q1;a[a[q2].up].down=q2;
		q1=a[q1].right;q2=a[q2].right;
		temp=a[temp.right];
	}
	temp=a[c[9]];
	a[c[9]].left=c[12];a[c[10]].right=a[c[12]].right;a[c[11]].left=temp.left;a[c[12]].right=c[9];
	a[a[c[9]].left].right=c[9];a[a[c[10]].right].left=c[10];a[a[c[11]].left].right=c[11];a[a[c[12]].right].left=c[12];
	//边1 
	q1=a[c[9]].down;q2=a[c[10]].down;q3=a[c[11]].down;q4=a[c[12]].down;
	for(int i=1;i<=l-2;i++)
	{
		a[q2].right=a[q4].right;
		a[q4].right=q1;
		a[q3].left=a[q1].left;
		a[q1].left=q4;
		a[a[q2].right].left=q2;
		a[a[q4].right].left=q4;
		a[a[q3].left].right=q3;
		a[a[q1].left].right=q1;
		q1=a[q1].down;q2=a[q2].down;q3=a[q3].down;q4=a[q4].down;
	}
	temp=a[c[13]];
	q1=c[13],q2=c[15];
	for(int i=1;i<=l;i++)	
	{
		a[q1].down=a[q2].down;a[q2].down=temp.down;
		a[a[q1].down].up=q1;a[a[q2].down].up=q2;
		q1=a[q1].right;q2=a[q2].right;
		temp=a[temp.right];
	}
	temp=a[c[13]]; 
	a[c[13]].left=c[16];a[c[14]].right=a[c[16]].right;a[c[15]].left=temp.left;a[c[16]].right=c[13];
	a[a[c[13]].left].right=c[13];a[a[c[14]].right].left=c[14];a[a[c[15]].left].right=c[15];a[a[c[16]].right].left=c[16];
	//第二次交换
/*	head=a[a[1].down];
	while(a[head.down].val!=-1||a[head.right].val!=-1)
		{
			if(a[head.left].val==-1)
			{
				t=head.down;
			}
			while(a[head.right].val!=-1)
			{
				cout<<head.val<<" "; 	
				head=a[head.right]; 
			}
			if(a[head.right].val==-1)
				cout<<head.val<<" ";
			cout<<endl;
			if(t==-1)
				break;
			head=a[t];
		}*/
	temp=a[c[3]];
	q1=c[3];q2=c[9];
	for(int i=1;i<=l;i++)	
	{
		a[q1].up=a[q2].up;a[q2].up=temp.up;
		a[a[q1].up].down=q1;a[a[q2].up].down=q2;
		q1=a[q1].right;q2=a[q2].right;temp=a[temp.right];
	}
	temp=a[c[3]];
	a[c[3]].left=a[c[9]].left;a[c[9]].left=temp.left;
	a[a[c[3]].left].right=c[3];a[a[c[9]].left].right=c[9];
	temp=a[c[4]];
	a[c[4]].right=a[c[10]].right;a[c[10]].right=temp.right;
	a[a[c[4]].right].left=c[4];a[a[c[10]].right].left=c[10];//边1 
	temp=a[a[c[4]].down];
	q1=a[c[4]].down;q2=a[c[10]].down;
	for(int i=1;i<=l-2;i++)
	{
		a[q1].right=a[q2].right;a[q2].right=temp.right;
		a[a[q1].right].left=q1;a[a[q2].right].left=q2;
		q1=a[q1].down;q2=a[q2].down;temp=a[temp.down];
	}                                                    //边2 
	temp=a[c[7]];
	q1=c[7];q2=c[13];
	for(int i=1;i<=l;i++)	
	{
		a[q1].down=a[q2].down;a[q2].down=temp.down;
		a[a[q1].down].up=q1;a[a[q2].down].up=q2;
		q1=a[q1].right;q2=a[q2].right;temp=a[temp.right];
	}
	temp=a[c[7]];
	a[c[7]].left=a[c[13]].left;a[c[13]].left=temp.left;
	a[a[c[7]].left].right=c[7];a[a[c[13]].left].right=c[13];
	temp=a[c[8]];
	a[c[8]].right=a[c[14]].right;a[c[14]].right=temp.right;
	a[a[c[8]].right].left=c[8];a[a[c[14]].right].left=c[14];//边3
	temp=a[a[c[3]].down];
	q1=a[c[3]].down;q2=a[c[9]].down;
	for(int i=1;i<=l-2;i++)
	{
		a[q1].left=a[q2].left;a[q2].left=temp.left;
		a[a[q1].left].right=q1;a[a[q2].left].right=q2;
		q1=a[q1].down;q2=a[q2].down;temp=a[temp.down];
	}                                                      //边4 
	//第三次交换
	/*head=a[a[1].down];
	while(a[head.down].val!=-1||a[head.right].val!=-1)
		{
			if(a[head.left].val==-1)
			{
				t=head.down;
			}
			while(a[head.right].val!=-1)
			{
				cout<<head.val<<" "; 	
				head=a[head.right]; 
			}
			if(a[head.right].val==-1)
				cout<<head.val<<" ";
			cout<<endl;
			if(t==-1)
				break;
			head=a[t];
		}*/
}
void qq(int x,int y,int d)
{
	
	int top=a[1].down;
	for(int i=1;i<x;i++) top=a[top].down;
	for(int i=1;i<y;i++) top=a[top].right;
	int q1=top,q2=a[top].right,q3=a[q2].down,q4=a[q1].down;
	point temp=a[q1];
	a[q1].up=a[q2].up;a[q1].down=q2;a[q1].right=a[q2].right;a[q1].left=q4;
	a[a[q1].up].down=q1;a[a[q1].down].up=q1;a[a[q1].right].left=q1;a[a[q1].left].right=q1;
	a[q2].up=q1;a[q2].down=a[q3].down;a[q2].right=a[q3].right;a[q2].left=q3;
	a[a[q2].up].down=q2;a[a[q2].down].up=q2;a[a[q2].right].left=q2;a[a[q2].left].right=q2;
	a[q3].up=q4;a[q3].down=a[q4].down;a[q3].right=q2;a[q3].left=a[q4].left;
	a[a[q3].up].down=q3;a[a[q3].down].up=q3;a[a[q3].right].left=q3;a[a[q3].left].right=q3;	
	a[q4].up=temp.up;a[q4].down=q3;a[q4].right=q1;a[q4].left=temp.left;
	a[a[q4].up].down=q4;a[a[q4].down].up=q4;a[a[q4].right].left=q4;a[a[q4].left].right=q4;
	int t;
	head=a[a[1].down];
/*(	while(a[head.down].val!=-1||a[head.right].val!=-1)
		{
			if(a[head.left].val==-1)
			{
				t=head.down;
			}
			while(a[head.right].val!=-1)
			{
				cout<<head.val<<" "; 	
				head=a[head.right]; 
			}
			if(a[head.right].val==-1)
				cout<<head.val<<" ";
			cout<<endl;
			if(t==-1)
				break;
			head=a[t];
		}*/
}


int main()
{
	//srand(time(NULL));
	cin>>n>>m;	
	/*n = 100;
	m = 100;
	
	cout << n << " " << m << endl;   
	*/
	int x,y,z;
	creat();
	int hang=1,lie=1;
	int p;	
	int t;
	for(int i=1;i<=m;i++)
	{
		cin>>x>>y>>z;
		
	/*	x = rand() % (n - 1);
		x ++;
		
		y = rand() % (n - 1);
		y ++;
		
		z = rand() % min((n - x + 1) / 2, (n - y + 1) / 2);
		z ++;
		
		cout << x << " " << y << " " << z << endl;
	*/
		
				
		memset(c,0,sizeof(c));
		if(z!=1)
			xz(x,y,z);	
		else
			qq(x,y,z);
	}	
	/*head=a[a[1].down];
	while(a[head.down].val!=-1||a[head.right].val!=-1)
		{
			if(a[head.left].val==-1)
			{
				t=head.down;
			}
			while(a[head.right].val!=-1)
			{
				cout<<head.val<<" "; 	
				head=a[head.right]; 
			}
			if(a[head.right].val==-1)
				cout<<head.val<<" ";
			cout<<endl;
			if(t==-1)
				break;
			head=a[t];
		}*/
	int u=1,v=1;
	head=a[a[1].down];
	while(a[head.down].val!=-1||a[head.right].val!=-1)
	{
		if(a[head.left].val==-1)
		{
			t=head.down;
		}
		while(a[head.right].val!=-1)
		{
			dp[u][v]=head.val; 
			v++;
			head=a[head.right]; 
		}
		if(a[head.right].val==-1)
		{
			dp[u][v]=head.val;
			v=1;u++;
		}
		if(t==-1)
			break;
		head=a[t];
	} 
/*	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
			cout<<dp[i][j]<<" ";
		cout<<endl;
	}*/
	for(int i=1;i<n;i++)
		dp[1][i+1]+=dp[1][i];
	for(int i=2;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
			dp[i][j]+=max(dp[i-1][j],dp[i][j-1]);
	}
	cout<<dp[n][n];
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ant_e_zz/article/details/80269361
今日推荐