title: OutputError: Find the longest path in a matrix with given constraints
body: <p><strong>My matrix is:</strong></p>
  
  <pre><code>4 8 7 3 
  2 5 9 3 
  6 3 2 5 
  4 4 1 6
  </code></pre>
  
  <p><strong>Problem (Skiing):</strong></p>
  
  <p>Each <strong>number</strong> represents the <strong>elevation</strong> of that area of the mountain. </p>
  
  <p>From each area (i.e. box) in the grid, you can go <strong>north</strong>, <strong>south</strong>, <strong>east</strong>, <strong>west</strong> - but only if the elevation of the area you are going into is <strong>less</strong> than the one you are in. </p>
  
  <p>I.e. you can only ski downhill. </p>
  
  <p>You can start anywhere on the map and you are looking for a starting point with the <strong>longest possible path down</strong> as measured by the number of boxes you visit.</p>
  
  <p>And if there are several paths down of the same length, you want to take the one with the <strong>steepest vertical drop</strong>, i.e. the largest difference between your starting elevation and your ending elevation.</p>
  
  <p><strong>My Solution:</strong></p>
  
  <pre><code>def findSkiPath():
      mySolution = [0] * 3
      mySolution[0] = 0 # Distance
      mySolution[1] = 0 # Drop
      cellIndexRow = 0
      cellIndexCol = 0
      myPath = []
      myMatrix = [[4, 5, 8, 7],[1, 1, 5, 9], [0, 7, 5, 5], [7, 4, 2, 9]]
      countRows = len(myMatrix)
      countCols = len(myMatrix[0])
      for i in range(0, countRows - 1):
          for j in range(0, countCols - 1):
              myValue = myMatrix[i][j]
              myPath.append(myValue)
  
              #check east
              cellIndexRow = i
              cellIndexCol = j + 1
              checkAdjacentCells(cellIndexRow, cellIndexCol, myValue, myMatrix , mySolution , myPath )
  
              #check west
              cellIndexRow = i
              cellIndexCol = j - 1
              checkAdjacentCells(cellIndexRow, cellIndexCol, myValue, myMatrix , mySolution , myPath )
  
              #check north
              cellIndexRow = i - 1
              cellIndexCol = j
              checkAdjacentCells(cellIndexRow, cellIndexCol, myValue, myMatrix , mySolution , myPath )            
  
              #check south
              cellIndexRow = i + 1
              cellIndexCol = j
              checkAdjacentCells(cellIndexRow, cellIndexCol, myValue, myMatrix , mySolution , myPath )
      print (mySolution)
  
  def checkAdjacentCells(cellIndexRow, cellIndexCol, myValue, myMatrix , mySolution , myPath ):
  
      #The base case - If we go beyond the limits of the matrix
      if (cellIndexRow &lt; 0 or cellIndexRow &gt; (len(myMatrix) - 1) or cellIndexCol &lt; 0 or cellIndexCol &gt; (len(myMatrix[0]) - 1)):
          evaluateSolution(mySolution , myPath )
          return
  
      #check if the next cell has a lower value than the current cell
      tmpValue = myMatrix[cellIndexRow][cellIndexCol]
      if tmpValue &lt; myValue:
          newPath = myPath
          newPath.append(tmpValue)
  
          r = cellIndexRow
          c = cellIndexCol
          #check east
          cellIndexRow = r
          cellIndexCol = c + 1
          checkAdjacentCells(cellIndexRow, cellIndexCol, tmpValue, myMatrix , mySolution , newPath )
  
          #check west
          cellIndexRow = r
          cellIndexCol = c - 1
          checkAdjacentCells(cellIndexRow, cellIndexCol, tmpValue, myMatrix , mySolution , newPath )
  
          #check north
          cellIndexRow = r - 1
          cellIndexCol = c
          checkAdjacentCells(cellIndexRow, cellIndexCol, tmpValue, myMatrix , mySolution , newPath )          
  
          #check south
          cellIndexRow = r + 1
          cellIndexCol = c
          checkAdjacentCells(cellIndexRow, cellIndexCol, tmpValue, myMatrix , mySolution , newPath )
  
      evaluateSolution(mySolution , myPath )
  
  def evaluateSolution(mySolution , myPath ):
  
      myDistance = 1
      mySolutionDistance = int(mySolution[0])
      mySolutionDrop = int(mySolution[1])
  
      if myDistance &lt; mySolutionDistance:
          return
  
      myDrop = myPath[0] - myPath[-1]
  
      if myDistance &gt; mySolutionDistance or myDrop &gt; mySolutionDrop:
          mySolution[0] = myDistance
          mySolution[1] = mySolutionDrop
          mySolution[2] = myPath
  
  if __name__ == "__main__":
  
      findSkiPath()
  </code></pre>
  
  <p><strong>Issues:</strong></p>
  
  <p>Current output (distance, drop, path):</p>
  
  <blockquote>
    <p>[1, 0, [4, 2, 8, 7, 3, 4, 2, 5, 2, 3, 2, 1, 7, 3, 2, 5, 2, 3, 2, 1, 9,
    3, 5, 2, 3, 2, 1, 7, 3, 2, 1, 6, 3, 2, 1, 2, 4, 3, 2, 1, 2, 1]]</p>
  </blockquote>
  
  <p>Expected output:</p>
  
  <blockquote>
    <p>[5,8,[9,5,3,2,1]]</p>
  </blockquote>
  
  <p>On this particular map, the longest path down is of length=5, drop=8 (9-1=8), and path: 9-5-3-2-1.</p>