Reputation:
I'm trying to upadte my validity checker for my Sudoku solver, but I'm not really sure how to use Stacks
in the way that I want. I already know how the basics when it comes to Stacks
, but I'm trying to store multiple values at one index.
My Sudoku board is stored in a 3D array, so in my stack I need to store the value, sub-square where the value was found, row it was found in and column it was found in.
So suppose that my array was this:
int[][][] solved = {{{5,3,4},{6,7,2},{1,9,8}}, //Let's say I want to check if 5 is in the right position.
{{6,7,8},{1,9,5},{3,4,2}},
{{9,1,2},{3,4,8},{5,6,7}},
{{8,5,9},{4,2,6},{7,1,3}},
{{7,6,1},{8,5,3},{9,2,4}},
{{4,2,3},{7,9,1},{8,5,6}},
{{9,6,1},{2,8,7},{3,4,5}},
{{5,3,7},{4,1,9},{2,8,6}},
{{2,8,4},{6,3,5},{1,7,9}}};
The information would be [5,0,0,0].
I already have a working checker but now I need to know how to store multiple values at a single index in a Stack
. This way if it is in the wrong position, I can just pop it off and try a different number.
Any insight will be appreciated!
Upvotes: 2
Views: 1847
Reputation: 17568
Create an object that encapsulates the group of values. The class should mark the fields private
and provide getters and setters, but I've left it out for brevity.
I also don't know the types for the data, I'm assuming int
.
public class Move
{
public int value;
public int row;
public int subSquare;
}
Then you will have Stack<Move>
Then you can add Move
s to the stack:
Stack<Move> stack = ...
Move move = new Move();
move.value = 1;
move.row = 2;
move.subSquare = 3;
stack.push(move);
Upvotes: 1