Difference between taking cumulative maximum VS individual maximum for SPOJ problem: ABCPATH

Revision en6, by abusomani, 2018-11-10 16:53:24

I successfully solved a very simple and yet conceptual problem on SPOJ called ( ABCPATH ) using three concepts(multi source BFS, Dijkstra and DP) and in the process I hit a surprising result in between, while implementing DP solution.

Let me first describe the problem in a nutshell :

We are given a 2D array of characters from 'A' to 'Z'. We need to start from 'A' and find the longest path with consecutive alphabets ( cannot trace back like 'N' to 'M' and then again a 'N'). Direction of travel is horizontal, vertical and diagonal.

I submitted two times for the problem. One wrong answer and second accepted. The only thing I did differently in the accepted logic was taking maximum after travelling into all eight directions. To give more insight let me add the logic of code.

Link to the Accepted solution : Accepted Solution

Link to the Wrong solution : Wrong Solution with one line shifted

The main logic of code that got SUBMITTED is ( Omitting the code for taking input and headers) :

        for (int i = 1; i <= N; i++)
        {
            for (int j = 1; j <= M; j++)
            {
                dp[i][j] = -INF;
                cin >> arr[i][j];

                if (arr[i][j] == 'A')
                    dp[i][j] = 1;
            }
        }

        for (char ch = 'A'; ch <= 'Z'; ch++)
        {
            for (int i = 1; i <= N; i++)
                for (int j = 1; j <= M; j++)
                {
                    if (arr[i][j] != ch)
                        continue;

                    for (int k = 0; k < 8; k++)
                    {
                        ll newX = i + rows[k];
                        ll newY = j + cols[k];
                        if (isValid(newX, newY) and (arr[newX][newY] == (ch - 1)))
                        {
                            dp[i][j] = max(dp[newX][newY] + 1, dp[i][j]);
                        }
                    }
                    
                    longest = max(longest, dp[i][j]); // THE LINE DIFFERENT FROM THE WRONG SUBMISSION
                }
     
   }

and the code that got WRONG, is :

       for (int i = 1; i <= N; i++)
            for (int j = 1; j <= M; j++)
            {
                dp[i][j] = 0;
                cin >> arr[i][j];

                if (arr[i][j] == 'A')
                    dp[i][j] = 1;
            }

        for (char ch = 'A'; ch <= 'Z'; ch++)
        {
            for (int i = 1; i <= N; i++)
                for (int j = 1; j <= M; j++)
                {
                    if (arr[i][j] != ch)
                        continue;

                    for (int k = 0; k < 8; k++)
                    {
                        ll newX = i + rows[k];
                        ll newY = j + cols[k];
                        if (isValid(newX, newY) and (arr[newX][newY] == (ch - 1)))
                        {
                            dp[i][j] = max(dp[newX][newY] + 1, dp[i][j]);
                            longest = max(longest, dp[i][j]); // THE LINE DIFFERENT FROM THE WRONG SUBMISSION
                        }
                    }
                }
        }

So, I ran both the code on multiple test cases trying to debug where the conflict comes and the code fails. Also, getting maximum over individual distances at the time of computation and maximum once the value is computed over all eight directions, how should the value be different?

I would be really obliged to see a test case to get the core logic of code failure in one case and pass in another.

Thanks

Tags #dynamic-programming, #spoj, longest path, maximum, 2d-dp, #dp

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en6 English abusomani 2018-11-10 16:53:24 0 Added links to the solution (published)
en5 English abusomani 2018-11-10 16:52:41 180 Tiny change: '/FnY4vt)\nLink to ' -> '/FnY4vt)\n\nLink to ' (saved to drafts)
en4 English abusomani 2018-11-10 16:42:52 130 (published)
en3 English abusomani 2018-11-10 16:38:17 985 Tiny change: 'aders) :\n\n`\n ' -> 'aders) :\n`return 0;`\n\n`\n '
en2 English abusomani 2018-11-10 16:20:05 2719
en1 English abusomani 2018-11-10 16:10:45 275 Initial revision (saved to drafts)