Java parsing question

There is some problem with your java parser.

Given this class :

 public static class Position {
        public int x=-1;
        public int y=-1;

        public Position(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public Position() {
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Position position = (Position) o;
            return x == position.x && y == position.y;
        }

        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }

        @Override
        public String toString() {
            return "Position{" +
                    "x=" + x +
                    ", y=" + y +
                    '}';
        }
   }

when i do :

 Set<Position> visited = new HashSet<>();
[...]
  Position current_position=new Position(KC, KR);
            visited.add(current_position);
[...]
                System.err.println("visited "+visited);

I have this output :
visited [Position{x=8, y=6}, Position{x=7, y=6}, Position{x=7, y=6}, Position{x=6, y=6}, Position{x=12, y=6}, Position{x=12, y=6}, Position{x=11, y=6}, Position{x=11, y=6}, Position{x=10, y=6}, Position{x=10, y=6}, Position{x=9, y=6}, Position{x=9, y=6}, Position{x=8, y=6}]
(there is doublon, but should not have any doublon because of the datastruct)

to fix it, I have to use a strange trick :
Position current_position=new Position((KC+1), (KR+1));
and it’s work like a charm.

I’ve also noted another strange behavior. When I do :
System.err.println("world["+KR+1+"]["+(KC)+"]=" +world[KR][KC]);

i have this output :
"world[61][7]=1"instead of “world[7][7]=1”

I cannot fully explain why you have double same-item in a set because your main program and the imported classes are not shown, and there are too many irrelevant elements for debug.

Try this:
Copy and paste this code to any puzzle and run it.

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class Solution {

	public static void main(String[] args) {
		Position pos1 = new Position(7, 6);
		Position pos2 = new Position(7, 6);
		Position pos3 = new Position(7, 7);
		System.err.println("pos1 == pos2: " + (pos1 == pos2));
		System.err.println("pos1 equals pos2: " + (pos1.equals(pos2)));

		Set<Position> set = new HashSet<>();
		set.add(pos1);
		set.add(pos2);
		set.add(pos3);
		System.err.println("Added 3 pos to set");
		for (Position po : set) {
			System.err.println(po);
		}
	}
}

class Position {
	int x, y;

	public Position(int x, int y) {
		this.x = x;
		this.y = y;
	}

	@Override
	public int hashCode() {
		return Objects.hash(x, y);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Position other = (Position) obj;
		return x == other.x && y == other.y;
	}

	@Override
	public String toString() {
		return "Position [x=" + x + ", y=" + y + "]";
	}
}

I see this correct output:

Standard Output Stream:

pos1 == pos2: false
pos1 equals pos2: true
Added 3 pos to set
Position [x=7, y=6]
Position [x=7, y=7]

Let’s know if you find anything different.

Here is the full code, the line ending by “<----- here parsing issue” are the place where the parser not working properly and I have to do an obscur trick to make it work

import java.util.*;
import java.io.*;
import java.math.*;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
class Player {
   public static class Position {
        public int x=-1;
        public int y=-1;

        public Position(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public Position() {
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Position position = (Position) o;
            return x == position.x && y == position.y;
        }

        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }

        @Override
        public String toString() {
            return "Position{" +
                    "x=" + x +
                    ", y=" + y +
                    '}';
        }
   }
   static int nRow;
   static int nColumn;
   static int[][] world;
   static int wall = -1;
   static int unknow = 0;
   static int walkable =1;
   static Set<Position> visited = new HashSet<>();
    static Set<Position> toVisit = new HashSet<>();

    public static List<Position> getNeighboor(Position p) {
        List<Position> walkablePosition  = new ArrayList<Position>();
        // up 
        if((p.y-1)>=0 && world[p.y-1][p.x] == 1) {
            walkablePosition.add(new Position(p.x, p.y-1));
        } 
        // left
        if((p.x-1)>=0 && world[p.y][p.x-1] == 1) {
            walkablePosition.add(new Position(p.x-1, p.y));
        }
         // right
        if((p.x+1)<nRow && world[p.y][p.x+1] == 1) {
            walkablePosition.add(new Position(p.x+1, p.y));
        }
        // down
        if((p.y+1)<nColumn && world[p.y+1][p.x] == 1) {
            walkablePosition.add(new Position(p.x, p.y+1));
        }
        return walkablePosition;
    }
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int R = in.nextInt(); // number of rows.
        int C = in.nextInt(); // number of columns.
        int A = in.nextInt(); // number of rounds between the time the alarm countdown is activated and the time the alarm goes off.
        nRow =R;
        nColumn=C;
        System.err.println("number of rows "+nRow);
        System.err.println("number of columns "+nColumn);

