LABYR1 - Labyrinth - SPOJ Solution C++

  Problem Link : LABYR1 


👉 Hint : edit please

 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
int ans=0;
int li,lj;
void dfs(char arr[1001][1001],int i,int j,int l,int r,int c,int pi,int pj)
{
	bool k=0;

	if(i-1 >=0 && i-1!=pi && arr[i-1][j]=='.')
	{
		k=1;
		dfs(arr,i-1,j,l+1,r,c,i,j);
	}
	if(i+1 <r && i+1!=pi && arr[i+1][j]=='.')
	{
		k=1;
		dfs(arr,i+1,j,l+1,r,c,i,j);
	}
	if(j-1 >=0 && j-1!=pj && arr[i][j-1]=='.')
	{
		k=1;
		dfs(arr,i,j-1,l+1,r,c,i,j);
	}
	if(j+1 <c && j+1!=pj && arr[i][j+1]=='.')
	{
		k=1;
		dfs(arr,i,j+1,l+1,r,c,i,j);
	}
	if(k==0)
	{
		if(l>ans)
		{
		    
			ans=l;
			li=i;
			lj=j;
		}
	
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin>>t;
	char arr[1001][1001];
	while(t--)
	{
		int c,r;
		bool b=1;
		cin>>c>>r;
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				cin>>arr[i][j];
			}
		}
		ans=0;
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				
				if(arr[i][j]=='.')
				{
				    dfs(arr,i,j,0,r,c,-1,-1);
				    dfs(arr,li,lj,0,r,c,-1,-1);
				    b=0;
				    break;
				}
				
			}
			if(b==0)
			    break;
		}
		cout<<"Maximum rope length is "<<ans<<"."<<endl;


	}

}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!