JosepRivaille

P71711: Ziga-Zaga

May 21st, 2015
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. typedef vector<int> Columna;
  7. typedef vector<Columna> Matriu;
  8.  
  9. bool ZigaZaga(const Matriu& M, int f, int c) {
  10.     int comp = M[0][0];
  11.     bool primer = true;
  12.     for (int j  = 0; j < c; ++j) {
  13.                 if (j%2 == 0) { //Baixar
  14.                     for (int i =0; i < f; ++i) {
  15.                         if (not primer) {
  16.                             if (M[i][j] <= comp) return false;
  17.                             else comp = M[i][j];
  18.                         }
  19.                         primer = false;
  20.                     }
  21.                 }
  22.                 else { //Pujar
  23.                     for (int i = f-1; i >= 0; --i) {
  24.                         if (M[i][j] <= comp) return false;
  25.                         else comp = M[i][j];
  26.                     }
  27.                 }
  28.     }
  29.     return true;
  30. }
  31.  
  32. int main() {
  33.     int f, c;
  34.     int count = 0;
  35.     while (cin >> f >> c) {
  36.         ++count;
  37.         Matriu M(f, Columna(c));
  38.         for (int i = 0; i < f; ++i) {
  39.             for (int j = 0; j < c; ++j) cin >> M[i][j];
  40.         }
  41.         if (ZigaZaga(M, f, c)) cout << "matriu " << count << ": si" << endl;
  42.         else cout << "matriu " << count << ": no" << endl;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment