You are on page 1of 3

import java.util.

*;
/*
* INSTRUCTION:
* This is a Java starting code for hw6_1.
* When you finish the development, download this file.
* Note that the current filename is "Main.java".
* But rename it to "Main_hw6_1.java".
* After that, upload the renamed file on Canvas.
*/

// Finish the head comment with Abstract, Name, and Date.


/*
* Title: Main_hw6_1.java
* Abstract:This program takes in the rows and columns of a max coin collecting
problem and calculates the most coins that are possible to collect and also the
path
* Name:Kevin Yoshimoto
* Date:12/13/2022
*/

class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Setup 2 dimension array to take input
int rows = scan.nextInt();
int columns = scan.nextInt();
int[][] maze = new int[rows][columns];
int[][] finished = new int[rows][columns];
int max = 0;

for (int i = 0; i < rows; i++) {


for (int a = 0; a < columns; a++) {
maze[i][a] = scan.nextInt();
}
}

// need to change the if statements to not look for negative index


for (int i = 0; i < rows; i++) {
for (int a = 0; a < columns; a++) {

int value = 0;
if (maze[i][a] == 1) {
value++;
}
if (i == 0 && a == 0) {
finished[i][a] = value;
} else {
if (i == 0 && a > 0) {
value = value + maze[i][a - 1];
}
if (i > 0 && a == 0) {
value = value + maze[i - 1][a];
}

if (i > 0 && a > 0) {


if (finished[i][a - 1] >= finished[i - 1][a]) {
value = value + finished[i][a - 1];
} else {
value = value + finished[i - 1][a];
}
}
}
finished[i][a] = value;
}
}

for (int i = 0; i < rows; i++) {


for (int a = 0; a < columns; a++) {
if (finished[i][a] > max) {
max = finished[i][a];
}
}
}
System.out.println("Max coins:" + max);

// TODO make this start at the end


int i = rows - 1;
int a = columns - 1;
int[] pathR = new int[rows];
int[] pathC = new int[columns];
ArrayList<String> path = new ArrayList();
// TODO Move from the end to the top
while (i >= 0 && a >= 0) {

if(i == 0 && a == 0){


// pathR[i] = i;
// pathC[a] = a;
// String p = "(" + i + "," + a + ")";
// path.add(0, p);
break;
}

if(i == rows -1 && a == columns - 1){


pathR[i] = i + 1;
pathC[a] = a+ 1;
String p = "(" + (i + 1) + "," + (a + 1) + ")";
path.add(0, p);
}
if(i != 0 && a != 0){
if(finished[i][a - 1] >= finished[i - 1][a]){
a--;
pathR[i] = i;
pathC[a] = a;
String p = "(" + (i + 1) + "," + (a + 1) + ")->";
path.add(0, p);
}else{
i--;
pathR[i] = i;
pathC[a] = a;
String p = "(" + (i + 1) + "," + (a + 1) + ")->";
path.add(0, p);
}
}else if(i == 0){
a--;
pathR[i] = i;
pathC[a] = a;
String p = "(" + (i + 1) + "," + (a + 1) + ")->";
path.add(0, p);
}else if(a == 0){
i--;
pathR[i] = i;
pathC[a] = a;
String p = "(" + (i + 1) + "," + (a + 1) + ")->";
path.add(0, p);
}
// if (i == 0 && a == 0) {
// System.out.print("Path:(1,1)->");
// }
// if ((i == rows - 1 && a == columns - 2) || (i == rows - 2 && a == columns
- 1)) {
// System.out.println("(" + (rows) + "," + (columns) + ")");
// break;
// }
// if (i == rows - 1) {
// System.out.print("(" + (i + 1) + "," + (a + 2) + ")->");
// a++;
// } else if (a == columns - 1) {
// System.out.print("(" + (i + 2) + "," + (a + 1) + ")->");
// i++;
// } else if (finished[i][a + 1] >= finished[i + 1][a]) {
// System.out.print("(" + (i + 1) + "," + (a + 2) + ")->");
// a++;
// } else {
// System.out.print("(" + (i + 2) + "," + (a + 1) + ")->");
// i++;
// }
}

System.out.print("Path:");
for(int r = 0; r < path.size(); r++){
System.out.print(path.get(r));
}
System.out.println();
}
}

You might also like