私はすでにこの問題に多くの労力を費やしており、本当に終わりに近づいています。全体的な目標は、ラダーの各「ラング」が前の単語とは 1 文字異なる 2 つの 5 文字の単語の間に最小長の単語ラダーを作成することでした。例えば:
[heads, heals, hells, halls, hails, tails]
プログラムは、最初と最後の単語、および必要なはしごの長さを入力する必要がある場所から開始し、プログラムはそれを解決する必要があります。私はすでにかなり進んでいるので、現在の状況を説明するために詳細の多くを割愛します.
たとえば、「babes」から「child」へと移行し、10 文字の単語のはしごを探しているとします。
私は何千もの単語のペアを持っています.2つのペアは互いに1つの文字が異なります. 以下はペアのほんの一部です。
[(bares, babes), (banes, babes), (bates, babes), (babel, babes), (bases, babes), (bales, babes)...] etc.
これは長い間続きますが、そこには目的の単語が存在し、最初の単語 (babes) と最後の単語 (child) の間にパスがあり、そのはしごが 10 単語であることが保証されています。長いです。
どうすればこれを達成できますか?
編集:私はすでにグラフを実装しており、BFS を使用して開始単語から終了単語に移動しています。
public List<T> minLengthPath(T src, T dest, int length)
{
T start = src;
Deque<T> queue = new LinkedList<T>(); //Holds items to visit
Queue<List<T>> ladder = new LinkedList<List<T>>(); //Holds all the ladders?
Set<T> checker = new HashSet<T>(); //Holds visited items
queue.add(start);
checker.add(start);
while(!queue.isEmpty()){
T slot = queue.remove();
if(slot.equals(dest))
{
System.out.println(slot);
return null; //Should be returning ladder
}
Set<Pair<Integer, T>> thing = this.edges.get(slot);
Set<T> edges = findEdges(thing); //Returns the edges of the node
Iterator<T> check = edges.iterator();
for(int a = 0; a < edges.size(); a ++)
{
T hole = check.next();
if(!checker.contains(hole))
{
checker.add(hole);
queue.add(hole);
}
}
}
return null;
}