Recursive to find the shortest path of a two-dimensional array, given an integer and an array arbitrarily select the number of the array to be able to get the integer

Source: Internet
Author: User

1. Give you a two-dimensional array, each number in the two-dimensional array is positive, requires to go from the upper left to the lower right corner, each step can only go to the right or down the number of steps along the way to add up, return the minimum path and

1 #I,j determines that the return value is fixed2 defWalk (Matrix, I, j):3     ifi = = Len (matrix)-1 andj = = Len (matrix[0])-1:#to the lower right corner, return the node value4         returnMatrix[i][j]5     ifi = = Len (Matrix)-1:#reach the last line6         returnMATRIX[I][J] + walk (Matrix, I, j+1)#only to the right.7     ifj = = Len (matrix[0])-1:#reach the last column8         returnMATRIX[I][J] + walk (Matrix, i+1, J)#can only go down9right = Matrix[i][j] + walk (Matrix, I, j+1)#the shortest path to the right-hand corner andTenDown = Matrix[i][j] + walk (Matrix, i+1, J)#the shortest path to the lower-right corner of the bottom position and One     returnMin (right, down)

2. Give you an array of arr, and an integer to aim. If the number in Arr can be selected arbitrarily, can it be added to the aim, return TRUE or False

1 # Association to find all the subsequence of a string 2 def issum (arr, I, SUM, aim): 3     if i = = len (arr):4         return sum = = aim5      returnor issum (arr, i+1, Sum+arr[i], AIM)

Recursive to find the shortest path of a two-dimensional array, given an integer and an array arbitrarily select the number of the array to be able to get the integer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.