@@ -152,66 +152,70 @@ int main() {
152152
153153## 其他语言版本
154154
155- ### Java
155+ ### Java
156+
156157``` Java
158+ import java.util.* ;
159+
157160public class Main {
158- // BFS方法
159- public static int ladderLength (String beginWord , String endWord , List<String > wordList ) {
160- // 使用set作为查询容器,效率更高
161- HashSet<String > set = new HashSet<> (wordList);
162-
163- // 声明一个queue存储每次变更一个字符得到的且存在于容器中的新字符串
164- Queue<String > queue = new LinkedList<> ();
165-
166- // 声明一个hashMap存储遍历到的字符串以及所走过的路径path
167- HashMap<String , Integer > visitMap = new HashMap<> ();
168- queue. offer(beginWord);
169- visitMap. put(beginWord, 1 );
170-
171- while (! queue. isEmpty()) {
172- String curWord = queue. poll();
173- int path = visitMap. get(curWord);
174-
175- for (int i = 0 ; i < curWord. length(); i++ ) {
176- char [] ch = curWord. toCharArray();
177- // 每个位置尝试26个字母
178- for (char k = ' a' ; k <= ' z' ; k++ ) {
179- ch[i] = k;
180-
181- String newWord = new String (ch);
182- if (newWord. equals(endWord)) return path + 1 ;
183-
184- // 如果这个新字符串存在于容器且之前未被访问到
185- if (set. contains(newWord) && ! visitMap. containsKey(newWord)) {
186- visitMap. put(newWord, path + 1 );
187- queue. offer(newWord);
161+ public static void main (String [] args ) {
162+ Scanner scanner = new Scanner (System . in);
163+ int n = scanner. nextInt();
164+ scanner. nextLine();
165+ String beginStr = scanner. next();
166+ String endStr = scanner. next();
167+ scanner. nextLine();
168+ List<String > wordList = new ArrayList<> ();
169+ wordList. add(beginStr);
170+ wordList. add(endStr);
171+ for (int i = 0 ; i < n; i++ ) {
172+ wordList. add(scanner. nextLine());
173+ }
174+ int count = bfs(beginStr, endStr, wordList);
175+ System . out. println(count);
176+ }
177+
178+ /**
179+ * 广度优先搜索-寻找最短路径
180+ */
181+ public static int bfs (String beginStr , String endStr , List<String > wordList ) {
182+ int len = 1 ;
183+ Set<String > set = new HashSet<> (wordList);
184+ Set<String > visited = new HashSet<> ();
185+ Queue<String > q = new LinkedList<> ();
186+ visited. add(beginStr);
187+ q. add(beginStr);
188+ q. add(null );
189+ while (! q. isEmpty()) {
190+ String node = q. remove();
191+ // 上一层结束,若下一层还有节点进入下一层
192+ if (node == null ) {
193+ if (! q. isEmpty()) {
194+ len++ ;
195+ q. add(null );
196+ }
197+ continue ;
198+ }
199+ char [] charArray = node. toCharArray();
200+ // 寻找邻接节点
201+ for (int i = 0 ; i < charArray. length; i++ ) {
202+ // 记录旧值,用于回滚修改
203+ char old = charArray[i];
204+ for (char j = ' a' ; j <= ' z' ; j++ ) {
205+ charArray[i] = j;
206+ String newWord = new String (charArray);
207+ if (set. contains(newWord) && ! visited. contains(newWord)) {
208+ q. add(newWord);
209+ visited. add(newWord);
210+ // 找到结尾
211+ if (newWord. equals(endStr)) return len + 1 ;
188212 }
189213 }
214+ charArray[i] = old;
190215 }
191216 }
192-
193217 return 0 ;
194218 }
195-
196- public static void main (String [] args ) {
197- /* code */
198- // 接收输入
199- Scanner sc = new Scanner (System . in);
200- int N = sc. nextInt();
201- sc. nextLine();
202- String [] strs = sc. nextLine(). split(" " );
203-
204- List<String > wordList = new ArrayList<> ();
205- for (int i = 0 ; i < N ; i++ ) {
206- wordList. add(sc. nextLine());
207- }
208-
209- // wordList.add(strs[1]);
210-
211- // 打印结果
212- int result = ladderLength(strs[0 ], strs[1 ], wordList);
213- System . out. println(result);
214- }
215219}
216220
217221```
0 commit comments