用JAVA编写
有一个矩阵
A B C D E F G
A 1 1 0 0 1 0 0
B 1 1 1 0 0 0 0
C 0 1 1 0 0 0 1
D 0 0 0 1 0 1 1
E 1 0 0 0 0 1 0
F 0 0 0 1 1 1 0
G 0 0 1 1 0 0 1
d-->f-->e-->a
d-->g-->c-->b-->a
其中 1 :代表可以走,0:代表不能走,输出 从A到D的路径?
例如:a->b->c->g->d
你这个编号我看不懂啊 小a代表哪个位置
import java.util.Stack;
public class TestMain {
//或者使用char 'a'代表的数字 然后用+1去做
static char[] dic = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
static Stack<Integer> stack = new Stack<Integer>();
public static void main(String args[]) {
int [][] lu = {
{1, 1, 0, 0, 1, 0, 0},
{1, 1, 1, 0, 0, 0, 0},
{0, 1, 1, 0, 0, 0, 1},
{0, 0, 0, 1, 0, 1, 1},
{1, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 1, 1, 1, 0},
{0, 0, 1, 1, 0, 0, 1}};
//计算从a到d点的方法
find(0, 3, lu);
}
private static void find(int from, int to, int[][] lu) {
stack.push(from);
//如果i j直达 则查找结束
if (lu[from][to] == 1) {
stack.push(to);
print();
stack.pop();
return;
} else {
for (int index=0; index<lu[from].length; index++) {
if (lu[from][index] == 1 && !stack.contains(index)) {
find(index, to, lu);
}
}
}
stack.pop();
}
static void print() {
String result = "";
for (Integer i:stack) {
result += dic[i] + "->";
}
System.out.println(result.substring(0, result.length() - 2));
}
}