JosepRivaille

P18660: Sopa de lletres

May 26th, 2015
1,665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. struct Dir {
  8.     int x, y;
  9. };
  10.  
  11. struct Pos  {
  12.     int x, y;
  13. };
  14.  
  15.  
  16. typedef vector<char> Row;
  17. typedef vector<Row> Matrix;
  18.  
  19.  
  20. bool found(const Matrix& M, string s, const Pos& p, const Dir& d) {
  21.     int f = M.size();
  22.     int c = M[0].size();
  23.     int i = p.x;
  24.     int j = p.y;
  25.     int tam_s = s.size();
  26.     for (int k = 0; k < tam_s; ++k) {
  27.         if (i < f && j < c) {
  28.             if (M[i][j] != s[k] && char(M[i][j] - 'a' + 'A') != s[k]) return false;
  29.             i += d.x;
  30.             j += d.y;
  31.         }
  32.         else return false;
  33.     }
  34.     return true;
  35. }
  36.  
  37. void change_word(Matrix& M, string s, const Pos& p, const Dir& d) {
  38.     int f = M.size();
  39.     int c = M[0].size();
  40.     int i = p.x;
  41.     int j = p.y;
  42.     int tam_s = s.size();
  43.     for (int k = 0; k < tam_s; ++k) {
  44.         if (i < f && j < c) {
  45.             if (M[i][j] >= 'a' && M[i][j] <= 'z') M[i][j] = char(M[i][j] - 'a' + 'A');
  46.         }
  47.         i += d.x;
  48.         j += d.y;
  49.     }
  50. }
  51.  
  52. void read_words(vector<string>& v) {
  53.     int tam = v.size();
  54.     for (int i = 0; i < tam; ++i) cin >> v[i];
  55. }
  56.  
  57. void read_matrix(Matrix& M) {
  58.     int f = M.size();
  59.     int c = M[0].size();
  60.     for (int i = 0; i < f; ++i) {
  61.         for (int j = 0; j < c; ++j) {
  62.             cin >> M[i][j];
  63.         }
  64.     }
  65. }
  66.  
  67. void write_matrix(Matrix& M) {
  68.     int f = M.size();
  69.     int c = M[0].size();
  70.     for (int i = 0; i < f; ++i) {
  71.         for (int j = 0; j < c; ++j) {
  72.             if (j != 0) cout << ' ';
  73.             cout << M[i][j];
  74.         }
  75.         cout << endl;
  76.     }
  77. }
  78.  
  79. int main() {
  80.     int n, f, c;
  81.     bool first = true;
  82.     while (cin >> n >> f >> c) {
  83.         vector<string> v(n);
  84.         read_words(v);
  85.         Matrix M(f, Row(c));
  86.         read_matrix(M);
  87.         Dir vertical;
  88.         Dir horitzontal;
  89.         Dir diagonal;
  90.         vertical.x = 1;
  91.         vertical.y = 0;
  92.         horitzontal.x = 0;
  93.         horitzontal.y = 1;
  94.         diagonal.x = 1;
  95.         diagonal.y = 1;
  96.         Pos pos;
  97.         Matrix aux = M;
  98.         for (int k = 0; k < n; ++k) {
  99.             for (pos.x = 0; pos.x < f; ++pos.x) {
  100.                 for (pos.y = 0; pos.y < c; ++pos.y) {
  101.                     if (found(M, v[k], pos, vertical)) change_word(aux, v[k], pos, vertical);
  102.                     if (found(M, v[k], pos, horitzontal)) change_word(aux, v[k], pos, horitzontal);
  103.                     if (found(M, v[k], pos, diagonal)) change_word(aux, v[k], pos, diagonal);
  104.                 }
  105.             }
  106.         }
  107.         if (not first) cout << endl;
  108.         else first = false;
  109.         write_matrix(aux);
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment