72分求助
查看原帖
72分求助
1246509
fbghi楼主2024/11/23 15:29

各位大佬们,本蒟蒻求助,这个思路好像有点小众,现在是72分

//同洛谷P1004方格取数 
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
struct node_xy{
	int x,y;
}t;
queue<node_xy> qq;
struct node{
	int s;
	queue<node_xy> q;
}f[12][12];
int a[12][12],ff[12][12];
int main(){
	int i,j,k,x,y,n;
	cin>>n;
	while(1)
	{
		cin>>x>>y>>k;
		if(!x&&!y&&!k) break;
		a[x][y]+=k;
	}
//	for(i=1;i<=n;i++)
//	{
//		for(j=1;j<=n;j++) cout<<a[i][j]<<" ";
//		cout<<endl; 
//	}
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			f[i][j].q.push({i,j});
			if(f[i-1][j].s>f[i][j-1].s||j==1)
			{
				f[i][j].s+=f[i-1][j].s;
				while(f[i-1][j].q.size())
				{
					t=f[i-1][j].q.front();
					f[i-1][j].q.pop();
					qq.push(t);
				}
				while(qq.size())
				{
					t=qq.front();
					f[i-1][j].q.push(t);
					f[i][j].q.push(t);
					qq.pop();
				}
			}
			else
			{
				f[i][j].s+=f[i][j-1].s;
				while(f[i][j-1].q.size())
				{
					t=f[i][j-1].q.front();
					f[i][j-1].q.pop();
					qq.push(t);
				}
				while(qq.size())
				{
					t=qq.front();
					f[i][j-1].q.push(t);
					f[i][j].q.push(t);
					qq.pop();
				}
			}
			f[i][j].s+=a[i][j];
		}
	}
	while(f[n][n].q.size())
	{
		t=f[n][n].q.front();
		f[n][n].q.pop();
		a[t.x][t.y]=0;
//		cout<<t.x<<" "<<t.y<<endl; 
	}
//	for(i=1;i<=n;i++)
//	{
//		for(j=1;j<=n;j++) cout<<a[i][j]<<" ";
//		cout<<endl; 
//	}
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++) ff[i][j]=max(ff[i-1][j],ff[i][j-1])+a[i][j];
	}
	cout<<f[n][n].s+ff[n][n];
	return 0;
}
2024/11/23 15:29
加载中...