1 / 2
Aug 2022

I need some help in this problem as I couldn’t find detailed approach for this anywhere in google


I thought using simple dfs and finding longest connected component should work, so I coded like this
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int,int> pi;
int res=0;
void dfs(vector&s,int i,int j,int n,int m,vector<vector>&vis,int len)
{
vis[i][j]=1;
res=max(res,len);
if(i+1<n && vis[i+1][j]==0) dfs(s,i+1,j,n,m,vis,len+1);
if(i-1>=0 && vis[i-1][j]==0) dfs(s,i-1,j,n,m,vis,len+1);
if(j+1<m && vis[i][j+1]==0) dfs(s,i,j+1,n,m,vis,len+1);
if(j-1>=0 && vis[i][j-1]==0) dfs(s,i,j-1,n,m,vis,len+1);
}
int main()
{

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t=1;
  cin>>t;
  while(t--)
  {
        int m,n;
        cin>>m>>n;
        int i,j;
        vector<string>s(n);
        for(i=0;i<n;i++) cin>>s[i];
        vector<vector<int>>vis(n,vector<int>(m,0));
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(s[i][j]=='#')
                vis[i][j]=1;
            }
        }
        res=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(!vis[i][j])
                {
                    dfs(s,i,j,n,m,vis,0);
                }
            }
        }
        cout<<"Maximum rope length is "<<res<<"."<<endl;
  }
  return 0;

}
It is working on sample test cases and also on my own generated small cases, if I understood the problem correctly, but its giving wrong answer when I submit it. Can anyone please help me understand the question or approach better, or if I understood it correctly, then point out the problem with my code ?
Thank you !

  • created

    Aug '22
  • last reply

    Aug '22
  • 1

    reply

  • 560

    views

  • 2

    users

  • 1

    link

I see you’ve got accepted, did you find out what your problem was?