These are my three input text files. Input A 6 10 50 5 12 27 23 16 38 44 30 15 3
ID: 3743108 • Letter: T
Question
These are my three input text files.
Input A
6
10 50 5 12 27
23 16 38 44
30 15 33
33 43
12
Input B
6
15 20 34 24 1
23 35 23 15
35 24 33
12 30
44
Input C
6
1 25 35 23 50
1 33 44 20
1 34 44
1 24
1
Explanation / Answer
CODE:
CanoeAndPosts.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class CanoeAndPosts {
public static int Canoe(int A[][]) {
/*the solutions matrix*/
int[][] T = new int[A.length][A[0].length];
/*Let the first row of T be the first row of A[][]*/
for(int i = 0; i < A.length; i++) {
T[0][i] = A[0][i];
}
/*Dynamically incur solution into T*/
for(int i = 1; i < A.length; i++) {
for(int j = 0; j < A.length; j++) {
if (j <= i) {
T[i][j] = T[i - 1][j];
} else {
T[i][j] = Math.min(T[i - 1][j], T[i - 1][i] + A[i][j]);
}
}
}
return T[T.length - 1][T.length - 1];
}
public static void main (String[] args) {
int min = 0;
try {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the input file name with path:");
String fileName = sc.nextLine();
BufferedReader bufReader = new BufferedReader(new FileReader(fileName));
String fileRead = bufReader.readLine();
int n = Integer.parseInt(fileRead);
int arrayCanoe[][] = new int[n][n];
String[] fileRead2 = new String[n];
for(int i=0;i<n;i++)
fileRead2[i] = bufReader.readLine();
for(int i=0;i<n;i++) {
try {
String[] array = fileRead2[i].split(" ");
//for(int j=0;j<n;j++)
//System.out.println(array[j]);
for(int k=0;k<n;k++) {
if(array[k].equals("-")) {
arrayCanoe[i][k] = 0; }
else {
arrayCanoe[i][k] = Integer.parseInt(array[k]);
}
//System.out.println(arrayCanoe[i][k]);
}
}catch(Exception e) {}
}
min = Canoe(arrayCanoe);
System.out.println("The min cost is :"+min);
sc.close();
bufReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Input.txt File :
4
- 10 15 50
- - 40 20
- - - 35
Sample Output :
Enter the input file name with path: D:/Input.txt The min cost is :30
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.