erni_hax

sortedmatrix X21748

Jan 14th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1.   14: #include <iostream>
  2.   15: #include <vector>
  3.   16:
  4.   17: typedef std::vector<std::vector<int> > Matrix;
  5.   18:
  6.   19: bool sortedmatrix(const Matrix& M) {
  7.   20:     if(!M.size() || !M.at(0).size()) return true; // If matrix is null return true
  8.   21:     std::vector<int> vec_columns = M.at(0); // We create an auxiliar vector to avoid check twice the matrix copying it from first row
  9.   22:     for(int x = 0; x < M.size(); x++) {
  10.   23:         int n_row = M[x][0]; //we load first value
  11.   24:         for(int y = 0; y < M.at(0).size(); y++) {
  12.   25:             //We check that the row is ascending otherwise return false
  13.   26:             if(n_row <= M[x][y]) n_row = M[x][y]; else return false;
  14.   27:             //We check that the column is ascending too otherwise return false
  15.   28:             if(vec_columns[y] <= M[x][y]) vec_columns[y] = M[x][y]; else return false;
  16.   29:         }
  17.   30:     }
  18.   31:     return true; //It's sorted, return true
  19.   32: }
  20.   33:
  21.   34: int main() {
  22.   35:     int n, m;
  23.   36:     while(std::cin >> n >> m) { //load matrix size
  24.   37:         Matrix matrix(n, std::vector<int>(m)); //Initialize the matrix
  25.   38:         //load the matrix
  26.   39:         for(int x = 0; x < n; x++)
  27.   40:             for(int y = 0; y < m; y++)
  28.   41:                 std::cin >> matrix[x][y];
  29.   42:         //we check if it's sorted
  30.   43:         if(sortedmatrix(matrix))
  31.   44:             std::cout << "yes" << std::endl;
  32.   45:         else
  33.   46:             std::cout << "no" << std::endl;
  34.   47:     }
  35.   48:     return 0;
  36.   49: }
Advertisement
Add Comment
Please, Sign In to add comment