csansoon

P8.05 P18660 Word search

Nov 29th, 2018
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5.  
  6. typedef vector<vector<char> /**/> Matrix;
  7.  
  8. // mode: [1] = e-d; [2] = d-b; [3] = hor;
  9. bool searchWord(string word, Matrix &sopa, int posROW, int posCOL, string &mode)
  10. {
  11.     mode = "";
  12.     int i = 0;
  13.     int fROW = posROW;
  14.     int fCOL = posCOL;
  15.     int sizeRow = sopa.size();
  16.     int sizeCol = sopa[0].size();
  17.     bool noCabeHori = word.length() > sizeCol - posCOL;
  18.     bool invalidED = noCabeHori;
  19.     bool noCabeVerti = word.length() > sizeRow - posROW;
  20.     bool invalidDB = noCabeVerti;
  21.     bool noCabeDiago = word.length() > min(sizeRow - posROW, sizeCol - posCOL);
  22.     bool invalidHOR = noCabeDiago;
  23.     if (noCabeHori and noCabeVerti)
  24.         return false;
  25.     while ((!invalidED or !invalidDB or !invalidHOR) and i < word.length())
  26.     {
  27.         // e-d:
  28.         if (!noCabeHori)
  29.         {
  30.             if (word[i] != sopa[fROW][posCOL])
  31.                 invalidED = true;
  32.             else if (i == word.length() - 1 and !invalidED)
  33.                 mode += "1";
  34.         }
  35.         // d-b
  36.         if (!noCabeVerti)
  37.         {
  38.             if (word[i] != sopa[posROW][fCOL])
  39.                 invalidDB = true;
  40.             else if (i == word.length() - 1 and !invalidDB)
  41.                 mode += "2";
  42.         }
  43.         // hor
  44.         if (!noCabeDiago)
  45.         {
  46.             if (word[i] != sopa[posROW][posCOL])
  47.                 invalidHOR = true;
  48.             else if (i == word.length() - 1 and !invalidHOR)
  49.                 mode += "3";
  50.         }
  51.         ++posCOL;
  52.         ++posROW;
  53.         ++i;
  54.     }
  55.  
  56.     if (mode != "")
  57.         return true;
  58.     return false;
  59. }
  60. int main()
  61. {
  62.     bool isFirst = true;
  63.     int nwords, filas, columnas;
  64.     string mode;
  65.  
  66.     while (cin >> nwords)
  67.     {
  68.         cin >> filas >> columnas;
  69.         vector<string> palabras(nwords);
  70.         Matrix sopa(filas, vector<char>(columnas));
  71.         Matrix resultado(filas, vector<char>(columnas));
  72.  
  73.         for (int i = 0; i < nwords; ++i)
  74.             cin >> palabras[i];
  75.  
  76.         for (int i = 0; i < filas; ++i)
  77.             for (int j = 0; j < columnas; ++j)
  78.             {
  79.                 cin >> sopa[i][j];
  80.                 resultado[i][j] = sopa[i][j];
  81.             }
  82.  
  83.         for (int i = 0; i < filas; ++i)
  84.         {
  85.             for (int j = 0; j < columnas; ++j)
  86.             {
  87.                 for (int k = 0; k < nwords; ++k)
  88.                 {
  89.                     if (searchWord(palabras[k], sopa, i, j, mode))
  90.                     {
  91.                         for (int l = 0; l < mode.length(); ++l)
  92.                         {
  93.                             if (mode[l] == '1')
  94.                             {
  95.                                 for (int c = j; c < j + palabras[k].length(); ++c)
  96.                                     resultado[i][c] = (resultado[i][c] >= 'a' ? resultado[i][c] - ('a' - 'A') : resultado[i][c]);
  97.                             }
  98.                             else if (mode[l] == '2')
  99.                             {
  100.                                 for (int c = i; c < i + palabras[k].length(); ++c)
  101.                                     resultado[c][j] = (resultado[c][j] >= 'a' ? resultado[c][j] - ('a' - 'A') : resultado[c][j]);
  102.                             }
  103.                             else if (mode[l] == '3')
  104.                             {
  105.                                 for (int c = 0; c < +palabras[k].length(); ++c)
  106.                                     resultado[c + i][c + j] = (resultado[c + i][c + j] >= 'a' ? resultado[c + i][c + j] - ('a' - 'A') : resultado[c + i][c + j]);
  107.                             }
  108.                         }
  109.                     }
  110.                 }
  111.             }
  112.         }
  113.  
  114.         if (isFirst)
  115.             isFirst = false;
  116.         else
  117.             cout << endl;
  118.         for (int i = 0; i < filas; ++i)
  119.         {
  120.             isFirst = true;
  121.             for (int j = 0; j < columnas; ++j)
  122.             {
  123.                 if (isFirst)
  124.                     isFirst = false;
  125.                 else
  126.                     cout << " ";
  127.                 cout << resultado[i][j];
  128.             }
  129.             cout << endl;
  130.         }
  131.         isFirst = false;
  132.     }
  133. }
  134.  
  135. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment