<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    小明思考

    Just a software engineer
    posts - 124, comments - 36, trackbacks - 0, articles - 0
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    字梯游戲II

    Posted on 2013-04-18 17:32 小明 閱讀(1364) 評論(0)  編輯  收藏 所屬分類: 數據結構和算法
    問題給定兩個單詞(一個開始,一個結束)和一個字典,找出所有的最短的從開始單詞到結束單詞的變換的序列(可能不止一個),并滿足:

    1.每次只能變換一個字母
    2.所有的中間單詞必須存在于字典中

    比如:
    輸入:
    start = "hit"
    end = "cog"
    dict = ["hot","dot","dog","lot","log"]

    那么最短的變化序列有兩個
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]。
    注意:
    1. 所有單詞的長度都是相同的
    2. 所有單詞都只含有小寫的字母。

    分析:
    跟之前的字梯游戲相比,這個問題要求求出所有的最短序列,所以要使用一個Prev List來記錄前驅節點(可能不止一個)。這樣根據這個Prev List就可以遍歷出所有的最短組合。

    代碼如下:

    public class WordLadder2 {
        private List<List<Integer>> prev;
        private String[] allWords;

        private boolean canChange(String a, String b) {
            int diff = 0;
            int len = a.length();
            for (int i = 0; i < len; ++i) {
                if (a.charAt(i) != b.charAt(i)) {
                    ++diff;
                    if (diff > 1) {
                        return false;
                    }
                }
            }
            return true;
        }

        private ArrayList<ArrayList<String>> perm(int node) {
            ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
            String current = allWords[node];
            if (node == 0) {
                ArrayList<String> as = new ArrayList<String>();
                as.add(current);
                result.add(as);
            } else {
                List<Integer> p = prev.get(node);
                for (Integer pnode : p) {
                    ArrayList<ArrayList<String>> subResult = perm(pnode.intValue());
                    for (ArrayList<String> as : subResult) {
                        as.add(current);
                        result.add(as);
                    }
                }
            }
            return result;
        }

        public ArrayList<ArrayList<String>> findLadders(String start, String end,
                HashSet<String> dict) {
            allWords = new String[dict.size() + 2];
            int t = 0;
            allWords[t++] = start;
            for (String d : dict) {
                allWords[t++] = d;
            }
            allWords[t++] = end;
            int size = allWords.length;
            boolean[][] matrix = new boolean[size][size];
            for (int i = 0; i < size; ++i) {
                String si = allWords[i];
                for (int j = i + 1; j < size; ++j) {
                    String sj = allWords[j];
                    if (canChange(si, sj)) {
                        matrix[i][j] = matrix[j][i] = true;
                    }
                }
            }

            int[] cost = new int[size];
            prev = new ArrayList<List<Integer>>();
            for (int i = 0; i < size; ++i) {
                cost[i] = Integer.MAX_VALUE;
                prev.add(new ArrayList<Integer>());
            }
            cost[0] = 0;
            List<Integer> openlist = new ArrayList<Integer>();
            openlist.add(0);
            while (!openlist.isEmpty()) {
                int current = openlist.remove(openlist.size() - 1);
                boolean[] mn = matrix[current];
                int cc = cost[current];
                for (int i = 0; i < size; ++i) {
                    if (mn[i]) {
                        if (cost[i] > cc + 1) {
                            cost[i] = cc + 1;
                            prev.get(i).clear();
                            prev.get(i).add(current);
                            openlist.add(0, i);
                        } else if (cost[i] == cc + 1) {
                            prev.get(i).add(current);
                        }
                    }
                }
            }

            if (cost[size - 1] != Integer.MAX_VALUE) {
                return perm(size - 1);
            } else {
                return new ArrayList<ArrayList<String>>();
            }
        }
    }

    主站蜘蛛池模板: 成人片黄网站A毛片免费| 精品无码一级毛片免费视频观看 | 一二三四在线观看免费中文在线观看| 国产高清不卡免费在线| 亚洲精品亚洲人成在线观看麻豆| 99精品视频在线观看免费播放 | 亚洲毛片免费视频| 亚洲一级免费视频| 久草在视频免费福利| 亚洲综合av一区二区三区 | 国产高清在线精品免费软件| 亚洲色成人四虎在线观看| 精品久久久久久久免费加勒比| 亚洲精品久久无码| 亚洲精品偷拍视频免费观看| 国产V片在线播放免费无码 | 免费av一区二区三区| 久久久久亚洲AV无码专区体验| 免费在线视频你懂的| 精品国产日韩久久亚洲| 免费一级肉体全黄毛片| 中文字幕乱理片免费完整的| 人人狠狠综合久久亚洲婷婷| 国产免费不卡视频| 日韩在线一区二区三区免费视频| 亚洲韩国精品无码一区二区三区| 亚洲美女免费视频| 国产成人综合亚洲一区| 老司机亚洲精品影视www| 1000部啪啪毛片免费看| 精品亚洲福利一区二区| 亚洲va中文字幕无码久久| 中文字幕无码不卡免费视频| 美景之屋4在线未删减免费| 亚洲av综合avav中文| 成人免费视频网址| 久久午夜夜伦鲁鲁片无码免费| 亚洲乱色伦图片区小说| 国产A在亚洲线播放| 日本一道一区二区免费看 | 最近国语视频在线观看免费播放|