Word Loadder II (***) -- BFS DFS

来源:互联网 发布:诚邀辣妹 网络性与爱 编辑:程序博客网 时间:2024/04/28 16:44

题目 : 和 word Loadder 不同的是他要列出所有最短的路径

思路 :
1. BFS生成从start 到 end 节点与高度的map
2. DFS从 end节点,递归遍历到start, 找出所有可能的路径

代码来自 : Leetcode Word Ladder II 解题报告

public class Solution {      //记录每个单词所在的层数      HashMap<String,Integer> path = new HashMap<String,Integer>();      //bfs生成path      void bfs(String start, String end, HashSet<String> dict) {          Queue queue = new LinkedList<String>();          queue.add(start);          path.put(start,0);          String current;          while(!queue.isEmpty()) {              current = (String)queue.poll();              if(current==end) {                  continue;              }              for(int i=0;i<current.length();i++) {                  char[] strCharArr = current.toCharArray();                  for(char ch='a';ch<='z';ch++) {                      if(strCharArr[i]==ch) {                          continue;                      }                      strCharArr[i] = ch;                      String newWord = new String(strCharArr);                      if(newWord.equals(end)==true||dict.contains(newWord)) {                          //每个单词在path中只能出现一次,也就是每个单词只能出现在一层中,这样就很巧妙的解决了环的问题。              if(path.get(newWord)==null) {                              int depth = (int)path.get(current);                              path.put(newWord,depth + 1);                              queue.add(newWord);                          }                      }                  }              }          }      }      //从目标单词往回找开始单词,记录所有路径      void dfs(String start, String end, HashSet<String> dict, ArrayList<String> pathArray,ArrayList<ArrayList<String>> result) {          //找到了,需要reverse加入的所有单词      if(start.equals(end)==true) {              pathArray.add(start);              Collections.reverse(pathArray);              result.add(pathArray);              return;          }          if(path.get(start)==null) {              return;          }          pathArray.add(start);          int nextDepth = (int)path.get(start) - 1;          for(int i=0;i<start.length();i++) {              char[] strCharArr = start.toCharArray();              for(char ch='a';ch<='z';ch++) {                  if(strCharArr[i]==ch) {                      continue;                  }                  strCharArr[i] = ch;                  String newWord = new String(strCharArr);          //只相差一个字母同时这个单词所在的层数也是当前单词的上一层                  if(path.get(newWord)!=null&&(path.get(newWord)==nextDepth)) {                 //这里是一个新的ArrayList对象,这里就像一个分支点                     ArrayList<String> newPathArray = new ArrayList<String>(pathArray);                      dfs(newWord,end,dict,newPathArray,result);                  }              }          }      }      public ArrayList<ArrayList<String>> findLadders(String start, String end, HashSet<String> dict) {          ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();          ArrayList<String> path = new ArrayList<String>();          if(start==null||end==null||start.length()!=end.length()) {              return result;          }          bfs(start, end, dict);          dfs(end,start, dict, path, result);          return result;      }  }  
0 0
原创粉丝点击