Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.util.*;
- class Solution{
- public static void main(String[] args) {
- class RottenApples implements Comparable<RottenApples>{
- public int x;
- public int y;
- RottenApples(int x,int y){
- this.x=x;
- this.y=y;
- }
- void print()
- {
- System.out.println(this.x+ " "+this.y);
- }
- @Override
- public int compareTo(RottenApples o) {
- if(this.x == o.x && this.y==o.y)
- return 0;
- else return 1;
- }
- }
- Scanner sc=new Scanner(System.in);
- int row,column,days;
- LinkedList<RottenApples> rottenApples=new LinkedList<>();
- LinkedList<RottenApples> nextInLineApples=new LinkedList<>();
- row=sc.nextInt();
- column=sc.nextInt();
- days=sc.nextInt();
- int xCoord,yCoord;
- while(sc.hasNext()){
- xCoord=sc.nextInt();
- yCoord=sc.nextInt();
- RottenApples apple = new RottenApples(row-xCoord,yCoord-1);
- nextInLineApples.add(apple);
- }
- LinkedList<RottenApples> helper=new LinkedList<>();
- //Comparable doesn't work ?
- for(int i=0;i<=days;i++){
- while(!nextInLineApples.isEmpty()){
- //Test queues in java
- RottenApples newApple=nextInLineApples.peek();
- rottenApples.add(newApple);
- nextInLineApples.remove();
- if(newApple.x+1 < row){
- helper.add(new RottenApples(newApple.x+1,newApple.y));
- }
- if(newApple.x-1 >=0){
- helper.add(new RottenApples(newApple.x-1,newApple.y));
- }
- if(newApple.y-1>=0){
- helper.add(new RottenApples(newApple.x,newApple.y-1));
- }
- if(newApple.y+1<column){
- helper.add(new RottenApples(newApple.x,newApple.y+1));
- }
- }
- for(RottenApples var : helper)
- nextInLineApples.add(var);
- helper.clear();
- }
- for(RottenApples var: rottenApples)
- var.print();
- System.out.println(row* column - rottenApples.size());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement