Advertisement
Guest User

avoid duplicate

a guest
Nov 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <queue>
  4. #include <iterator>
  5.  
  6.  
  7. class RottenApple
  8.  
  9. {
  10. public:
  11. int x;
  12. int y;
  13.  
  14. RottenApple(int x, int y)
  15. {
  16. this->x = x;
  17. this->y = y;
  18. }
  19.  
  20. bool operator ==(const RottenApple& obj) const
  21. {
  22. if (this->x == obj.x && this->y == obj.y)
  23. return true;
  24.  
  25. return false;
  26. }
  27.  
  28.  
  29. void print()
  30. {
  31. std::cout << this->x << " " << this->y << std::endl;
  32. }
  33. };
  34.  
  35. bool operator<(const RottenApple& a, const RottenApple& b) {
  36. return a.x < b.x;
  37. }
  38.  
  39. int main()
  40. {
  41. int row, column, days;
  42. std::cin >> row >> column >> days;
  43.  
  44. std::set<RottenApple* > rotten_apples;
  45. std::queue<RottenApple*> queued_apples;
  46.  
  47. int x, y;
  48. while (std::cin >> x >> y)
  49. {
  50. RottenApple* apple = new RottenApple(row - x, y - 1);
  51. queued_apples.push(apple);
  52. }
  53.  
  54. for (int i = 0; i <= days; i++)
  55. {
  56. std::queue<RottenApple* > nextInLine;
  57. while (!queued_apples.empty())
  58. {
  59. RottenApple* apple = queued_apples.front();
  60. queued_apples.pop();
  61. rotten_apples.insert(apple);
  62.  
  63. if (apple->x + 1 < row)
  64. {
  65. nextInLine.push(new RottenApple(apple->x + 1, apple->y));
  66. }
  67.  
  68. if (apple->x - 1 >= 0)
  69. {
  70. nextInLine.push(new RottenApple(apple->x - 1, apple->y));
  71. }
  72.  
  73. if (apple->y + 1 < column)
  74. {
  75. nextInLine.push(new RottenApple(apple->x, apple->y + 1));
  76. }
  77.  
  78. if (apple->y - 1 >= 0)
  79. {
  80. nextInLine.push(new RottenApple(apple->x, apple->y - 1));
  81. }
  82.  
  83. }
  84. queued_apples = nextInLine;
  85. }
  86.  
  87.  
  88. for (auto temp : rotten_apples)
  89. temp->print();
  90.  
  91. std::cout << rotten_apples.size() << std::endl;
  92.  
  93. return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement