Question

Given a matrix with m rows and n columns, m adjacent numbers are chosen from m...

Given a matrix with m rows and n columns, m adjacent numbers are chosen from m rows, where two numbers are adjacent to each other if they are directly connected vertically or diagonally and only one number is taken from one row. Design a dynamic programming algorithm to find the smallest sum of these m numbers.

For example, given a 3 by 3 matrix

1 2 3
4 5 6
7 0 2

The sum of three numbers 1, 4, and 0 from three rows is the smallest sum 5.

Example 1:

Input:

  • board = [[1,2,3],[4,5,6],[7,0,2]]

Output:

  • 5

Example 2:

Input:

  • board = [[1]]

Output:

  • 1

Example 3:

Input:

  • board = [[1,2,3]]

Output:

  • 1

Example 4:

Input:

  • board = [[1,2,3,0],[4,5,1,6],[7,0,2,0]]

Output:

  • 1

here is some starting code

def matrix(board):

sum = ...

return sum

0 0
Add a comment Improve this question Transcribed image text
Answer #1

All the explanations is in the code comments. Please take care of indentation by referring to code screenshots.

Hope this helps! Comment in case of a doubt.

Code:

# recursive function using dynamic programming
# as each location is board is traversed only once, the time complexity is O(n^2)
def matrix(board, dp, i, j):
  
# first row
if(i == 0):
dp[i][j] = board[i][j]
  
# other rows
else:
a = 100000 # largest value possible, can take it as required
b = dp[i-1][j] # vertically up
c = 100000 # largest value possible, can take it as required
  
# left diagonal, if present
if(j > 0):
a = dp[i-1][j-1]
# right diagonal, if present
if((j+1) < len(board[0])):
c = dp[i-1][j+1]
  
# calculate dp which is
# value of present cell + minimum value from reachable cells of the above row
dp[i][j] = board[i][j] + min(a, b, c)
  
# end of column
if(j == (len(board[0]) -1)):
# reached the end of board
if(i == (len(board) -1)):
# return the minimum value from the last row
return min(dp[i])
else:
# move to the next row
return matrix(board, dp, i+1, 0)
else:
# move to the next column
return matrix(board, dp, i, j+1)

# sample run
# example 1
board = [[1,2,3],[4,5,6],[7,0,2]]
# dp denotes the minimum sum for that cell in row upto that column
# initailly all are -1
dp = [[-1 for i in range(len(board[0]))] for j in range(len(board))]
print('Example 1:', matrix(board, dp, 0, 0))

# example 2
board = [[1]]
dp = [[-1 for i in range(len(board[0]))] for j in range(len(board))]
print('Example 2:', matrix(board, dp, 0, 0))

# example 3
board = [[1,2,3]]
dp = [[-1 for i in range(len(board[0]))] for j in range(len(board))]
print('Example 3:', matrix(board, dp, 0, 0))

# example 4
board = [[1,2,3,0],[4,5,1,6],[7,0,2,0]]
dp = [[-1 for i in range(len(board[0]))] for j in range(len(board))]
print('Example 4:', matrix(board, dp, 0, 0))

Sample run:

Example 1: 5 Example 2: 1 Example 3: 1 Example 4: 1

Code screenshots:

i # recursive function using dynamic programming # as each location is board is traversed only once, the time complexity is O

Add a comment
Know the answer?
Add Answer to:
Given a matrix with m rows and n columns, m adjacent numbers are chosen from m...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON 3: An n x n matrix forms a magic square if the following conditions are...

    PYTHON 3: An n x n matrix forms a magic square if the following conditions are met: 1. The elements of the matrix are numbers 1,2,3, ..., n2 2. The sum of the elements in each row, in each column and in the two diagonals is the same value. Question: Complete the function that tets if the given matrix m forms a magic square. def is_square(m): '''2d-list => bool Return True if m is a square matrix, otherwise return False...

  • We are given a color picture consisting of an m?n array AŒ1::m;1::n? of pixels, where each...

    We are given a color picture consisting of an m?n array AŒ1::m;1::n? of pixels, where each pixel specifies a triple of red, green, and blue (RGB) intensities. Sup- pose that we wish to compress this picture slightly. Specifically, we wish to remove one pixel from each of the m rows, so that the whole picture becomes one pixel narrower. To avoid disturbing visual effects, however, we require that the pixels removed in two adjacent rows be in the same or...

  • ) A binary mxn matrix is a matrix with m rows and n columns, whose entries...

    ) A binary mxn matrix is a matrix with m rows and n columns, whose entries can be only 0 or 1. How many mxn binary matrices are there?

  • Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elemen...

    Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elements. which are called eernents (or components). The elements of an (m × n)-dimensional matrix A are denoted as a,, where 1im and1 S, symbolically, written as, A-a(1,1) S (i.j) S(m, ). Written in the familiar notation: 01,1 am Gm,n A3×3matrix The horizontal and vertical lines of entries in a matrix are called rows and columns, respectively A matrix with the...

  • how can i solve the system of these magic matrices using matlab software ? Exercice 3. A magic matrix is a square matrix with integer entries in which all the rows, columns and the two diagona...

    how can i solve the system of these magic matrices using matlab software ? Exercice 3. A magic matrix is a square matrix with integer entries in which all the rows, columns and the two diagonals have the same sum. For example, A- 3 5 7 4 9 2 Complete the following magic matrices 17? ?? 3 ? 2 ? 2? ? Do the following steps in each case: 1. Write the system of equations and put it under the...

  • please answer both questions thank you! How many rows and columns must a matrix A have...

    please answer both questions thank you! How many rows and columns must a matrix A have in order to define a mapping from R into R by the rule T(x) Ax? Choose the correct answer below OA. The matrix A must have 7 rows and 7 columns. O B. The matrix A must have 9 rows and 7 columns OC. The matrix A must have 9 rows and 9 columns O D. The matrix A must have 7 rows and...

  • matlab( answer the question in matlab) show the full answer with comment please. W a rnction...

    matlab( answer the question in matlab) show the full answer with comment please. W a rnction to find the largest product of adjacent number pairs in a matrix, where pair combinations can be selected the horsonal accent or vertically adjacent (but not diagonally adjacent) of numbers in a matrix are accent if they are in the same row or column and located next to each other when the 2D matrix is displayed The largest product of a pair of adjacent...

  • Assignment: Write a C function that accepts the pointer to a matrix of integer numbers (i.e....

    Assignment: Write a C function that accepts the pointer to a matrix of integer numbers (i.e. a two-dimensional array), number of rows and number of columns as input and prints the matrix after rotating it 90 degrees clockwise. Example: void rotate-matrix (int* matrix, int row, int column) Inputs: row 4, column-6 and the pointer to the matrix below: 2 3 6 5 8 0 Output:

  • A matrix with dimensions m by n, where m > n, has fewer rows than columns.

    Enter T or F depending on whether the statement is true or false. (Only ONE attempt allowed.) (You must enter Tor F -- True and False will not work.)_______  1. A matrix with dimensions m by n, where m > n, has fewer rows than columns._______  2. The 3rd row, 4th column entry of a matrix is below and to the right of the 2nd row, 3rd column entry.

  • We use JAVA. Thanks.    Create an application whose main method asks the user to enter...

    We use JAVA. Thanks.    Create an application whose main method asks the user to enter an n by m integer matrix that contains nm integer numbers. n and m should be between 1 and 10. If the user enters a             number less than 1 or greater than 10, the program will continue to ask the user to enter an integer number between 1 and 10. The program should print the sum of the boundary elements of the matrix....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT