A word ladder is a game in which you start with a word and change one letter at
ID: 3560124 • Letter: A
Question
A word ladder is a game in which you start with a word and change one letter at a time until arriving at an end word. Each word along the way must be an english word.
For example, starting from FISH you can make a word ladder to MAST through the following ladder:
FISH, WISH, WASH, MASH, MAST
Write a program using recursion to find the word ladder given a start word and an end word, or determines if no word ladder exists. Use the file "words.txt" as a dictionary of valid words. This file contains 87314 words. Your program does not need to find the shortest word ladder between words, any word ladder will do if one exists.
Explanation / Answer
import java.util.Arrays; import java.util.ArrayList; import java.util.Queue; import java.util.LinkedList; import java.util.Stack; class WordLadder { static ArrayList words=new ArrayList(); public static void main(String[] args) { EasyReader dictFile=new EasyReader("C:\dict.txt"); EasyReader input=new EasyReader(); String o; while ((o = dictFile.readWord()) != null) { words.add(o.toLowerCase()); } System.out.println("What is the starting word?"); String start=input.readLine(); System.out.println("What is the ending word?"); String end=input.readLine(); if(start.length()!=end.length()) { System.out.println("The words have to be the same length!"); System.exit(0); } boolean isfound=false; LinkedList q = new LinkedList(); LinkedList procq = new LinkedList(); Stack starter=new Stack(); starter.push(start); q.add(starter); ArrayList used=new ArrayList(); used.add(start); while(!isfound) { Stack process=(Stack)q.removeLast(); System.out.println("finding one-offs for "+process.peek()); ArrayList used); System.out.println(oneoffs); if(!oneoffs.isEmpty()) { for(int i=0;iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.