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, 22.06.2019 09:00
Designing a mobile web page is a little different from designing a regular web page. name at least three features that should be considered when designing a website that is mobile phone-friendly, and briefly explain why they are important.
Answers: 1
question
Computers and Technology, 23.06.2019 01:30
Which tab is used to change the theme of a photo album slide show? a. design b. view c. transitions d. home
Answers: 1
question
Computers and Technology, 23.06.2019 21:30
Examine the list below. which factors positively affect lifetime income? check all that apply.
Answers: 1
question
Computers and Technology, 24.06.2019 07:20
3pointsyou've found an image you want to insert into your slide presentation. youwant to make the image look more gray so that it looks like an older imagewhat would you need to adjust? 0.00o a. sizeo b. hueo c. contrasto d. tones
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
Mathematics, 01.11.2020 14:10
question
Social Studies, 01.11.2020 14:10
question
History, 01.11.2020 14:20
question
English, 01.11.2020 14:20
question
English, 01.11.2020 14:20
Questions on the website: 13722363