The word ladder game was invented by Lewis Carroll in 1877. The idea is to begin
ID: 3574103 • Letter: T
Question
The word ladder game was invented by Lewis Carroll in 1877. The idea is to begin with a start 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 that uses 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 that is available online with the source code for the book as your 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. This is from Walter Savitch, Java: An introduction to problem solving and programming. It's from Chapter 11, number 8. Please solve this RECURSIVELY using relatively simple methods. This is a very simple program for an intro to Java course. It does not need any fancy techniques like nodes and regex and all that, it just needs to be done RECURSIVELY.
Explanation / Answer
Solution:
/*
The word ladder game was invented by Lewis Carroll in 1877. The idea is to begin with a start 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 that uses 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 that is available online with the source code for the book as your 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. This is from Walter Savitch, Java: An introduction to problem solving and programming. It's from Chapter 11, number 8. Please solve this RECURSIVELY using relatively simple methods. This is a very simple program for an intro to Java course. It does not need any fancy techniques like nodes and regex and all that, it just needs to be done RECURSIVELY. */
#include<fstream>
#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
using namespace std;
/*
determine if a bridge can be formed between two strings
*/
bool possible_bridge(string a, string b)
{
if (a.length()!=b.length())
{
return false;
}
bool difference=false;
for (int i=0;i<a.length();i++)
{
if ((a[i]!=b[i])&&(!difference))
{
difference=true;
}
else if ((a[i]!=b[i])&&(difference))
{
return false;
}
}
return true;
}
/*
determine if there is already an indentical string in the vector
*/
bool isRepeat(vector<string>& no_repeat,string word)
{
for (int i=0;i<no_repeat.size();i++)
{
if (no_repeat[i]==word)
{
return true;
}
}
return false;
}
void word_bridge(string last,vector<string>bridge[],bool& found,int size)
{
ifstream fin;
int count[size];
for (int i=0;i<size;i++)
{
count[i]=0;
}
for (int i=0;i<size;i++)
{
string word;
fin.open("text.dat");
while (fin>>word)
{
if ((possible_bridge(word,bridge[i][bridge[i].size()-1]))&&(!isRepeat(bridge[i],word)))
{
count[i]++;
}
}
fin.close();
}
int total=0;
for (int i=0;i<size;i++)
{
total+=count[i];
}
if (total==0)
{
cout<<"no ladder available ";
return;
}
int n(0),i(0);
vector<string>temp[total];
for (int k=0;k<total;k++)
{
temp[k]=bridge[i];
n++;
if (n==count[i])
{
i++;
n=0;
}
}
n=0;
i=0;
string word;
fin.open("text.dat");
for (int k=0;k<total;k++)
{
do
{
fin>>word;
} while (!((possible_bridge(word,temp[k][temp[k].size()-1]))&&(!isRepeat(temp[k],word))));
temp[k].push_back(word);
n++;
if (n==count[i])
{
i++;
n=0;
fin.close();
fin.open("text.dat");
}
}
if (fin.is_open())
{
fin.close();
}
for (int i=0;i<total;i++)
{
if (temp[i][temp[i].size()-1]==last)
{
for (int k=0;k<temp[i].size();k++)
{
found=true;
cout<<temp[i][k]<<" ";
}
return;
}
}
if (!found)
{
word_bridge(last,temp,found,total);
}
}
int main()
{
vector<string>bridge[1];
bridge[0].push_back("ade");
bool found=false;
word_bridge("bqe",bridge,found,1);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.