mitakvd

2. Square Manipulator

Nov 25th, 2019
830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. vector<vector<int>> readMatrix()
  9. {
  10.     vector<vector<int>> result;
  11.     int rows, cols;
  12.     cin >> rows >> cols;
  13.     result.resize(rows);
  14.     for (int i = 0; i < rows; ++i)
  15.     {
  16.         result[i].resize(cols);
  17.         for (int j = 0; j < cols; ++j)
  18.         {
  19.             cin >> result[i][j];
  20.         }
  21.     }
  22.     return result;
  23. }
  24.  
  25. bool isPosible(vector<vector<int>>& matrix, int side, int row, int col)
  26. {
  27.     if ((row + side) <= (int)matrix.size() && (col + side) <= (int)matrix[row].size())
  28.     {
  29.         return true;
  30.     }
  31.     return false;
  32. }
  33.  
  34. int calculateSquare(vector<vector<int>>& matrix, int sideOfSquare, int row, int col)
  35. {
  36.     int sum = 0;
  37.     const int sizeOfRows = row + (sideOfSquare);
  38.     for (int i = row; i < sizeOfRows; i++)
  39.     {
  40.         const int sizeOfCols = col + (sideOfSquare);
  41.         for (int j = col; j < sizeOfCols; j++)
  42.         {
  43.             sum += matrix[i][j];
  44.         }
  45.     }
  46.     return sum;
  47. }
  48.  
  49. void findSquare(vector<vector<int>> & matrix, int side, int sum)
  50. {
  51.     vector<int> output;
  52.     const int sizeOfRows = matrix.size();
  53.     for (int i = 0; i < sizeOfRows; i++)
  54.     {
  55.         const int sizeOfCols = matrix[i].size();
  56.         for (int j = 0; j < sizeOfCols; j++)
  57.         {
  58.             int row = i;
  59.             int col = j;
  60.             if (isPosible(matrix, side, row, col))
  61.             {
  62.                 int squareSum = calculateSquare(matrix, side, row, col);
  63.                 if (squareSum >= sum)
  64.                 {
  65.                     output.push_back(floor(squareSum / (side * side)));
  66.                 }
  67.             }
  68.         }
  69.     }
  70.     sort(output.begin(), output.end());
  71.     for (int elem : output)
  72.     {
  73.         cout << elem << ' ';
  74.     }
  75. }
  76.  
  77. int main()
  78. {
  79.     vector<vector<int>> matrix = readMatrix();
  80.    
  81.     int squareSize, targetSum;
  82.     cin >> squareSize >> targetSum;
  83.    
  84.     findSquare(matrix, squareSize, targetSum);
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment