注意下时间还有图中的环就可以了
#include <bits/stdc++.h>
//#define int long long
#define x it.first
#define y it.second
using namespace std;
const int N=2e3+10,M=5e6+1000;
char p[N][N];
bool st[N][N];
int dst[N][N];
int dr[N][N];
typedef pair<int,int>PII;
int n,m;
int f=0;
void bfs(PII it,int t)
{
if(dst[x][y])return ;
if(st[x][y])
{
dst[x][y]=t-dr[x][y];
st[x][y]=0;
f=1;
return ;
}
dr[x][y]=t;
if(x<1||x>n||y<1||y>m)return ;
st[x][y]=1;
int &sum=dst[x][y];
int i=x,j=y;
if(p[i][j]=='R')j++;
else if(p[i][j]=='L')j--;
else if(p[i][j]=='U')i--;
else if(p[i][j]=='D')i++;
bfs({i,j},t+1);
sum=dst[i][j];
if(!f)sum++;
if(!st[x][y])f=0;
st[x][y]=0;
}
void sove() {
cin>>n>>m;
int t=1;
for(int i=1;i<=n;i++)cin>>p[i]+1;
for(int i=1;i<=n+1;i++)
{
for(int j=1;j<=m+1;j++)dst[i][j]=st[i][j]=0;
}
PII it;
int ma=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(dst[i][j])continue;
f=0;
bfs({i,j},1);
int sum=dst[i][j];
if(ma<sum)
{
ma=sum;
it={i,j};
}
}
}
cout<<x<<" "<<y<<" "<<ma<<"\n";
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int t = 1;
cin >> t;
while (t--)sove();
}
唉,还是太菜了,调试了一上午才调试出来