1 / 8
Jan 2022
#include <vector>
using namespace std;
typedef long long int ll;
#define ull unsigned long long
#define rep(i, m, n) for (auto i = m; i < n; i++)
#define mi map<ll,ll>::iterator 
#define pb push_back
#define pob pop_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define MOD 1000000007
#define f(n) for(int i=0;i<n;i++)
#define PI 3.1415926536
#define dhroov ios_base::sync_with_stdio(false); cin.tie(NULL);
#define taxi auto

// ll visited[51][51];
// #define N=100001;
// bool visited[N];

// DFS
ll dfs(ll z,ll c, ll i, ll j,vector<vector<ll>> a,ll (&visited)[51][51])
{
      if(i>=z||i<0||j>=c||j<0||visited[i][j]!=0) return 0;
      visited[i][j]=1;
      ll p,q,r,s,t,u,v,w;
      p=q=r=s=t=u=v=w=0;
      if(i+1<a.size()&&a[i+1][j]==a[i][j]+1){p=1; p+=dfs(z,c,i+1,j,a,visited);}
      if(j+1<a[0].size()&&a[i][j+1]==a[i][j]+1){ q=1; q+=dfs(z,c,i,j+1,a,visited);}
      if(i-1>=0&&a[i-1][j]==a[i][j]+1){ r=1; r+=dfs(z,c,i-1,j,a,visited);}
      if(j-1>=0&&a[i][j-1]==a[i][j]+1){ s=1; s+=dfs(z,c,i,j-1,a,visited);}
      if(i+1<a.size()&&j+1<a[0].size()&&a[i+1][j+1]==a[i][j]+1){ t=1; t+=dfs(z,c,i+1,j+1,a,visited);}
      if(i+1<a.size()&&j-1>=0&&a[i+1][j-1]==a[i][j]+1){ u=1; u+=dfs(z,c,i+1,j-1,a,visited);}
      if(i-1>=0&&j+1<a[0].size()&&a[i-1][j+1]==a[i][j]+1){ v=1; v+=dfs(z,c,i-1,j+1,a,visited);}
      if(i-1>=0&&j-1>=0&&a[i-1][j-1]==a[i][j]+1){ w=1; w+=dfs(z,c,i-1,j-1,a,visited);}
      
      return(max({p,q,r,s,t,u,v,w}));
}
 
int main() 
{
   dhroov;
   ll t,y=0;
   while(1)
   {
          ll visited[51][51]={0};
         y++;
         ll r,c,m,ans=0;
         char z;
         cin>>r>>c;
         if(r==0&&c==0) break;
      vector<vector<ll>> v;
      f(r){vector <ll> k; rep(j,0,c) {cin>>z;k.pb(int(z)-65);}v.pb(k);}
      // cin>>h>>w;
      f(r)
      {
            rep(j,0,c)
            {
                 
                  // cout<<i<<j<<" ";
                  if(v[i][j]==0)
                  {
                        // cout<<i<<j<<" ";
                     m=dfs(r,c,i,j,v,visited);
                  //    cout<<m<<" ";
                  // cout<<m<<endl;
                     ans=max(ans,m+1);
                  }
            }
      }
      cout<<"Case :"<<y<<" "<<ans<<endl;
   }
}
   
   
  • created

    Jan '22
  • last reply

    Jan '22
  • 7

    replies

  • 673

    views

  • 2

    users

  • 1

    link

It doesn’t read the correct input format, and doesn’t write the correct output format. But apart from that, it’s good to go.

You probably needs to test it against the sample input from the problem statement.

When posting code, please put three back ticks ``` on a line by themselves both before and after the code. That will stop the editor from changing

(r) into ®
-- into –
" into ” or “

etc

ABCPATH2

I corrected the output format and input as well as mentioned and the code seems good to me but then also it is showing wrong answer

Can you show me the number of test cases in the sample input?

ya ya I’ve corrected that too… let me update it… still its getting wrong answer

Check your output format again.

I also get a compile error on ideone.

prog.cpp:39:35: error: no matching function for call to ‘max()’
return(max({p,q,r,s,t,u,v,w}));

interestingly same max is running in my ideone… after 100s of minor corrections this is my program which I guess is perfect please look into it

I added

#include <iostream>
#include <algorithm>

to make it compile in C++14.

100s of corrections maybe, but you’ve missed an important one. So perfect it is not. You still need to check and correct the output format.