← 返回 bytedance 的题目列表Word Ladder (Shortest Transformation Path)
类型:online_judge
Problem (Coding)
Given a beginWord, an endWord, and a dictionary wordList (lowercase words). In one move you may change exactly one character, and the resulting word must exist in wordList.
Output a shortest transformation sequence from beginWord to endWord (including both endpoints). If no such sequence exists, print an empty line.
Input (stdin)
Line 1: beginWord
Line 2: endWord
Line 3: integer k (dictionary size)
Next k lines: one word each
Output (stdout)
If exists: print the sequence in one line separated by spaces
Else: print an empty line
Constraints
1 <= len(word) <= 10
1 <= k <= 5e4
All words have the same length
Testcases
(see CN version)
Example
Input
hit
cog
6
hot
dot
dog
lot
log
cog
Output
hit hot dot dog cog