1
$\begingroup$

According to question,

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

   3
  7 4
 2 4 6
8 5 9 3

That is, 3 + 7 + 4 + 9 = 23.

I think, the largest number should be from top to bottom is 3 + 7 + 6 + 9 = 25 instead of 23. If we will take all the largest number from the rows.

Is my understanding wrong?

Because when I wrote the program for

                         75
                        95 64
                      17 47 82
                     18 35 87 10
                   20 04 82 47 65
                  19 01 23 75 03 34
                 88 02 77 73 07 63 67
                99 65 04 28 06 16 70 92
               41 41 26 56 83 40 80 70 33
              41 48 72 33 47 32 37 16 94 29
             53 71 44 65 25 43 91 52 97 51 14
            70 11 33 28 77 73 17 78 39 68 17 57
           91 71 52 38 17 14 91 43 58 50 27 29 48
          63 66 04 68 89 53 67 30 73 16 69 87 40 31
         04 62 98 27 23 09 70 98 73 93 38 53 60 04 23

Program.c

int main()
{
    int max = 0;
    int temp = 0;
    int i, j, ans;
    int myarr[15];
    int arr[15][15] = {
        75,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
        95,  64,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
        17,  47,  82,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
        18,  35,  87,  10,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
        20,  04,  82,  47,  65,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
        19,  01,  23,  75,  03,  34,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
        88,  02,  77,  73,  07,  63,  67,   0,   0,   0,   0,   0,   0,   0,   0, 
        99,  65,  04,  28,  06,  16,  70,  92,   0,   0,   0,   0,   0,   0,   0, 
        41,  41,  26,  56,  83,  40,  80,  70,  33,   0,   0,   0,   0,   0,   0, 
        41,  48,  72,  33,  47,  32,  37,  16,  94,  29,   0,   0,   0,   0,   0, 
        53,  71,  44,  65,  25,  43,  91,  52,  97,  51,  14,   0,   0,   0,   0, 
        70,  11,  33,  28,  77,  73,  17,  78,  39,  68,  17,  57,   0,   0,   0, 
        91,  71,  52,  38,  17,  14,  91,  43,  58,  50,  27,  29,  48,   0,   0, 
        63,  66,  04,  68,  89,  53,  67,  30,  73,  16,  69,  87,  40,  31,   0, 
        04,  62,  98,  27,  23,   9,  70,  98,  73,  93,  38,  53,  60,  04,  23
    };
    for(i = 0, ans = 0; i < 15; i++)
    {
        for(j = 0; j < 15; j++)
        {
           temp = arr[i][j];
           if(temp > max)
           {
               max = temp;
           }
        }

        ans += max;
        cout << "Max of row " << i << " is " << max << endl;
        max = 0;
    }
    std::cout << ans << endl;
    system("pause");
}

I got the answer as 1313.

But it says it is a wrong answer. Please help me understanding the question., where I am going wrong?

$\endgroup$
  • $\begingroup$ You want to find a "connected" path, going from the top to the bottom row, in each step moving a "half-step" to the right or to the left. $\endgroup$ – mrf Dec 31 '13 at 12:45
  • $\begingroup$ @mrf: - Could you please elaborate? $\endgroup$ – Rasmi Ranjan Nayak Dec 31 '13 at 12:47
  • 2
    $\begingroup$ In your example 7 and 6 are not adjacent. (You need to move 3/2 steps to the right from 7 to 6.) $\endgroup$ – mrf Dec 31 '13 at 12:49
  • $\begingroup$ Doesn't the Project Euler site itself have a forum for discussing the questions? $\endgroup$ – GEdgar Dec 31 '13 at 22:51
  • 1
    $\begingroup$ @GEdgar It's only available to those who have solved the relevant question, I believe. The PE people are extremely anti-collaboration. $\endgroup$ – Potato Dec 31 '13 at 23:22
3
$\begingroup$

You can only step downward to the left or downward to the right from where you are. If you choose the $7$ in the second row, your only choices for the third row are $2$ or $4$, not $6$

$\endgroup$
  • $\begingroup$ Still I am confued, why can't I choose 6? $\endgroup$ – Rasmi Ranjan Nayak Dec 31 '13 at 13:05
  • $\begingroup$ It is not adjacent (next) to $7$ $\endgroup$ – Ross Millikan Dec 31 '13 at 13:06
1
$\begingroup$

In addition to people pointing out why you are wrong. I think you would benefit from someone telling you how to solve this problem

The correct algorithm should be working backward. Look up Bellman principle. This problem is often as used as a first example problem in introducing dynamic maximisation problem.

Edit: someone pointed out i shouldn't post a solution! I think the op would benefit from my relatively vague hint. It would reduce the complexity of your problem significantly!

$\endgroup$
  • 1
    $\begingroup$ Actually, I think it's a bad idea to post solutions to problems from project Euler. $\endgroup$ – mrf Dec 31 '13 at 22:51
  • $\begingroup$ @mrf what is project Euler, my precious? $\endgroup$ – Lost1 Dec 31 '13 at 22:51
1
$\begingroup$

You need to see the diagram like this:

$$\begin{array}{ccccccccccc} &&&3\\ &&\swarrow&&\searrow\\ &&7&&4\\ &\swarrow&&\searrow\swarrow&&\searrow\\ &2&&4&&6\\ \swarrow&&\searrow\swarrow&&\searrow\swarrow&&\searrow\\ 8&&5&&9&&3\\ \end{array}$$

so that you see there is no path from the $7$ to the $6$.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.