import java
.util
.ArrayList
;
import java
.util
.LinkedList
;
import java
.util
.List
;
public class Demo {
public static void main(String
[] args
) {
int[][] game
= new int[5][6];
for (int i
= 0; i
< game
.length
; i
++) {
for (int j
= 0; j
< 6; j
++) {
game
[i
][j
] = (int) (Math
.random() * 8) + 1;
}
}
int chess
= game
[0][0];
LinkedList
<Integer> path
= new LinkedList<Integer>();
path
.add(game
[0][0]);
int m
= 0;
int n
= 0;
int[] a
= new int[3];
while (m
<= 4&&n
<=5) {
if (m
<4&&n
< 5) {
a
[0] = game
[m
][n
+ 1];
a
[1] = game
[m
+ 1][n
];
a
[2] = game
[m
+ 1][n
+ 1];
int min
= a
[0];
int index
= 0;
for (int i
= 0; i
< a
.length
; i
++) {
if (min
> a
[i
]) {
min
= a
[i
];
index
= i
;
}
}
if (index
== 0) {
path
.add(a
[0]);
n
= n
+ 1;
} else if (index
== 1) {
path
.add(a
[1]);
m
+= 1;
} else {
path
.add(a
[2]);
n
+= 1;
m
+= 1;
}
}else if(m
<4&&n
==5){
path
.add(game
[m
+1][n
]);
m
+=1;
}else if(n
<5&&m
==4){
path
.add(game
[m
][n
+1]);
n
++;
}else{
break;
}
}
for (int i
= 0; i
< game
.length
; i
++) {
for (int j
= 0; j
< 6; j
++) {
System
.out
.print(game
[i
][j
] + " ");
}
System
.out
.println();
}
System
.out
.println("则最小和是:");
for (int i
= 0; i
< path
.size(); i
++) {
if(i
== path
.size()-1){
System
.out
.print(path
.get(i
));
break;
}
System
.out
.print(path
.get(i
)+"+");
}
}
}
转载请注明原文地址:https://tech.qufami.com/read-14004.html