# 求算法，哪位大神给个牛B一点的思路。

0

public static string[] Stops = new string[] { "AB", "BC", "CD", "DC", "DE", "AD", "CE", "EB", "AE" };

Eysa | 初学一级 | 园豆：62

1
``` 1 package cnblogs;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Stack;
6
7 /**
8  * Created by Kischn on 2015/11/21.
9  */
10 public class Test {
11
12     public static int[][] m = new int[26][26];
13     public static void find(Stack<Integer> stack, List<Integer> way, int end){
14         if(stack.isEmpty()) return;
15         Integer tmp = stack.pop();
17         int len = way.size();
18         if(tmp == end){
19             for(int j = 0; j < len - 2; j++){
20                 System.out.print((char)(way.get(j) + 'A'));
21                 System.out.print((char)((way.get(j + 1)) + 'A'));
22                 System.out.print("->");
23             }
24             System.out.print((char)(way.get(len - 2) + 'A'));
25             System.out.print((char)(way.get(len - 1) + 'A'));
26             System.out.println();
27         }else {
28             for (int i = 0; i < 26; i++) {
29                 if (m[tmp][i] == 1) {
30                     stack.push(i);
31                     find(stack, way, end);
32                 }
33             }
34         }
35         way.remove(len - 1);
36     }
37
38     public static void main(String[] args) {
39         String[] Stops = new String[] { "AB", "BC", "CD", "DC", "DE", "AD", "CE", "EB", "AE" };
40         char start = 'A';
41         char end = 'C';
42         char a,b;
43         for(int i = 0; i < Stops.length; i++){
44             a = Stops[i].charAt(0);
45             b = Stops[i].charAt(1);
46             m[a - 'A'][b -'A'] = 1;
47         }
48         List<Integer> way = new ArrayList<Integer>();
49         Stack<Integer> stack = new Stack<Integer>();
50         stack.push(start - 'A');
51         find(stack, way, end - 'A');
52     }
53
54 }```
View Code

兄台，多谢啦。

Eysa | 园豆：62 (初学一级) | 2015-11-22 00:21

@Eysa: 思路是构造的有向图，你的数据得保证不能有环啊~如果符合你要求，给分哦~

@之奇一昂:  m[a - 'A'][b -'A'] = 1;   这句代码是什么意思？  m明明是个int类型的啊，怎么还添加了字符？

Eysa | 园豆：62 (初学一级) | 2015-11-23 19:19

@Eysa: A-Z 分别减'A' 就是 0-26了~这是用数字来表示那些字母就方便多了，ASCII表中A-Z是连续的

0

SharpCJ | 园豆：242 (菜鸟二级) | 2015-11-27 22:23

您需要登录以后才能回答，未注册用户请先注册