JosepRivaille

X17276: Diagonals creixents

May 23rd, 2015
1,337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. typedef vector<int> Row;
  7. typedef vector<Row> Matrix;
  8.  
  9.  
  10. //Pre: Llegeix una sequencia de valors
  11. //Post: Omple la matriu amb aquests;
  12. void read_matrix(Matrix& M) {
  13.     int f = M.size();
  14.     int c = M[0].size();
  15.     for (int i = 0; i < f; ++i) {
  16.         for (int j = 0; j < c ; ++j) {
  17.             cin >> M[i][j];
  18.         }
  19.     }
  20. }
  21.  
  22. //Pre: Consulta les diagonals de la matriu a partir del punt a,b
  23. //Post: Retorna true si i nomes si esta ordenat creixentment
  24. bool increasing_d(const Matrix& M, int a, int b) {
  25.     int f = M.size();
  26.     int c = M[0].size();
  27.     //dalt-dreta
  28.     int i = a-1, j = b+1, comp = M[a][b];
  29.     while (i >= 0 && j < c) {
  30.         if (M[i][j] > comp) comp = M[i][j];
  31.         else return false;
  32.         --i;
  33.         ++j;
  34.     }
  35.     //dalt-esquerra
  36.     i = a-1, j = b-1, comp = M[a][b];
  37.     while (i >= 0 && j >= 0) {
  38.         if (M[i][j] > comp) comp = M[i][j];
  39.         else return false;
  40.         --i;
  41.         --j;
  42.     }
  43.     //baix-esquerra
  44.     i = a+1, j = b-1, comp = M[a][b];
  45.     while (i < f && j >= 0) {
  46.         if (M[i][j] > comp) comp = M[i][j];
  47.         else return false;
  48.         ++i;
  49.         --j;
  50.     }
  51.     //baix-dreta
  52.     i = a+1, j = b+1, comp = M[a][b];
  53.     while (i < f && j < c) {
  54.         if (M[i][j] > comp) comp = M[i][j];
  55.         else return false;
  56.         ++i;
  57.         ++j;
  58.     }
  59.     return true;
  60. }
  61.  
  62. int main() {
  63.     int f, c;
  64.     while (cin >> f >> c) { //Tamany matriu
  65.         Matrix M(f, Row(c));
  66.         read_matrix(M);
  67.         cin >> f >> c; //Posicio inicial
  68.         if (increasing_d(M, f, c)) cout << "si" << endl;
  69.         else if (not increasing_d(M, f, c)) cout << "no" << endl;
  70.     }
  71. }
  72.  
  73. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment