Advertisement
carlos1993

Word Ladder

Dec 26th, 2013
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. /*
  2.  * File:   main.cpp
  3.  * Author: Carlos Arturo Alaniz Galvan
  4.  *
  5.  * Word Ladder game, using an undirected graph and BFS
  6.  * Created on December 26, 2013, 9:09 AM
  7.  */
  8. #include <cstdlib>
  9. #include <unordered_map> //Hash table
  10. #include <list> //Queue Stack
  11. #include <fstream>
  12. #include <iostream>
  13. using namespace std;
  14. typedef list<string> str_list;
  15. typedef unordered_map<string, str_list> adj_list;
  16. typedef unordered_map<string, string> visited;
  17.  
  18. adj_list* load(string file_name = "dic.txt") {
  19.     adj_list *word_map = new adj_list;
  20.     string word;
  21.     ifstream file(file_name.c_str());
  22.     while (getline(file, word)) {
  23.         word_map->insert(pair<string, str_list>(word, list<string>()));
  24.     };
  25.     file.close();
  26.     return word_map;
  27. }
  28.  
  29. void preprocess(adj_list* word_map, string file_name = "dic.txt") {
  30.     ifstream file(file_name.c_str());
  31.     string word;
  32.     string other_word;
  33.     int per = 0;
  34.     int b = 0;
  35.     while (getline(file, word)) {
  36.         for (int i = 0; i < word.size(); i++) {
  37.             other_word = word;
  38.             for (int j = 97; j <= 122; j++) {
  39.                 other_word[i] = static_cast<char> (j);
  40.                 if (word_map->count(other_word) > 0 && other_word != word) {
  41.                     word_map->at(word).push_back(other_word);
  42.                 }
  43.             }
  44.         }
  45.         per++;
  46.     }
  47.     file.close();
  48. };
  49.  
  50. void print_adj(string word, adj_list* word_map) {
  51.  
  52.     str_list::iterator i;
  53.     for (i = word_map->at(word).begin(); i != word_map->at(word).end(); i++) {
  54.         cout << *i << "  ";
  55.     }
  56. };
  57.  
  58. void print_path(str_list& p) {
  59.     str_list::iterator i;
  60.     while (!p.empty()) {
  61.         cout << p.back() << endl;
  62.         p.pop_back();
  63.     }
  64.  
  65. };
  66.  
  67. bool BFS(string start, string end, adj_list* word_map) {
  68.     if (word_map->count(start) <= 0 ||
  69.             word_map->count(end) <= 0)
  70.         return 0;
  71.     visited visited_nodes;
  72.     str_list word_queue;
  73.     str_list path;
  74.     string word;
  75.     str_list::iterator i;
  76.     visited_nodes.insert(pair<string, string>(start, start));
  77.     word_queue.push_back(start);
  78.     while (!word_queue.empty()) {
  79.         word = word_queue.front();
  80.         //cout << word << "  ";
  81.         word_queue.pop_front();
  82.         if (word == end) {
  83.             while (word != start) {
  84.                 path.push_back(word);
  85.                 word = visited_nodes.at(word);
  86.             }
  87.             path.push_back(word);
  88.             print_path(path);
  89.             return 1;
  90.         }
  91.         for (i = word_map->at(word).begin(); i != word_map->at(word).end(); i++) {
  92.             if (visited_nodes.count(*i) <= 0) {
  93.                 visited_nodes.insert(pair<string, string>(*i, word));
  94.                 word_queue.push_back(*i);
  95.             }
  96.         }
  97.     }
  98.     return 0;
  99. };
  100.  
  101. int main() {
  102.     cout << "loading dictionary into Hash Table..." << endl;
  103.     adj_list* word_map = load();
  104.     cout << "loaded :)" << endl;
  105.     cout << "loading graph..." << endl;
  106.     preprocess(word_map);
  107.     cout << "loaded :)" << endl;
  108.     string start, end;
  109.     while (1) {
  110.         cout << "Type the starting word. or '999' to exit: ";
  111.         cin >> start;
  112.         if (start == "999")
  113.             break;
  114.         cout << "Type the goal word: ";
  115.         cin >> end;
  116.         if (!BFS(start, end, word_map)) {
  117.             cout << start << " is either not in the dictionary or cannot be transformed into any other word.";
  118.         };
  119.         cout << endl;
  120.     }
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement