Advertisement
RoshHoul

appple

Jan 27th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 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 (std::find(coord.begin(), coord.end(), pair) != coord.end()) {
  18. //      return coord;
  19.     }
  20.  
  21.  
  22.     if (x < 1)
  23.         x = 1;
  24.     if (y < 1)
  25.         y = 1;
  26.     if (x >= r)
  27.         x = r - 1;
  28.     if (y >= c)
  29.         y = c - 1;
  30.    
  31.    
  32.     coord.push_back(make_pair(x, y - 1));
  33.     coord.push_back(make_pair(x - 1, y));
  34.     coord.push_back(make_pair(x, y));
  35.     coord.push_back(make_pair(x + 1, y));
  36.     coord.push_back(make_pair(x, y + 1));
  37.  
  38.     return coord;
  39. }
  40.  
  41. void UpdateMatrix(bool **matrix, vector<pair<int,int> > coord) {
  42.     for (int i = 0; i < coord.size(); i++) {
  43.         matrix[coord[i].first][coord[i].second] = true;
  44.     }
  45. }
  46.  
  47. void DrawMatrix(bool **matrix, int r, int c) {
  48.     for (int i = 1; i < r; i++) {
  49.         for (int j = 1; j < c; j++) {
  50.             cout << matrix[i][j] << " ";
  51.         }
  52.         cout << endl;
  53.     }
  54.  
  55.  
  56.     cout << endl;
  57. }
  58.  
  59.  
  60. int main()
  61. {
  62.     int r, c, days, rotten;
  63.  
  64.     cin >> r >> c >> days;
  65.  
  66.     //matrix borders
  67.     r += 1;
  68.     c += 1;
  69.  
  70.     bool** matrix = new bool*[r+1];
  71.     for (int i = 0; i <= r; i++) {
  72.         matrix[i] = new bool[c+1];
  73.     }
  74.  
  75.     for (int i = 0; i <= r; i++) {
  76.         for (int j = 0; j <= c; j++) {
  77.             matrix[i][j] = 0;
  78.         }
  79.     }
  80.  
  81.     cout << " 1 or 2 rotten apples? " << endl;
  82.     cin >> rotten;
  83.  
  84.     for (int i = 0; i < rotten; i++) {
  85.         int x, y;
  86.         cin >> x >> y;
  87.  
  88.         //Check for boundries
  89.         if (x > r - 1)
  90.             x = r -1;
  91.         else if (x < 1)
  92.             x = 1;
  93.         if (y > c - 1)
  94.             y = c -1;
  95.         else if (y < 1)
  96.             y = 1;
  97.        
  98.         matrix[x][y] = true;
  99.     }
  100.  
  101.    
  102.     vector <pair<int, int> > coord;
  103.     for (int i = 0; i < days; i++) {
  104.         UpdateMatrix(matrix, coord);
  105.         for (int j = 1; j <= r; j++) {
  106.             for (int k = 1; k <= c; k++) {
  107.                 if (matrix[j][k] == true) {
  108.                 //vector<pair<int, int> > temp = MarkAdj(matrix, coord, j, k, r, c);
  109.                 coord = MarkAdj(matrix, coord, j, k, r, c);
  110.                 //coord.insert(coord.end(), temp.begin(), temp.end());
  111.                 }
  112.             }
  113.         }
  114.     }
  115.  
  116.    
  117.  
  118.  
  119.     cout << "RESULT" << endl;
  120.     int count = 0;
  121.     for (int i = 1; i < r; i++) {
  122.         for (int j = 1; j < c; j++) {
  123.             if (matrix[i][j] == false) {
  124.                 count++;
  125.             }
  126.         }
  127.     }
  128.  
  129.     DrawMatrix(matrix, r, c);
  130.     cout << "Apples Count is: " << count << endl;
  131.  
  132.     for (int i = 0; i <= r; i++) {
  133.         delete[] matrix[i];
  134.  
  135.     }
  136.  
  137.     delete[] matrix;
  138.  
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement