Computers and Technology
Computers and Technology, 14.05.2021 03:10, jaileen84

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));
}
}

answer
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 18:00, abbygriffin2009
Martha is a healer, a healthcare provider, and an experienced nurse. she wants to share her daily experiences, as well as her 12 years of work knowledge, with people who may be interested in health and healing. which mode of internet communication can martha use?
Answers: 3
image
Computers and Technology, 23.06.2019 16:30, isaiahhuettnerowgg8d
What is one reason why indoor air pollution has become an increasing problem.
Answers: 1
image
Computers and Technology, 23.06.2019 21:40, minnie7760
Draw the resistor’s voltage and current phasors at t=15ms. draw the vectors with their tails at the origin. the orientation of your vectors will be graded. the exact length of your vectors will not be graded.
Answers: 2
image
Computers and Technology, 24.06.2019 03:30, ilovewaffles70
Auniform resource locator (url) is a formatted string of text that web browsers, email applications, and other software programs use to identify a particular resource on the internet. true false
Answers: 2
Do you know the correct answer?
A board has n*m cells, and each cell has a value (positive or negative). The game is to start from t...

Questions in other subjects: