Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- bool rec(vector<string> &cw, vector<string>vec) {
- if (!vec.size()) return true;
- string word = vec[vec.size()-1];
- vec.pop_back();
- vector<vector<int>> cw_vec;
- for (int row = 0; row < cw.size(); row++)
- for (int j = word.length(); j < cw[0].length() + 1; j++) {
- int k = 0;
- while (k < word.length() && (cw[row][j - word.length() + k] == '-' || cw[row][j - word.length() + k] == word[k]))
- k++;
- if (k == word.length())
- cw_vec.push_back({0, row, j - word.length()});
- }
- for (int col = 0; col < cw[0].length(); col++)
- for (int i = word.length(); i < cw.size() + 1; i++) {
- int k = 0;
- while (k < word.length() && (cw[i - word.length() + k][col] == '-' || cw[i - word.length() + k][col] == word[k]) )
- k++;
- if (k == word.length())
- cw_vec.push_back({1, (i - word.length()), col});
- }
- for(int i = 0; i < cw_vec.size(); i++){
- if (cw_vec[i][0] == 0)
- for (int k = 0; k < word.length(); ++k)
- cw[cw_vec[i][1]][cw_vec[i][2] + k] = word[k];
- else
- for (int k = 0; k < word.length(); ++k)
- cw[cw_vec[i][1] + k][cw_vec[i][2]] = word[k];
- if(rec(cw, vec)){
- vec.push_back(word);
- return true;
- }
- if (cw_vec[i][0] == 0)
- for (int k = 0; k < word.length(); ++k)
- cw[cw_vec[i][1]][cw_vec[i][2] + k] = '-';
- else
- for (int k = 0; k < word.length(); ++k)
- cw[cw_vec[i][1] + k][cw_vec[i][2]] = '-';
- }
- vec.push_back(word);
- return false;
- }
- vector<string> cwPuzzle(vector<string> cw, string words) {
- words+=";";
- vector<string> vec;
- int pos = 0;
- string token;
- while ((pos = words.find(";")) != -1) {
- token = words.substr(0, pos);
- words.erase(0, pos + 1);
- vec.push_back(token);
- }
- rec(cw, vec);
- return cw;
- }
- int main()
- {
- ofstream fout(getenv("OUTPUT_PATH"));
- vector<string> cw(10);
- for (int i = 0; i < 10; i++) {
- string cw_item;
- getline(cin, cw_item);
- cw[i] = cw_item;
- }
- string words;
- getline(cin, words);
- vector<string> result = cwPuzzle(cw, words);
- for (int i = 0; i < result.size(); i++) {
- fout << result[i];
- if (i != result.size() - 1) {
- fout << "\n";
- }
- }
- fout << "\n";
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement