能否优化?
查看原帖
能否优化?
549228
Starmoon_dhw楼主2024/5/12 13:49

42分后面的点8.2s 可否继续优化?

#include<bits/stdc++.h>
using namespace std;
#define int long long
char a[1505][1505];
int b[1505][1505];
int sum[1505][1505];
int ans,n,q,fx,fy;
void bfs(int x,int y)
{
	if(x<0||y<0)
		return;
	if(a[x-1][y]=='D')
		b[x-1][y]=b[x][y],bfs(x-1,y);
	else if(a[x][y-1]=='R')
		b[x][y-1]=b[x][y],bfs(x,y-1);
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
			cin>>a[i][j];
		cin>>b[i][n+1];
	}
	for(int i=1;i<=n;i++)
		cin>>b[n+1][i];
	for(int i=n;i>=1;i--)
	{
		for(int j=n;j>=1;j--)
		{
			if(a[i][j]=='D')
				b[i][j]=b[i+1][j];
			else if(a[i][j]='R')
				b[i][j]=b[i][j+1];
			sum[i][j]=b[i][j]+sum[i][j+1]+sum[i+1][j]-sum[i+1][j+1]; 
		}
	}
	
	cout<<sum[1][1]<<"\n";
	cin>>q;
	for(int i=1;i<=q;i++)
	{
		cin>>fx>>fy;
		if(a[fx][fy]=='D')	
			a[fx][fy]='R';
		else if(a[fx][fy]=='R')
			a[fx][fy]='D';
		for(int i=n;i>=1;i--)
		{
			for(int j=n;j>=1;j--)
			{
				if(a[i][j]=='D')
					b[i][j]=b[i+1][j];
				else if(a[i][j]='R')
					b[i][j]=b[i][j+1];
				sum[i][j]=b[i][j]+sum[i][j+1]+sum[i+1][j]-sum[i+1][j+1]; 
			}
		}
		cout<<sum[1][1]<<"\n"; 
	}
	
	return 0;
 } 
2024/5/12 13:49
加载中...