Professional Documents
Culture Documents
Optimal Path
Optimal Path
* Title: Main_hw6_1.java
* Abstract: This program collects the max amount of coins on an n x m board as
covered in class. It also displays the max coins and the optimal path taken to get
the maximum amount of coins.
* Name: Alex O'Brien
* Date: 2/21/23
*/
import java.util.Scanner;
import java.util.ArrayList;
class Main
{
// global variables
static Scanner kb = new Scanner(System.in);
// functions
// initialize board
static void initializeBoard(int[][] board, int rows, int cols) {
// initialize board with input
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
board[i][j] = kb.nextInt();
}
}
}
// print optimal path with arrows until the last location is reached
for (int i = path.size() - 1; i >= 0; i--) {
if (i == 0)
System.out.println("(" + path.get(i).get(0) + "," +
path.get(i).get(i + 1) + ")");
else {
System.out.print("(" + path.get(i).get(0) + "," +
path.get(i).get(1) + ")");
System.out.print("->");
}
}
}
// driver program
public static void main(String[] args) {
// local variables
int rows = kb.nextInt();
int cols = kb.nextInt();
int[][] board = new int[rows][cols];
int[][] revisedBoard = new int[rows][cols];
ArrayList<ArrayList<Integer>> path = new ArrayList<ArrayList<Integer>>();
// call functions to initialize arrays, find optimal path and print results
initializeBoard(board, rows, cols);
revisedBoard = updateBoard(board, revisedBoard, rows, cols);
findPath(revisedBoard, path);
printResults(revisedBoard, rows, cols, path);
kb.close();
}
}