95pts MLE#12求调
查看原帖
95pts MLE#12求调
1380676
He_XY楼主2024/10/21 16:58

L 表示 lake,T 表示 tree

#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> pii;
const int maxn=3005;
const int inf=2e9;
int n,m,q,r,k;
char s[maxn][maxn];
int t[maxn][maxn];
bool used[maxn][maxn];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
bool check(int x,int y){
	bool flag=false;
	if(x+1<=n){
		flag|=(s[x+1][y]=='L');
	}
	if(y+1<=m){
		flag|=(s[x][y+1]=='L');
	}
	if(x-1>0){
		flag|=(s[x-1][y]=='L');
	}
	if(y-1>0){
		flag|=(s[x][y-1]=='L');
	}
	return flag;
}
void dfs(int x,int y){
	used[x][y]=true;
	bool flag=check(x,y);
	for(int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(xx<=0||xx>n||yy<=0||yy>m) continue;
		if(s[xx][yy]=='L') continue;
		if(t[xx][yy]==0&&check(xx,yy)){
			s[xx][yy]='T';
			t[xx][yy]=inf;
			dfs(xx,yy);
			flag=true;
		}
		if(s[xx][yy]=='T'){
			flag=true;
			if(t[xx][yy]!=inf){
				t[xx][yy]=inf;
				if(!used[xx][yy]){
					dfs(xx,yy);
				}
			}
		}
	}
	if(flag){
		t[x][y]=inf;
		for(int i=0;i<4;i++){
			int xx=x+dx[i];
			int yy=y+dy[i];
			if(x<=0||xx>n||yy<=0||yy>m) continue;
			if(s[xx][yy]=='T'){
				if(t[xx][yy]!=inf){
					t[xx][yy]=inf;
					dfs(xx,yy);
				}
			}
		}
	}
	
}
int32_t main(){
	
	memset(s,'.',sizeof(s));
	cin>>n>>m>>q>>r>>k;
	while(q--){
		int a1,a2,b1,b2;
		cin>>a1>>a2>>b1>>b2;
		for(int i=a1;i<=b1;i++){
			for(int j=a2;j<=b2;j++){
				s[i][j]='L';
			}
		}
	}
	queue<pair<int,pii> > q;
	while(r--){
		
		int ti,x,y;
		cin>>ti>>x>>y;
		q.push(make_pair(ti,make_pair(x,y)));
		while(ti-q.front().first>k){
			pair<int,pii> p=q.front();
			q.pop();
			if(t[p.second.first][p.second.second]==inf){
				continue;
			}else{
				t[p.second.first][p.second.second]=0;
				s[p.second.first][p.second.second]='.';
				used[p.second.first][p.second.second]=false;
			}
		}
		s[x][y]='T';
		t[x][y]=ti;
		dfs(x,y);
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(s[i][j]=='T'&&t[i][j]==inf){
				ans++;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}
2024/10/21 16:58
加载中...