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