Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. /*
  2.  * File: WordLadder.cpp
  3.  * --------------------
  4.  * Name: [TODO: enter name here]
  5.  * Section: [TODO: enter section leader here]
  6.  * This file is the starter project for the word ladder problem
  7.  * on Assignment #2.
  8.  * [TODO: extend the documentation]
  9.  */
  10.  
  11. #include <iostream>
  12. //#include <fstream>
  13. #include <queue>
  14. #include <vector>
  15. #include <set>
  16.  
  17. #include "console.h"
  18. #include "simpio.h"
  19. #include "lexicon.h"
  20.  
  21. using namespace std;
  22.  
  23. typedef vector <string> svector;
  24.  
  25. //svector lexicon;
  26.  
  27. // easier for debugging
  28. ostream& operator <<(ostream& o, const svector& l) {
  29.     for (int i = 0; i < l.size(); i++) {
  30.         o << l[i] << " ";
  31.     }
  32.  
  33.     o << endl;
  34.  
  35.     return o;
  36. }
  37.  
  38. svector find_ladder(const string& head_word, const string& tail_word) {
  39.     queue <svector> ladder_queue;
  40.     set <string> is_word_used;
  41.  
  42.     svector initial_ladder;
  43.     initial_ladder.push_back(head_word);
  44.  
  45.     ladder_queue.push(initial_ladder);
  46.  
  47.     while (!ladder_queue.empty()) {
  48.         svector current_ladder = ladder_queue.front(); // get front svector reference
  49.         ladder_queue.pop();
  50.  
  51.         if (current_ladder[current_ladder.size() - 1] == tail_word) {
  52.             return current_ladder;
  53.         }
  54.     }
  55.  
  56.     return initial_ladder;
  57. }
  58.  
  59. int main() {
  60.     Lexicon lexicon("EnglishWords.dat");
  61.  
  62.     cout << lexicon.size() << endl;
  63.  
  64.     string head_word, tail_word;
  65.  
  66.     /*while (true) {
  67.         cout << "Enter start word (RETURN to quit) : ";
  68.         cin >> head_word;
  69.  
  70.         if (head_word == "") break;
  71.  
  72.         cout << "Enter destination word: ";
  73.         cin >> tail_word;
  74.  
  75.         cout << find_ladder(head_word, tail_word) << endl;
  76.     }*/
  77.  
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement