Robot on the Board 2(dfs)

 注意下时间还有图中的环就可以了

#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();
}

唉,还是太菜了,调试了一上午才调试出来

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值