CST 370 Assignment 6
CST 370 Assignment 6
Homework 6
Due: 02/21/2023 (Tuesday) (11:55 PM)
How to turn in: Write two programs in either C++ or Java and submit them on Canvas before
the due.
• You can submit your programs multiple times before the due. However, the last submission will be
used for grading.
• You have to submit two programs together, especially at your last submission. If you submit, for
example, only one program at the last submission, we are able to see only that program when we
grade your homework.
• Due time is 11:55(PM). Since there could be a long delay between your computer and Canvas, you
should submit it early.
• When you submit your homework program, don’t forget to include "Title", "Abstract", "Name", and
"Date".
• For hw6_1, rename the source file named “main.cpp” to “main_hw6_1.cpp” (for C++) or
rename the source file named “Main.java” to “Main_hw6_1.java” for Java.
• For hw6_2, rename the source file named “main.cpp” to “main_hw6_2.cpp” (for C++) or
rename the source file named “Main.java” to “Main_hw6_2.java” for Java.
• After that, upload the renamed files on Canvas.
• If you submit your source programs without changing the filenames, we can’t
differentiate different programs in a homework.
– Thus, we can’t grade your program properly.
• Please DO NOT change the name of the main procedure (“main()” for C++ and “class
Main” for Java)
1. Write a C++ (or Java) program for hw6_1 to collect maximum number of coins on an n x m board
which was covered in the class.
5 6
0 0 0 0 1 0
0 1 0 1 0 0
0 0 0 1 0 1
0 0 1 0 0 1
1 0 0 0 1 0
The first line (= 5 and 6 in the example) indicates that the board has 5 rows and 6 columns. From the
second line, the configuration of the board is presented. The number 1 indicates that there is a coin on
the cell, while the number 0 means no coin. For the homework, you can assume that the board size is
less than or equal to 25 x 25.
Sample Run 0: Assume that the user typed the following lines
5 6
0 0 0 0 1 0
0 1 0 1 0 0
0 0 0 1 0 1
0 0 1 0 0 1
1 0 0 0 1 0
This is the correct output. Note that the coordinate of the starting cell is (1,1) and the destination is (5,6).
Your program should display maximum coins and path to collect them. When backtracking from the
destination cell, if there is more than one optimal path, your solution should always pick the path from
the left, not from the top.
Max coins:5
Path:(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,5)->(3,6)->(4,6)->(5,6)
Sample Run 1: Assume that the user typed the following lines. Again, when backtracking from the
destination spot, your solution should always pick the path from the left, not from the top, if there is
more than one optimal path.
4 5
0 0 0 1 0
0 1 1 1 0
1 1 1 0 1
0 0 0 0 0
Max coins:4
Path:(1,1)->(2,1)->(3,1)->(3,2)->(3,3)->(3,4)->(3,5)->(4,5)
Sample Run 2: Assume that the user typed the following lines
3 2
1 1
0 0
0 1
Max coins:3
Path:(1,1)->(1,2)->(2,2)->(3,2)
2. Write a C++ (or Java) program for hw6_2 that implements the Floyd algorithm to display all-pairs
shortest paths as we covered in the class.
Input format: This is a sample input from a user.
4
0 -1 3 -1
2 0 -1 -1
-1 7 0 1
6 -1 -1 0
The first line (= 4 in the example) indicates that there are four vertices in the input graph. Then the
following 4 lines present the distances among the vertices. Note that the value –1 indicates the infinity.
For this homework, you can assume that the number of vertices is less than 25.
Sample Run 0: Assume that the user typed the following lines
4
0 -1 3 -1
2 0 -1 -1
-1 7 0 1
6 -1 -1 0
This is the correct output. In the class, we drew all five matrices such as D(0), D(1), D(2), D(3), and D(4). For
the homework, just present the last matrix (= D(4)).
0 10 3 4
2 0 5 6
7 7 0 1
6 16 9 0
Sample Run 1: Assume that the user typed the following lines
3
0 2 -1
-1 0 2
2 -1 0
0 2 4
4 0 2
2 4 0
Sample Run 2: Assume that the user typed the following lines
4
0 10 -1 -1
-1 0 15 -1
-1 -1 0 20
50 -1 -1 0
This is the correct output.
0 10 25 45
85 0 15 35
70 80 0 20
50 60 75 0
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner object = new Scanner(System.in);
int rows = object.nextInt();
int columns = object.nextInt();
int grid[][] = new int[rows][columns];
// loop through user input
for(int i = 0; i < grid.length; i++)
{
for(int j = 0; j < grid[0].length; j++)
{
grid[i][j] = object.nextInt();
}
}
System.out.print("Path:");
while(row < grid.length && col < grid[0].length)
{
System.out.print("(" + (row + 1) + "," + (col + 1)+")");
if(row == grid.length - 1 && col == grid[0].length - 1)
{
System.out.print("");
}
else
{
System.out.print("->");
}
public static int util(int row, int col, int grid[][], int dp[][])
{
if(row >= grid.length || col >= grid[0].length)
{
return 0;
}
if(dp[row][col] != -1)
{
return dp[row][col];
}
if(grid[row][col] == 1)
{
dp[row][col] += 1;
}
return dp[row][col];
}
}
/*
* Title: Main_hw6_2.java
* Abstract: This program implements Floyd's Algorithm displaying
* pairs of short paths
* Name: Adam Momand
* Date: 02/ 16/ 2023
*/
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner object = new Scanner(System.in);
int vertices = object.nextInt();
int distance[][] = new int[vertices][vertices];
}
}
}
//calling print function
Matrix(distance, vertices);
}
public static void Matrix(int distance[][], int vertice)
{
for(int i = 0; i < vertice; i++)
{
for(int j = 0; j < vertice; j++)
{
System.out.print(distance[i][j]);
if(j < vertice - 1)
{
System.out.print(" ");
}
}
if(i < vertice - 1)
{
System.out.println();
}
}
System.out.println();
}
}