终于写完了,大牛们见笑了。
require 'set'
$dict=Set.new ['fox','fog','dog']
def indict?(word)
$dict.include? word
end
def transfer(first,last)
queue=[first]
hash={first=>0}
until queue.empty?
word=queue.shift
step=hash[word]
(0...word.length).each do |i|
curr=word[i]
('a'..'z').each do |c|
word[i]=c
if(word==last)
return step+1
elsif indict?(word) && !hash[word]
hash[word]=step+1
queue<end
end
word[i]=curr
end
end
-1
end
p transfer('fox','dog')