Advertisement
RoshHoul

Apples1

Jan 27th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. // Apples.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <list>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. vector <pair<int, int>> MarkAdj(bool **matrix, vector<pair<int,int> > coord, int x, int y, int r, int c) {
  13.    
  14.     vector<pair<int, int> > tempCoord;
  15.     auto pair = make_pair(x, y);
  16.  
  17.     if (x < 1)
  18.         x = 1;
  19.     if (y < 1)
  20.         y = 1;
  21.     if (x >= r)
  22.         x = r - 1;
  23.     if (y >= c)
  24.         y = c - 1;
  25.    
  26.    
  27.     coord.push_back(make_pair(x, y - 1));
  28.     coord.push_back(make_pair(x - 1, y));
  29.     coord.push_back(make_pair(x, y));
  30.     coord.push_back(make_pair(x + 1, y));
  31.     coord.push_back(make_pair(x, y + 1));
  32.  
  33.     return coord;
  34. }
  35.  
  36. void UpdateMatrix(bool **matrix, vector<pair<int,int> > coord) {
  37.     for (int i = 0; i < coord.size(); i++) {
  38.         matrix[coord[i].first][coord[i].second] = true;
  39.     }
  40. }
  41.  
  42. void DrawMatrix(bool **matrix, int r, int c) {
  43.     for (int i = 1; i < r; i++) {
  44.         for (int j = 1; j < c; j++) {
  45.             cout << matrix[i][j] << " ";
  46.         }
  47.         cout << endl;
  48.     }
  49.  
  50.  
  51.     cout << endl;
  52. }
  53.  
  54.  
  55. int main()
  56. {
  57.     int r, c, days, rotten;
  58.  
  59.     cin >> r >> c >> days;
  60.  
  61.     //matrix borders
  62.     r += 1;
  63.     c += 1;
  64.  
  65.     bool** matrix = new bool*[r+1];
  66.     for (int i = 0; i <= r; i++) {
  67.         matrix[i] = new bool[c+1];
  68.     }
  69.  
  70.     for (int i = 0; i <= r; i++) {
  71.         for (int j = 0; j <= c; j++) {
  72.             matrix[i][j] = 0;
  73.         }
  74.     }
  75.  
  76.     cout << " 1 or 2 rotten apples? " << endl;
  77.     cin >> rotten;
  78.  
  79.     for (int i = 0; i < rotten; i++) {
  80.         int x, y;
  81.         cin >> x >> y;
  82.  
  83.         //Check for boundries
  84.         if (x > r - 1)
  85.             x = r -1;
  86.         else if (x < 1)
  87.             x = 1;
  88.         if (y > c - 1)
  89.             y = c -1;
  90.         else if (y < 1)
  91.             y = 1;
  92.        
  93.         matrix[x][y] = true;
  94.     }
  95.  
  96.    
  97.     vector <pair<int, int> > coord;
  98.     for (int i = 0; i < days; i++) {
  99.         UpdateMatrix(matrix, coord);
  100.         for (int j = 1; j <= r; j++) {
  101.             for (int k = 1; k <= c; k++) {
  102.                 if (matrix[j][k] == true) {
  103.                 coord = MarkAdj(matrix, coord, j, k, r, c);
  104.                 }
  105.             }
  106.         }
  107.     }
  108.  
  109.     cout << "RESULT" << endl;
  110.     int count = 0;
  111.     for (int i = 1; i < r; i++) {
  112.         for (int j = 1; j < c; j++) {
  113.             if (matrix[i][j] == false) {
  114.                 count++;
  115.             }
  116.         }
  117.     }
  118.  
  119.     DrawMatrix(matrix, r, c);
  120.     cout << "Apples Count is: " << count << endl;
  121.  
  122.     for (int i = 0; i <= r; i++) {
  123.         delete[] matrix[i];
  124.  
  125.     }
  126.  
  127.     delete[] matrix;
  128.  
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement