Your problem is that you overwrite your values in your initial HashMap
. Where you set neighborQueenLocations
to be initialQueenLocations
, you basically just set a reference to your initialQueenLocations
HashMap. So When you do neighborQueenLocations.put(k, initialLocation - 1);
, you write to the memory which is reserved by the initialQueenLocations
, but "accessing" it through your neighborQueenLocations
variable.
...
for(int i = 0; i < queen; i++){
int[][] neighborBoard = new int[queen][queen];
// Here you are setting a reference, not copying the values
HashMap<Integer, Integer> neighborQueenLocations = initialQueenLocations;
...
And later in your code, you are overwriting the value in your initialQueenLocations
HashMap, since neighborQueenLocations
is just reference to your initialQueenLocations
.
...
neighborBoard[k][initialLocation] = 0;
neighborBoard[k][initialLocation - 1] = 1;
neighborQueenLocations.put(k, initialLocation - 1);
...
That's why it "remembers" the last step taken.