Advertisement
madras

Untitled

Apr 30th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. //#include <conio.h>
  4. #include <vector>
  5. #include <cmath>
  6. #include <fstream>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11. string  matrix[4];
  12. bool    matrix2[4][4];
  13.  
  14. struct v_2d // two-dimensional integer vector
  15. {
  16.     int x;
  17.     int y;
  18. };
  19.  
  20. vector < vector <v_2d> > kmb_slowo;
  21. vector < vector <v_2d> > slowo_kombinacje;
  22.  
  23. vector <int> ilosc_liter_pos; //[LENGTH] = {3,2,2,1,3};
  24. vector <int> przypadki; //[LENGTH];
  25.  
  26. bool compareLen(const string& a, const string& b)
  27. {
  28.     return (a.size() > b.size());
  29. }
  30.  
  31. void matrix_reset()
  32. {
  33.     for(int i = 0; i < 4; i++)
  34.     {
  35.         for(int j = 0; j < 4; j++)
  36.             matrix2[i][j] = true;
  37.     }
  38. }
  39.  
  40. bool matrix_search(char literka)
  41. {
  42.     for(int i = 0; i < 4; i++)
  43.     {
  44.         for(int j = 0; j < 4; j++)
  45.         {
  46.             if(matrix[i][j] == literka && matrix2[i][j])
  47.             {
  48.                 matrix2[i][j] = false;
  49.                 return true;
  50.             }
  51.         }
  52.     }
  53.  
  54.     return false;
  55. }
  56.  
  57. vector <v_2d> matrix_search2(char literka)
  58. {
  59.     vector <v_2d> vc_temp;
  60.     for(int i = 0; i < 4; i++)
  61.     {
  62.         for(int j = 0; j < 4; j++)
  63.         {
  64.             if(matrix[i][j] == literka)
  65.             {
  66.                 v_2d c_temp = {i, j};
  67.                 vc_temp.push_back(c_temp);
  68.             }
  69.         }
  70.     }
  71.     return vc_temp;
  72. }
  73.  
  74. bool inRange(v_2d c1, v_2d c2)
  75. {
  76.     float dist = sqrt((float)(c1.x-c2.x)*(c1.x-c2.x) + (c1.y-c2.y)*(c1.y-c2.y));
  77.  
  78.     if(dist < 1.5 && dist > 0.1) // interval: (0; sqrt2>
  79.         return true;
  80.  
  81.     return false;
  82. }
  83.  
  84. bool check_pattern(vector <v_2d> kandydat)
  85. {
  86.     matrix_reset();
  87.  
  88.     for(int i = 0; i < (int)kandydat.size() - 1; i++)
  89.     {
  90.         matrix2[kandydat[i].x][kandydat[i].y] = false;
  91.  
  92.         if(!inRange(kandydat[i], kandydat[i + 1]) || !matrix2[kandydat[i + 1].x][kandydat[i + 1].y])
  93.             return false;
  94.     }
  95.  
  96.     return true;
  97. }
  98.  
  99. void generate(int s, int length)
  100. {
  101.     //length - dl. slowa
  102.     //s - iterator, wart. poczatkowa == 0
  103.  
  104.     if(s > length - 1)
  105.         return;
  106.    
  107.     for(int i = 0; i < ilosc_liter_pos[s]; i++)
  108.     {
  109.         przypadki[s] = i;
  110.         generate(s + 1, length);
  111.  
  112.         if(s == length - 1)
  113.         {
  114.             vector <v_2d> kandydat;
  115.  
  116.             for(int k = 0; k < length; k++)
  117.             {
  118.                 // DONE: #TODO: przypadki[k]
  119.                 kandydat.push_back(kmb_slowo[k][przypadki[k]]);
  120.             }
  121.  
  122.             slowo_kombinacje.push_back(kandydat);
  123.         }
  124.     }
  125. }
  126.  
  127. int main()
  128. {
  129.     ios::sync_with_stdio(false);
  130.     vector <string> words;  // wszystkie
  131.     vector <string> words2; // pierwszy sort
  132.     vector <string> words3; // rezultat
  133.  
  134.     fstream plik1;
  135.     plik1.open("slo.txt", ios::in);
  136.  
  137.     while(!plik1.eof())
  138.     {
  139.         string temp;
  140.         getline(plik1, temp);
  141.         words.push_back(temp);
  142.     }
  143.     plik1.close();
  144.  
  145.     //cout << "(C)2014 Marcin Waszak\n";
  146.     //cout << "OSZUKISTA 0.1 PRE-ALPHA\n\n";
  147.     //cout << "Podaj 4 rzedy:\n\n";
  148.     int n;
  149.     cin >> n;
  150.  
  151.     for(int k = 0; k < n; k++)
  152.     {
  153.         cin >> matrix[0];
  154.         cin >> matrix[1];
  155.         cin >> matrix[2];
  156.         cin >> matrix[3];
  157.  
  158.         //matrix[0] = "fasf";
  159.         //matrix[1] = "kkkk";
  160.         //matrix[2] = "fawf";
  161.         //matrix[3] = "xxxx";
  162.  
  163.         //cout << matrix[0] << endl;
  164.         //cout << matrix[1] << endl;
  165.         //cout << matrix[2] << endl;
  166.         //cout << matrix[3] << endl;
  167.  
  168.         for(int i = 0; i < (int)words.size(); i++) // nowy wyraz
  169.         {
  170.             matrix_reset();
  171.  
  172.             for(int j = 0; j < (int)words[i].size(); j++) // bierzemy literk©
  173.             {
  174.                 if(!matrix_search(words[i][j]))
  175.                     break;
  176.  
  177.                 if(j == (int)words[i].size() - 1)
  178.                     words2.push_back(words[i]);
  179.             }
  180.         }  
  181.  
  182.         // OGARNIACZ
  183.         for(int i = 0; i < (int)words2.size(); i++) // bierze kolejne sˆowo ##
  184.         {
  185.             kmb_slowo.clear();
  186.             slowo_kombinacje.clear();
  187.  
  188.             ilosc_liter_pos.clear();
  189.             przypadki.clear();
  190.  
  191.             przypadki.resize(words2[i].size());
  192.  
  193.             for(int j = 0; j < (int)words2[i].size(); j++)
  194.             {
  195.                 vector <v_2d> c_temp1;
  196.                 c_temp1 = matrix_search2(words2[i][j]); // wszystkie warianty tej samej litery
  197.  
  198.                 kmb_slowo.push_back(c_temp1);
  199.             }
  200.  
  201.             // DONE: #TODO: crashes here
  202.             for(int j = 0; j < (int)words2[i].size(); j++)
  203.             {
  204.                 ilosc_liter_pos.push_back(kmb_slowo[j].size());
  205.             }
  206.  
  207.             // DONE: #TODO: crashes here
  208.             generate(0, (int)words2[i].size());
  209.  
  210.             // ### Recursion finished
  211.  
  212.             for(int j = 0; j < (int)slowo_kombinacje.size(); j++)
  213.             {
  214.                 if(check_pattern(slowo_kombinacje[j]))
  215.                 {
  216.                     words3.push_back(words2[i]);
  217.                     break;
  218.                 }
  219.             }
  220.         }
  221.  
  222.         sort(words3.begin(), words3.end(), compareLen);
  223.  
  224.         for(int i = 0; i < (int)words3.size(); i++)
  225.         {
  226.             cout << endl << words3[i];
  227.         }
  228.  
  229.         words2.clear();
  230.         words3.clear();
  231.     }
  232.  
  233.  
  234.  
  235.     //for(int i = 0; i < (int)slowo_kombinacje.size(); i++)
  236.     //{
  237.     //  for(int j = 0; j < (int)slowo_kombinacje[i].size(); j++)
  238.     //  {
  239.     //      cout << slowo_kombinacje[i][j].x << slowo_kombinacje[i][j].y << " ";
  240.     //  }
  241.     //  cout << endl;
  242.     //}
  243.  
  244.     //_getch();
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement