Advertisement
arsovski

duplicatesRemove

Nov 17th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.util.*;
  3.  
  4.  
  5.  
  6. class Solution{
  7.  
  8.  
  9. public static void main(String[] args) {
  10. class RottenApples implements Comparable<RottenApples>{
  11. public int x;
  12. public int y;
  13.  
  14. RottenApples(int x,int y){
  15. this.x=x;
  16. this.y=y;
  17. }
  18.  
  19.  
  20. void print()
  21. {
  22. System.out.println(this.x+ " "+this.y);
  23. }
  24.  
  25.  
  26. @Override
  27. public int compareTo(RottenApples o) {
  28. if(this.x == o.x && this.y==o.y)
  29. return 0;
  30. else return 1;
  31. }
  32. }
  33.  
  34. Scanner sc=new Scanner(System.in);
  35. int row,column,days;
  36.  
  37. LinkedList<RottenApples> rottenApples=new LinkedList<>();
  38. LinkedList<RottenApples> nextInLineApples=new LinkedList<>();
  39.  
  40. row=sc.nextInt();
  41. column=sc.nextInt();
  42. days=sc.nextInt();
  43.  
  44. int xCoord,yCoord;
  45. while(sc.hasNext()){
  46. xCoord=sc.nextInt();
  47. yCoord=sc.nextInt();
  48.  
  49. RottenApples apple = new RottenApples(row-xCoord,yCoord-1);
  50. nextInLineApples.add(apple);
  51. }
  52.  
  53.  
  54. LinkedList<RottenApples> helper=new LinkedList<>();
  55.  
  56. //Comparable doesn't work ?
  57. for(int i=0;i<=days;i++){
  58.  
  59. while(!nextInLineApples.isEmpty()){
  60.  
  61. //Test queues in java
  62. RottenApples newApple=nextInLineApples.peek();
  63. rottenApples.add(newApple);
  64. nextInLineApples.remove();
  65.  
  66. if(newApple.x+1 < row){
  67. helper.add(new RottenApples(newApple.x+1,newApple.y));
  68. }
  69.  
  70. if(newApple.x-1 >=0){
  71. helper.add(new RottenApples(newApple.x-1,newApple.y));
  72. }
  73.  
  74. if(newApple.y-1>=0){
  75. helper.add(new RottenApples(newApple.x,newApple.y-1));
  76. }
  77.  
  78. if(newApple.y+1<column){
  79. helper.add(new RottenApples(newApple.x,newApple.y+1));
  80. }
  81.  
  82.  
  83. }
  84.  
  85. for(RottenApples var : helper)
  86. nextInLineApples.add(var);
  87.  
  88. helper.clear();
  89. }
  90.  
  91.  
  92. for(RottenApples var: rottenApples)
  93. var.print();
  94.  
  95. System.out.println(row* column - rottenApples.size());
  96.  
  97.  
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement