Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Apples.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <list>
- #include <vector>
- using namespace std;
- vector <pair<int, int>> MarkAdj(bool **matrix, vector<pair<int,int> > coord, int x, int y, int r, int c) {
- vector<pair<int, int> > tempCoord;
- auto pair = make_pair(x, y);
- if (x < 1)
- x = 1;
- if (y < 1)
- y = 1;
- if (x >= r)
- x = r - 1;
- if (y >= c)
- y = c - 1;
- coord.push_back(make_pair(x, y - 1));
- coord.push_back(make_pair(x - 1, y));
- coord.push_back(make_pair(x, y));
- coord.push_back(make_pair(x + 1, y));
- coord.push_back(make_pair(x, y + 1));
- return coord;
- }
- void UpdateMatrix(bool **matrix, vector<pair<int,int> > coord) {
- for (int i = 0; i < coord.size(); i++) {
- matrix[coord[i].first][coord[i].second] = true;
- }
- }
- void DrawMatrix(bool **matrix, int r, int c) {
- for (int i = 1; i < r; i++) {
- for (int j = 1; j < c; j++) {
- cout << matrix[i][j] << " ";
- }
- cout << endl;
- }
- cout << endl;
- }
- int main()
- {
- int r, c, days, rotten;
- cin >> r >> c >> days;
- //matrix borders
- r += 1;
- c += 1;
- bool** matrix = new bool*[r+1];
- for (int i = 0; i <= r; i++) {
- matrix[i] = new bool[c+1];
- }
- for (int i = 0; i <= r; i++) {
- for (int j = 0; j <= c; j++) {
- matrix[i][j] = 0;
- }
- }
- cout << " 1 or 2 rotten apples? " << endl;
- cin >> rotten;
- for (int i = 0; i < rotten; i++) {
- int x, y;
- cin >> x >> y;
- //Check for boundries
- if (x > r - 1)
- x = r -1;
- else if (x < 1)
- x = 1;
- if (y > c - 1)
- y = c -1;
- else if (y < 1)
- y = 1;
- matrix[x][y] = true;
- }
- vector <pair<int, int> > coord;
- for (int i = 0; i < days; i++) {
- UpdateMatrix(matrix, coord);
- for (int j = 1; j <= r; j++) {
- for (int k = 1; k <= c; k++) {
- if (matrix[j][k] == true) {
- coord = MarkAdj(matrix, coord, j, k, r, c);
- }
- }
- }
- }
- cout << "RESULT" << endl;
- int count = 0;
- for (int i = 1; i < r; i++) {
- for (int j = 1; j < c; j++) {
- if (matrix[i][j] == false) {
- count++;
- }
- }
- }
- DrawMatrix(matrix, r, c);
- cout << "Apples Count is: " << count << endl;
- for (int i = 0; i <= r; i++) {
- delete[] matrix[i];
- }
- delete[] matrix;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement