Difference between taking cumulative maximum VS individual maximum ( SPOJ : ABCPATH)

Revision en2, by dogedogedoge, 2018-11-10 16:20:05

I successfully solved a very simple and yet conceptual problem on SPOJ called ( ABCPATH ) but hit a intriguing doubt in between.

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.

While submitting I got WA for the the Dynamic Programming logic I wrote which got accepted by changing position of just one line!

The code that got submitted ( 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]);**
            }
    }

`

and the code that got WRONG, where the highlighted line was just placed inside the logic of travelling in all eight directions :

` 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]);**
                    }
                }
            }
    }

`

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

History

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