Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- vector<vector<int>> readMatrix()
- {
- vector<vector<int>> result;
- int rows, cols;
- cin >> rows >> cols;
- result.resize(rows);
- for (int i = 0; i < rows; ++i)
- {
- result[i].resize(cols);
- for (int j = 0; j < cols; ++j)
- {
- cin >> result[i][j];
- }
- }
- return result;
- }
- bool isPosible(vector<vector<int>>& matrix, int side, int row, int col)
- {
- if ((row + side) <= (int)matrix.size() && (col + side) <= (int)matrix[row].size())
- {
- return true;
- }
- return false;
- }
- int calculateSquare(vector<vector<int>>& matrix, int sideOfSquare, int row, int col)
- {
- int sum = 0;
- const int sizeOfRows = row + (sideOfSquare);
- for (int i = row; i < sizeOfRows; i++)
- {
- const int sizeOfCols = col + (sideOfSquare);
- for (int j = col; j < sizeOfCols; j++)
- {
- sum += matrix[i][j];
- }
- }
- return sum;
- }
- void findSquare(vector<vector<int>> & matrix, int side, int sum)
- {
- vector<int> output;
- const int sizeOfRows = matrix.size();
- for (int i = 0; i < sizeOfRows; i++)
- {
- const int sizeOfCols = matrix[i].size();
- for (int j = 0; j < sizeOfCols; j++)
- {
- int row = i;
- int col = j;
- if (isPosible(matrix, side, row, col))
- {
- int squareSum = calculateSquare(matrix, side, row, col);
- if (squareSum >= sum)
- {
- output.push_back(floor(squareSum / (side * side)));
- }
- }
- }
- }
- sort(output.begin(), output.end());
- for (int elem : output)
- {
- cout << elem << ' ';
- }
- }
- int main()
- {
- vector<vector<int>> matrix = readMatrix();
- int squareSize, targetSum;
- cin >> squareSize >> targetSum;
- findSquare(matrix, squareSize, targetSum);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment