Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- //#include <conio.h>
- #include <vector>
- #include <cmath>
- #include <fstream>
- #include <algorithm>
- using namespace std;
- string matrix[4];
- bool matrix2[4][4];
- struct v_2d // two-dimensional integer vector
- {
- int x;
- int y;
- };
- vector < vector <v_2d> > kmb_slowo;
- vector < vector <v_2d> > slowo_kombinacje;
- vector <int> ilosc_liter_pos; //[LENGTH] = {3,2,2,1,3};
- vector <int> przypadki; //[LENGTH];
- bool compareLen(const string& a, const string& b)
- {
- return (a.size() > b.size());
- }
- void matrix_reset()
- {
- for(int i = 0; i < 4; i++)
- {
- for(int j = 0; j < 4; j++)
- matrix2[i][j] = true;
- }
- }
- bool matrix_search(char literka)
- {
- for(int i = 0; i < 4; i++)
- {
- for(int j = 0; j < 4; j++)
- {
- if(matrix[i][j] == literka && matrix2[i][j])
- {
- matrix2[i][j] = false;
- return true;
- }
- }
- }
- return false;
- }
- vector <v_2d> matrix_search2(char literka)
- {
- vector <v_2d> vc_temp;
- for(int i = 0; i < 4; i++)
- {
- for(int j = 0; j < 4; j++)
- {
- if(matrix[i][j] == literka)
- {
- v_2d c_temp = {i, j};
- vc_temp.push_back(c_temp);
- }
- }
- }
- return vc_temp;
- }
- bool inRange(v_2d c1, v_2d c2)
- {
- float dist = sqrt((float)(c1.x-c2.x)*(c1.x-c2.x) + (c1.y-c2.y)*(c1.y-c2.y));
- if(dist < 1.5 && dist > 0.1) // interval: (0; sqrt2>
- return true;
- return false;
- }
- bool check_pattern(vector <v_2d> kandydat)
- {
- matrix_reset();
- for(int i = 0; i < (int)kandydat.size() - 1; i++)
- {
- matrix2[kandydat[i].x][kandydat[i].y] = false;
- if(!inRange(kandydat[i], kandydat[i + 1]) || !matrix2[kandydat[i + 1].x][kandydat[i + 1].y])
- return false;
- }
- return true;
- }
- void generate(int s, int length)
- {
- //length - dl. slowa
- //s - iterator, wart. poczatkowa == 0
- if(s > length - 1)
- return;
- for(int i = 0; i < ilosc_liter_pos[s]; i++)
- {
- przypadki[s] = i;
- generate(s + 1, length);
- if(s == length - 1)
- {
- vector <v_2d> kandydat;
- for(int k = 0; k < length; k++)
- {
- // DONE: #TODO: przypadki[k]
- kandydat.push_back(kmb_slowo[k][przypadki[k]]);
- }
- slowo_kombinacje.push_back(kandydat);
- }
- }
- }
- int main()
- {
- ios::sync_with_stdio(false);
- vector <string> words; // wszystkie
- vector <string> words2; // pierwszy sort
- vector <string> words3; // rezultat
- fstream plik1;
- plik1.open("slo.txt", ios::in);
- while(!plik1.eof())
- {
- string temp;
- getline(plik1, temp);
- words.push_back(temp);
- }
- plik1.close();
- //cout << "(C)2014 Marcin Waszak\n";
- //cout << "OSZUKISTA 0.1 PRE-ALPHA\n\n";
- //cout << "Podaj 4 rzedy:\n\n";
- int n;
- cin >> n;
- for(int k = 0; k < n; k++)
- {
- cin >> matrix[0];
- cin >> matrix[1];
- cin >> matrix[2];
- cin >> matrix[3];
- //matrix[0] = "fasf";
- //matrix[1] = "kkkk";
- //matrix[2] = "fawf";
- //matrix[3] = "xxxx";
- //cout << matrix[0] << endl;
- //cout << matrix[1] << endl;
- //cout << matrix[2] << endl;
- //cout << matrix[3] << endl;
- for(int i = 0; i < (int)words.size(); i++) // nowy wyraz
- {
- matrix_reset();
- for(int j = 0; j < (int)words[i].size(); j++) // bierzemy literk©
- {
- if(!matrix_search(words[i][j]))
- break;
- if(j == (int)words[i].size() - 1)
- words2.push_back(words[i]);
- }
- }
- // OGARNIACZ
- for(int i = 0; i < (int)words2.size(); i++) // bierze kolejne sowo ##
- {
- kmb_slowo.clear();
- slowo_kombinacje.clear();
- ilosc_liter_pos.clear();
- przypadki.clear();
- przypadki.resize(words2[i].size());
- for(int j = 0; j < (int)words2[i].size(); j++)
- {
- vector <v_2d> c_temp1;
- c_temp1 = matrix_search2(words2[i][j]); // wszystkie warianty tej samej litery
- kmb_slowo.push_back(c_temp1);
- }
- // DONE: #TODO: crashes here
- for(int j = 0; j < (int)words2[i].size(); j++)
- {
- ilosc_liter_pos.push_back(kmb_slowo[j].size());
- }
- // DONE: #TODO: crashes here
- generate(0, (int)words2[i].size());
- // ### Recursion finished
- for(int j = 0; j < (int)slowo_kombinacje.size(); j++)
- {
- if(check_pattern(slowo_kombinacje[j]))
- {
- words3.push_back(words2[i]);
- break;
- }
- }
- }
- sort(words3.begin(), words3.end(), compareLen);
- for(int i = 0; i < (int)words3.size(); i++)
- {
- cout << endl << words3[i];
- }
- words2.clear();
- words3.clear();
- }
- //for(int i = 0; i < (int)slowo_kombinacje.size(); i++)
- //{
- // for(int j = 0; j < (int)slowo_kombinacje[i].size(); j++)
- // {
- // cout << slowo_kombinacje[i][j].x << slowo_kombinacje[i][j].y << " ";
- // }
- // cout << endl;
- //}
- //_getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement