0% found this document useful (0 votes)
42 views3 pages

Optimal Path

Uploaded by

api-594670325
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views3 pages

Optimal Path

Uploaded by

api-594670325
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

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

// traverse board and update values of revised board


static int[][] updateBoard(int[][] board, int[][]revisedBoard, int rows, int
cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int distTop = 0;
int distLeft = 0;

// find distance from top and left of board


if (i == 0)
distTop = 0;
else
distTop = revisedBoard[i - 1][j];
if (j == 0)
distLeft = 0;
else
distLeft = revisedBoard[i][j - 1];

// compare to adjacent cells


if (distTop != 0 || distLeft != 0) {
if (distTop >= distLeft)
revisedBoard[i][j] = distTop;
else
revisedBoard[i][j] = distLeft;
}

// increment new board if board location contains coin


if (board[i][j] == 1)
revisedBoard[i][j]++;
}
}
return revisedBoard;
}

// backtrack function to find the optimal path


static void findPath(int[][] board, ArrayList<ArrayList<Integer>> path) {
// variables for ArrayLists and traversal pointers
ArrayList<Integer> rowList = new ArrayList<Integer>();
ArrayList<Integer> colList = new ArrayList<Integer>();
int row = board.length - 1;
int col = board[row].length - 1;
boolean upperLeft = false;

// while not at the end


while (!upperLeft) {
int tempRow = 0;
int tempCol = 0;

// if pointers get to the upper left, end while loop


if (row == 0 && col == 0) {
tempRow = row + 1;
rowList.add(tempRow);
tempCol = col + 1;
colList.add(tempCol);
upperLeft = true;
break;
}

// tempRow/tempCol will add 1 since start is at 1


// traversing through the board left and up
if (row == 0 && col != 0) {
tempRow = row + 1;
rowList.add(tempRow);
tempCol = col + 1;
colList.add(tempCol);
col--;
}
else if (row != 0 && col == 0) {
tempRow = row + 1;
rowList.add(tempRow);
tempCol = col + 1;
colList.add(tempCol);
row--;
} else {
// move left if the left/top cell are equal
if (board[row][col - 1] == board[row - 1][col]) {
tempRow = row + 1;
rowList.add(tempRow);
tempCol = col + 1;
colList.add(tempCol);
col--;
}
// move left if the left cell is greater
else if (board[row][col - 1] > board[row - 1][col]) {
tempRow = row + 1;
rowList.add(tempRow);
tempCol = col + 1;
colList.add(tempCol);
col--;
// else move up
} else {
tempRow = row + 1;
rowList.add(tempRow);
tempCol = col + 1;
colList.add(tempCol);
row--;
}
}
}

// build optimal path using the ArrayLists


for (int i = 0; i < rowList.size(); i++) {
path.add(new ArrayList<Integer>());
}
for (int i = 0; i < rowList.size(); i++) {
path.get(i).add(rowList.get(i));
path.get(i).add(colList.get(i));
}
}

// print max coins and path


static void printResults(int[][] revisedBoard, int rows, int cols,
ArrayList<ArrayList<Integer>> path) {
System.out.println("Max coins:" + revisedBoard[rows - 1][cols - 1]);
System.out.print("Path:");

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

You might also like