try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(insertTableSQL);
byte[] initialState = {1, -1, -1, -1, 5, 6, -1, -1, 9, 10, -1, -1, 13, -1, -1, 0};
static StateSet<State> sSET = new State(0, initialState);
QUEUE.addHashSet<State>(s);
State current = null;{
private static final intlong numInsertedserialVersionUID = 0;1L;
while (!QUEUE.isEmpty()) {
numInserted++;@Override
public ifboolean contains(numInserted % 20000 ==Object 0obj) {
System.out.println(numInserted + "State "v += current.getMoves()State);
}obj;
current =for QUEUE.remove();
State mad : SET.add(currentthis); {
preparedStatementif (Arrays.setBytesequals(1, currentmad.getState());
preparedStatement.setInt(2, currentv.getMovesgetState());) {
preparedStatement.executeUpdate();
for (State n : findNeighbours(current)) {
if (!SET.contains(n)) {
SET.add(n);
if (!QUEUE.contains(n)) {
QUEUE.add(n);
}
return }true;
}
}
System.out.println("Recorded inserted :D");
} catch (SQLException e) {
System.out.println(e);
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
return false;
}
public static ArrayList<State> findNeighbours(State currentState) {
byte[] state = currentState.getState();
ArrayList<State> neighbours = new ArrayList<>();
for (int i = 0; i < state.length; i++) {
if (state[i] == 0) {
if (i % 4 != 0) {
byte[] left = new byte[16];
System.arraycopy(state, 0, left, 0, left.length);
byte temp = left[i];
left[i] = left[i - 1];
left[i - 1] = temp;
State s = new State(currentState.getMoves() + 1, left);
neighbours.add(s);
}
if (i % 4 != 3) {
byte[] right = new byte[16];
System.arraycopy(state, 0, right, 0, right.length);
byte temp = right[i];
right[i] = right[i + 1];
right[i + 1] = temp;
State s = new State(currentState.getMoves() + 1, right);
neighbours.add(s);
}
if (i > 3) {
;
static byte[]Queue<State> upQUEUE = new byte[16];
System.arraycopyLinkedList<>(state, 0, up, 0, up.length);
byte temp = up[i];
up[i] = up[i - 4];
up[i - 4] = temp;
State spublic =static newvoid State(currentState.getMovesmain() + 1,String[] upargs);
throws SQLException {
// TODO code application logic neighbours.add(s);here
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String insertTableSQL = "INSERT INTO six_six_three.six"
+ "(STATE, VALUE) VALUES"
+ "(?,?)";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(insertTableSQL);
byte[] initialState = {1, -1, -1, -1, 5, 6, -1, -1, 9, 10, -1, -1, 13, -1, -1, 0};
State s = new State(0, initialState);
QUEUE.add(s);
State current = null;
int numInserted = 0;
while (!QUEUE.isEmpty()) {
numInserted++;
if (numInserted % 20000 == 0) {
System.out.println(numInserted + " " + current.getMoves());
}
current = QUEUE.remove();
SET.add(current);
preparedStatement.setBytes(1, current.getState());
preparedStatement.setInt(2, current.getMoves());
preparedStatement.executeUpdate();
for (State n : findNeighbours(current)) {
if (!SET.contains(n)) {
SET.add(n);
if (!QUEUE.contains(n)) {
QUEUE.add(n);
}
}
}
}
System.out.println("Recorded inserted :D");
} catch (SQLException e) {
System.out.println(e);
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
if (i < 12) {
byte[] down = new byte[16];
System.arraycopy(state, 0, down, 0, down.length);
byte temp = down[i];
down[i] = down[i + 4];
down[i + 4] = temp;
State s = new State(currentState.getMoves() + 1, down);
neighbours.add(s);
public static ArrayList<State> findNeighbours(State currentState) {
byte[] state = currentState.getState();
ArrayList<State> neighbours = new ArrayList<>();
for (int i = 0; i < state.length; i++) {
if (state[i] == 0) {
if (i % 4 != 0) {
byte[] left = new byte[16];
System.arraycopy(state, 0, left, 0, left.length);
byte temp = left[i];
left[i] = left[i - 1];
left[i - 1] = temp;
State s = new State(currentState.getMoves() + 1, left);
neighbours.add(s);
}
if (i % 4 != 3) {
byte[] right = new byte[16];
System.arraycopy(state, 0, right, 0, right.length);
byte temp = right[i];
right[i] = right[i + 1];
right[i + 1] = temp;
State s = new State(currentState.getMoves() + 1, right);
neighbours.add(s);
}
if (i > 3) {
byte[] up = new byte[16];
System.arraycopy(state, 0, up, 0, up.length);
byte temp = up[i];
up[i] = up[i - 4];
up[i - 4] = temp;
State s = new State(currentState.getMoves() + 1, up);
neighbours.add(s);
}
if (i < 12) {
byte[] down = new byte[16];
System.arraycopy(state, 0, down, 0, down.length);
byte temp = down[i];
down[i] = down[i + 4];
down[i + 4] = temp;
State s = new State(currentState.getMoves() + 1, down);
neighbours.add(s);
}
}
}
}
return neighbours;
}
public static class State {
int moves;
byte[] composition;
public State(int moves, byte[] composition) {
this.moves = moves;
this.composition = composition;
}
public int getMoves() {
return moves;
}
public void setMoves(int moves) {
this.moves = moves;
}
public static class State {
int moves;
byte[] composition;
public State(int moves, byte[] composition) {
this.moves = moves;
this.composition = composition;
}
public int getMoves() {
return moves;
}
public void setMoves(int moves) {
this.moves = moves;
}
public byte[] getState() {
return composition;
}
}
}