JosepRivaille

X01236: Sopa de lletres fàcil

May 23rd, 2015
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. typedef vector< vector<char> > Matriu;
  8.  
  9. //Pre: Rep una matriu per emplenarla
  10. //Post: Escriu els valors corresponents a cada casella
  11. void read_matrix(Matriu& a) {
  12.     int tam = a.size();
  13.     for (int i = 0; i < tam; ++i) {
  14.         for (int j = 0; j < tam; ++j) {
  15.             cin >> a[i][j];
  16.         }
  17.     }
  18. }
  19.  
  20. //Pre: i, j son index valids de la matriu a
  21. //Post: retorna cert si i nomes si (i, j) es una posicio de inici
  22. //         diagonal de la paraula s a la matriu a
  23. bool es_subparaula(const string& s, const Matriu& a, int i, int j) {
  24.     int tam_s = s.size();
  25.     bool existeix = true;
  26.     int k = 0;
  27.     while (existeix && k < tam_s) {
  28.         if (a[i][j] == s[k]) {
  29.             ++k;
  30.             ++i;
  31.             ++j;
  32.         }
  33.         else existeix = false;
  34.     }
  35.     return existeix;
  36. }
  37.  
  38.  
  39. //Pre: Llegeix m sequencies de matrius a de tamany m i una paraula
  40. //Post: escriu true si i nomes si la paraula s apareix en
  41. //         alguna diagonal de la matriu a. En cas contrari escriu fals
  42. int main() {
  43.     int m;
  44.     cin >> m;
  45.     while (m != 0) {
  46.         string s;
  47.         int n;
  48.         cin >> s >> n;
  49.         Matriu a(n, vector<char>(n));
  50.         read_matrix(a);
  51.         int tam = s.size();
  52.         bool trobat = false;
  53.         for (int i = 0; i <= n-tam && not trobat; ++i) {
  54.             for (int j = 0; j <= n-tam && not trobat; ++j) {
  55.                 if (es_subparaula(s, a, i, j)) trobat = true;
  56.             }
  57.         }
  58.         if (not trobat) cout << "false" << endl;
  59.         else if (trobat) cout << "true" << endl;
  60.         --m;
  61.     }
  62. }
  63.  
  64. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment