代码MLE求条
查看原帖
代码MLE求条
795784
zxh923楼主2024/10/18 17:06
#include<bits/stdc++.h> 
//#define int long long
#define N 2009
#define pii pair<int,int>
#define x first
#define y second
#define mod 1000000007
#define pct __builtin_popcount
#define inf 2e12
#define eps 1e-10
using namespace std; 
int T=1,n,m,st[N][N],vis[N][N];
char s[N][N];
stack<pii>stk;
bool check(int x,int y){
	return x>=1&&x<=n&&y>=1&&y<=m;
}
inline int dfs(int x,int y,int t){
	stk.push({x,y});
	vis[x][y]=t;
	int type=1;
	if(s[x][y]=='R'){
		if(!check(x,y+1)){
			int tot=0;
			while(!stk.empty()){
				auto it=stk.top();
				stk.pop();
				int tx=it.x,ty=it.y;
				tot++;
				st[tx][ty]=tot;
			}
		}
		else if(vis[x][y+1]){
			stack<pii>tmp=stk;
			int cnt=0;
			int now=st[x][y+1];
			if(vis[x][y+1]!=t)type=0;
			while(!tmp.empty()){
				auto it=tmp.top();
				tmp.pop();
				cnt++;
				int tx=it.x,ty=it.y;
				if(tx==x&&ty==y+1)break;
			}
			int tot=0;
			while(!stk.empty()){
				auto it=stk.top();
				stk.pop();
				int tx=it.x,ty=it.y;
				tot++;
				int mx=tot;
				if(type)mx=max(mx,cnt);
				st[tx][ty]=mx+now;
			}
		}
		else dfs(x,y+1,t);
	}
	if(s[x][y]=='L'){
		if(!check(x,y-1)){
			int tot=0;
			while(!stk.empty()){
				auto it=stk.top();
				stk.pop();
				int tx=it.x,ty=it.y;
				tot++;
				st[tx][ty]=tot;
			}
		}
		else if(vis[x][y-1]){
			stack<pii>tmp=stk;
			int cnt=0;
			int now=st[x][y-1];
			if(vis[x][y-1]!=t)type=0;
			while(!tmp.empty()){
				auto it=tmp.top();
				tmp.pop();
				cnt++;
				int tx=it.x,ty=it.y;
				if(tx==x&&ty==y-1)break;
			}
			int tot=0;
			while(!stk.empty()){
				auto it=stk.top();
				stk.pop();
				int tx=it.x,ty=it.y;
				tot++;
				int mx=tot;
				if(type)mx=max(mx,cnt);
				st[tx][ty]=mx+now;
			}
		}
		else dfs(x,y-1,t);
	}
	if(s[x][y]=='D'){
		if(!check(x+1,y)){
			int tot=0;
			while(!stk.empty()){
				auto it=stk.top();
				stk.pop();
				int tx=it.x,ty=it.y;
				tot++;
				st[tx][ty]=tot;
			}
		}
		else if(vis[x+1][y]){
			stack<pii>tmp=stk;
			int cnt=0;
			int now=st[x+1][y];
			if(vis[x+1][y]!=t)type=0;
			while(!tmp.empty()){
				auto it=tmp.top();
				tmp.pop();
				cnt++;
				int tx=it.x,ty=it.y;
				if(tx==x+1&&ty==y)break;
			}
			int tot=0;
			while(!stk.empty()){
				auto it=stk.top();
				stk.pop();
				int tx=it.x,ty=it.y;
				tot++;
				int mx=tot;
				if(type)mx=max(mx,cnt);
				st[tx][ty]=mx+now;
			}
		}
		else dfs(x+1,y,t);
	}
	if(s[x][y]=='U'){
		if(!check(x-1,y)){
			int tot=0;
			while(!stk.empty()){
				auto it=stk.top();
				stk.pop();
				int tx=it.x,ty=it.y;
				tot++;
				st[tx][ty]=tot;
			}
		}
		else if(vis[x-1][y]){
			stack<pii>tmp=stk;
			int cnt=0;
			int now=st[x-1][y];
			if(vis[x-1][y]!=t)type=0;
			while(!tmp.empty()){
				auto it=tmp.top();
				tmp.pop();
				cnt++;
				int tx=it.x,ty=it.y;
				if(tx==x-1&&ty==y)break;
			}
			int tot=0;
			while(!stk.empty()){
				auto it=stk.top();
				stk.pop();
				int tx=it.x,ty=it.y;
				tot++;
				int mx=tot;
				if(type)mx=max(mx,cnt);
				st[tx][ty]=mx+now;
			}
		}
		else dfs(x-1,y,t);
	}
	return st[x][y];
}
void solve(int cs){
	cin>>n>>m;
	for(int i=0;i<=n+1;i++){
		for(int j=0;j<=m+1;j++){
			vis[i][j]=0;
			st[i][j]=0;
		}
	}
	for(int i=1;i<=n;i++){
		cin>>s[i]+1;
	}
	int cnt=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(!st[i][j]){
//				cout<<"begin"<<i<<' '<<j<<'\n';
				dfs(i,j,++cnt);
			}
		}
	}
	int x,y,mx=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
//			cout<<i<<' '<<j<<' '<<st[i][j]<<'\n';
			if(st[i][j]>mx){
				mx=st[i][j];
				x=i;y=j;
			}
		}
	}
	cout<<x<<' '<<y<<' '<<mx<<'\n';
}
void solution(){
	/*
	nothing here
	*/
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	srand(time(0));
	cin>>T;
	for(int cs=1;cs<=T;cs++){
		solve(cs);
	}
	return 0;
}

rt,第四个点 MLE

2024/10/18 17:06
加载中...