MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayFind the length of the shortest path through the matrix from the top left to bottom right corner. You may move right, down, or diagonally right-down one element at a time. The length of the path is the sum of the elements you pass through.

E.g.

M = [*8 6 10 10 4 7 7 7 9 *1 10 5 9 0 8 2 1 3 *2 8 8 8 7 7 9 5 10 *1 *10 *9 *4 *0 ];

>> shortest_path(M)

ans =

35

The shortest path through this matrix has length 35. Each element along the path is marked with a *

38 correct solutions
18 incorrect solutions

Last solution submitted on Sep 09, 2014

6 players like this problem

4 Comments

Show
1 older comment

Vitaly Lavrukhin
on 23 Nov 2012

functional programming approach

Jean-Marie SAINTHILLIER
on 8 Sep 2013

Congratulations.
Honestly, I have difficulties with this kind of algorithm. I have the feeling that you only increment i or j, but at the end you check all the matrix.

Vitaly Lavrukhin
on 9 Sep 2013

Thanks, Jean-Marie. This solution uses recursive calls of the function. So all elements of the matrix are processed.
If you would like to learn more about functional programming principles, I recommend 'Functional Programming Principles in Scala' course by Martin Odersky at Coursera. It starts soon. Actually, I wrote this solution after taking the course a year ago. It is very interesting to look at the same problem from different points of view.

Jean-Marie SAINTHILLIER
on 10 Sep 2013

Thanks for your advice.

1 Comment