-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCutSteelBar.java
More file actions
92 lines (73 loc) · 2.15 KB
/
Copy pathCutSteelBar.java
File metadata and controls
92 lines (73 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package algorithmAssignments;
public class CutSteelBar {
// public class cutBar{
// int start = -1;
// int end = -1;
// public cutBar(int start, int end) {
// this.start = start;
// this.end = end;
// }
// }
public static int calculateValue(int[] Bar, int s, int e) {
if(e - s == 1) {
return 0;
}
else {
return Bar[e] - Bar[s];
}
}
public static void cutMethod(int[] Bar, int[][] value, int[][] s) {
int lenth = Bar.length - 1;
for(int i = 0; i < lenth; i++) value[i][i] = -1; //meaningless, protect
for(int i = 0; i < lenth - 1; i++) {
value[i][i + 1] = 0;
}//boundary condition
for(int r = 2; r < lenth; r++) { //
for(int i = 0; i + r <= lenth; i++) {
int j = i + r;
for(int k = i + 1; k < j; k++) { //get min value between i and j
int temp = value[i][k] + value[k][j] + calculateValue(Bar, i, j);
if(temp < value[i][j]) {
value[i][j] = temp;
s[i][j] = k;
}
System.out.println("j "+ j + " temp "+ temp);
}
System.out.println("jj "+ j);
}
}
System.out.println("value");
for(int i = 0; i < lenth; i++) {
for(int j = 0; j < lenth; j++) {
System.out.print(value[i][j] + " ");
}
System.out.println("");
}
System.out.println("s");
for(int i = 0; i < lenth; i++) {
for(int j = 0; j < lenth; j++) {
System.out.print(s[i][j] + " ");
}
System.out.println("");
}
}
public static void main(String[] args) throws Exception {
// int[] SteelBar = new int[] {0,5,10,12,13,14,18,19};
int[] SteelBar = new int[] {0,1,2,3,44,45,46,47}; //95
int[][] price = new int[SteelBar.length][SteelBar.length];
int[][] sequence = new int[SteelBar.length][SteelBar.length];
for(int i = 0; i < SteelBar.length; i++) {
for(int j = 0; j < SteelBar.length; j++) {
// price[i][j] = Integer.MAX_VALUE;
price[i][j] = 1000;
}
}
// for(int i = 0; i < 6; i++) {
// for(int j = 0; j < 6; j++) {
// System.out.print(price[i][j] + " ");
// }
// System.out.println("");
// }
cutMethod(SteelBar, price, sequence);
}
}