Advertisement
RoshHoul

Apples2

Jan 27th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. // ConsoleApplication1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <vector>
  7. #include <set>
  8.  
  9. using namespace std;
  10.  
  11. set<pair<int, int> > pestilence(set < pair<int, int> > apples, int x, int y, int rows, int cols) {
  12.  
  13.     set<pair<int, int> > tempCoord;
  14.  
  15.  
  16.     if (y > 0)
  17.         tempCoord.insert(make_pair(x, y - 1));
  18.  
  19.     if ( x > 0)
  20.         tempCoord.insert(make_pair(x - 1, y));
  21.    
  22.     if (x+1 <= rows)
  23.         tempCoord.insert(make_pair(x + 1, y));
  24.    
  25.     if (y+1 <= cols)
  26.         tempCoord.insert(make_pair(x, y + 1));
  27.  
  28.  
  29.         return tempCoord;
  30. }
  31.  
  32. int main()
  33. {
  34.     int rows, cols, days;
  35.     set<pair <int, int> > badApple;
  36.     set<pair <int, int> > badApplesNew;
  37.  
  38.     cout << "Enter rows/cols/days" << endl;
  39.     cin >> rows >> cols >> days;
  40.  
  41.     cout << "Enter number of bad apples" << endl;
  42.     int bads;
  43.     cin >> bads;
  44.  
  45.     for (int i = 0; i < bads; i++) {
  46.         int x, y;
  47.         cin >> x >> y;
  48.         badApple.insert(make_pair(x, y));
  49.        
  50.     }
  51.  
  52.     for (int i = 1; i <= days; i++) {
  53.         set<pair<int, int> >::iterator it = badApple.begin();
  54.         for (it = badApple.begin(); it != badApple.end(); it++) {
  55.             auto tempSet = pestilence(badApple, it->first, it->second, rows, cols);
  56.             badApplesNew.insert(tempSet.begin(), tempSet.end());
  57.         }
  58.         badApple.insert(badApplesNew.begin(), badApplesNew.end());
  59.     }
  60.  
  61.     cout << badApple.size() << endl;
  62.  
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement