1 / 2
Dec 2022

Please help me figure out the error in my code , its giving WA.`

 #include <bits/stdc++.h>
    using namespace std;
    #define ed "\n"
    #define int long long
    #define ll long long
    #define fori(i, a, b) for(int i = a; i < b; i++)
    #define fora(a,n) for(int i = a; i < n; i++)
    #define forn(n) for(int i = 0; i < n; i++)
    #define nd cout << "\n"
    #define quick                          \
        std::ios_base::sync_with_stdio(0); \
        cin.tie(0);                        \
        cout.tie(0);
    const int M = 1e9 + 7;
    const int N = 1e5+5;
// -----------------------------------------------------------------------------------------------


int longestp(int i,int j,vector<string> &a,vector<vector<int>> &memoize){
    if(memoize[i][j]!=-1){
        return memoize[i][j];
    }
    int h=a.size(),w=a[0].size();
    int ans=1;
    for(auto x :{-1,0,1}){
        for(auto y:{-1,0,1}){
            if(x==0 && y==0){continue;}
            if(i+x<h && i+x>=0 && j+y>=0 && j+y<w && a[i+x][j+y]-a[i][j]==1){
                ans=max(ans,1+longestp(i+x,j+y,a,memoize));
            }
        }
    }
    memoize[i][j]=ans;
    return ans;
}
void solve()
{   
    while(true){
        int h,w,ans=0,ct=1;
        cin>>h>>w;
        if(h==0 && w==0){
            break;
        }
        vector<string>a(h);
        vector<vector<int>>memoize(51,vector<int>(51,-1));
        forn(h){
            cin>>a[i];
        }
        forn(h){
            fori(j,0,w){
                if(a[i][j]=='A'){
                    ans=max(ans,longestp(i,j,a,memoize));
                }
            }
        }
        cout<<"Case"<<" "<<ct<<":"<<" "<<ans<<ed;
    }
}

// ---------------------------------------------------------------------------------------
signed main()
{
    quick int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
}`Preformatted text`
  • created

    Dec '22
  • last reply

    Dec '22
  • 1

    reply

  • 592

    views

  • 2

    users

  • 1

    link

Try this test case and spot the mistake

4 3
ABE
CFG
BDH
ABC
4 3
ABE
CFG
BDH
ABC
4 3
ABE
CFG
BDH
ABC
0 0

ABCPATH3