subject

A board has n*m cells, and each cell has a value (positive or negative). The game is to start from the top-left cell, and move right or down or diagonal in each step, and finally reach the cell at the bottom-right cell. The objective is to find the maximum total values you may earn and to find a route that generates the maximum. Use the dynamic programming to model, program this problem, and compute its time complexity. Test your program using several data sets generated by a random number generator. I used a max sum path, but i'm struggling to correctly read diagonally. I was wondering if you can help with that. Thank you.
public class game
{
static int matrixGame(int[][] a, int n, int m)
{
if(n == 1)
return a[0][0];
int b[][] = new int[n+1][m+1];
int max = Integer. MIN_VALUE, maxi;
for(int k = 0; k < n; k++) {
b[n - 1][k] = a[n - 1][k];
}
for (int i = n-2; i >= 0; i--)
{
for (int j = 0; j < n; j++)
{
maxi = Integer. MIN_VALUE;
if (((j - 1) >= 0) && (maxi < b[i+1][j-1]))
maxi = b[i+1][j-1];
if(((j+1) < n) && (maxi < b[i+1][j+1]))
{
maxi = b[i+1][j+1];
}
b[i][j] = a[i][j] + maxi;
}
}
for(int i = 1; i <= n;i++)
for(int j = 1; j <= m; j++)
{
if (max < b[i][j])
{
b[i][j] = Math. max(Math. max(b[i-1][j - 1], b[i-1][j]), b[i-1][j] + a[i-1][j]);
max = b[i][j];
}
}
return max;
}
}
java. util. Arrays;
import java. util.*;
public class main {
public static void main(String[] args) {
Scanner input = new Scanner(System. in);
System. out. println("Please enter the dimensions of your matrices");
System. out. print("Rows: ");
int inputRow = input. nextInt();
System. out. print("Columns: ");
int inputCol = input. nextInt();
System. out. println("Please enter the values for matrix: ");
int[][] matrixOne;
matrixOne = new int[inputRow][inputCol];
for (int row = 0; row < inputRow; row++) {
for (int col = 0; col < inputCol; col++) {
matrixOne[row][col] = input. nextInt();
}
}
System. out. println("Matrix: ");
for (int i = 0; i < matrixOne. length; i++) {
for (int j = 0; j < matrixOne[i].length; j++) {
System. out. print(matrixOne[i][j] + " ");
}
System. out. println();
}
System. out. println();
int m = matrixOne. length;
int n = matrixOne. length;
System. out. print(game. matrixGame(matrixOne, n,m));
}
}

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 00:20
The open systems interconnection (osi) reference model: defines standards for many aspects of computing and communications within a network. is a generic description for how computers use multiple layers of protocol rules to communicate across a network. defines standards for wireless local area network (wlan) communication protocols. details the advantages and disadvantages of various basic network cabling options.
Answers: 1
question
Computers and Technology, 23.06.2019 01:30
Negative methods of behavior correction include all but this: sarcasm verbal abuse setting an example for proper behavior humiliation
Answers: 1
question
Computers and Technology, 23.06.2019 16:00
An english teacher would like to divide 8 boys and 10 girls into groups, each with the same combination of boys and girls and nobody left out. what is the greatest number of groups that can be formed?
Answers: 2
question
Computers and Technology, 24.06.2019 00:30
The best definition of an idiom is a. a word or phrase that describes a noun b. a word or phrase describing a verb c. a phrase containing figurative language in which the word expresses a different idea from its exact meaning d. a phrase that compares two unlike objects or ideas
Answers: 2
You know the right answer?
A board has n*m cells, and each cell has a value (positive or negative). The game is to start from t...
Questions
question
Chemistry, 02.10.2019 00:30
question
Mathematics, 02.10.2019 00:30
question
Mathematics, 02.10.2019 00:30
question
Mathematics, 02.10.2019 00:30
question
Mathematics, 02.10.2019 00:30
Questions on the website: 13722361