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
  • 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...

  • 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...

  • Recall that if A is an m times n matrix and B is a p × q matrix

    Recall that if A is an m times n matrix and B is a p × q matrix, then the product C = AB is defined if and only if n = p. in which case C is an m × q matrix.  a. Write a function M-file that takes as input two matrices A and B, and as output produces the product by rows of the two matrices. For instance, if A is 3 times 4 and B is...

  • (Markov matrix) An n by n matrix is called a positive Markov matrix if each element...

    (Markov matrix) An n by n matrix is called a positive Markov matrix if each element is positive and the sum of the elements in each column is 1. Write the following function to check whether a matrix is a Markov matrix: def isMarkovMatrix(m): Write a test program that prompts the user to enter a 3 by 3 matrix of numbers and tests whether it is a Markov matrix. Note that the matrix is entered by rows and the numbers...

  • Python Script format please! 1. Write a script that takes in three integer numbers from the...

    Python Script format please! 1. Write a script that takes in three integer numbers from the user, calculates, and displays the sum, average, product, smallest, and largest of the numbers input. Important things to note: a. You cannot use the min() or max() functions, you must provide the logic yourself b. The calculated average must be displayed as an integer value (ex. If the sum of the three values is 7, the average displayed should be 2, not 2.3333). Example:...

  • 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...

  • 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:

  • Write a function Transpose that transposes a matrix T with M rows and N colums. The...

    Write a function Transpose that transposes a matrix T with M rows and N colums. The transposed matrix, TT, should have N rows and M columns. Example. Given the matrix T. (C++14) Example: 1 2 3 0 -6 7 The transposed matrix TT is 1 0 2 -6 3 7 DEFAULT CODE: Add to code as needed: #include <iostream> #include <string> #include <cmath> using namespace std; void Transpose(int T[100][100], int TT[100][100], int M, int N) { int i; int j;...

  • Maximum Value But Limited Neighbors You are given an array a[1..n] of positive numbers and an...

    Maximum Value But Limited Neighbors You are given an array a[1..n] of positive numbers and an integer k. You have to produce an array b[1..n], such that: (i) For each j, b[j] is 0 or 1, (ii) Array b has adjacent 1s at most k times, and (iii) sum_{j=1 to n} a[j]*b[j] is maximized. For example, given an array [100, 300, 400, 50] and integer k = 1, the array b can be: [0 1 1 0], which maximizes the...

  • 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
ADVERTISEMENT