       world = new int[R+1][C+1];
        boolean retpat=false;
            // init world
            for (int i=0; i <R; i++) {
                for (int j=0; j < C; j++) {
                    world[i][j] = wall;
                }
            }

        // game loop
        while (true) {
            int KR = in.nextInt(); // row where Rick is located.
            int KC = in.nextInt(); // column where Rick is located.
            System.err.println("row where Rick is located " +KR);
            System.err.println("column where Rick is located "+KC);
     
            Position current_position=new Position(KC, KR); <----- here parsing issue
            visited.add(current_position);
            Position initial_position=new Position();
            Position destination= new Position();
            for (int i = 0; i < R; i++) {
                String ROW = in.next(); // C of the characters in '#.TC?' (i.e. one line of the ASCII maze).
                char[] currentRow = ROW.toCharArray();
                for (int j=0; j <ROW.length(); j++) {
                    if(currentRow[j] == '?') {
                        world[i][j] = unknow;
                        toVisit.add(new Position(j,i));
                    } else if (currentRow[j] == '.') {
                     world[i][j] = walkable;   
                    } else if (currentRow[j] == '#') {
                     world[i][j] = wall;   
                    } else if (currentRow[j] == 'T') {
                     world[i][j] = walkable;
                     initial_position.y=i;
                     initial_position.x=j;
                    } else if (currentRow[j] == 'C') {
                     world[i][j] = walkable;
                     destination.y=i;
                     destination.x=j;
                    System.err.println("destination found: "+destination);

                    }

                }
             //   System.err.println("world["+i+"]=" +ROW);
            }
            
            
            // Write an action using System.out.println()
            // To debug: System.err.println("Debug messages...");
                 System.err.println("world["+KR+1+"]["+(KC)+"]=" +world[KR+1][KC]); <----- here parsing issue
                 System.err.println("current_p"+current_position);
                System.err.println("destinatin"+destination);
System.err.println("visited.size"+visited.size());
                System.err.println("visited "+visited);

                System.err.println("walkable "+getNeighboor(current_position));
            
            if(current_position.equals(destination)) {
                System.err.println("llego");
                retpat = true;

            }
            if(retpat) {
             System.out.println("LEFT"); // Rick's next move (UP DOWN LEFT or RIGHT).

            } else if (world[(KR)][(KC+1)] == walkable) {
                current_position.y=KR;
                current_position.x = KC+1;
            System.out.println("RIGHT"); // Rick's next move (UP DOWN LEFT or RIGHT).
            } 
            else {
                System.err.println("world["+(KR)+"]["+(KC)+"]=" +world[KR][KC]);
            }
        }
    }
}

You are modifying the current_position object after it has been added to the hashset. At the point the objects were added they are all unique (which is all the hashset can check), but because you modify them they become the same when you print the contents.

I’m creating a new instance and add it to the list. What’s the problem ?

Like I said, same code work as expected when I do

  Position current_position=new Position(KC+1, KR+1); <----- here parsing issue
            visited.add(current_position);
        } else if (world[(KR)][(KC+1)] == walkable) {
            current_position.y=KR;
            current_position.x = KC+1;
        System.out.println("RIGHT"); // Rick's next move (UP DOWN LEFT or RIGHT).
        } 

At the end you modify the object after it’s been added to the set and modifying an object already in a set in a way which changes the hash / equality checks is undefined behaviour which will can (but won’t always as you might get lucky with hash collisions) allow duplicates (Set (Java SE 11 & JDK 11 ) - see note about mutable objects)

1 Like

Oh i think you’re right.
I was so sure its a parsing issue.

Absolutely my bad, sory