我创建了一个基于广度优先搜索的8平铺游戏。让我解释一下游戏:
您将获得一个包含8个整数的数组:
012
348
675
并给出一个整数m,它告诉你哪个瓷砖是空白的,在这个例子中我们说它将是8.你只能将这个瓷砖与瓷砖直接在其上方,下方,从右到左交换,使用这个您需要对数组进行排序的方法,如下所示:
012
345
678
您需要将空白区域的步骤放入向量中。因此,在上面的例子中,向量将是5,8,因为空白空间从位置#5开始,并在排序时结束于位置#8。我创建了排序代码,现在我无法弄清楚如何将位置添加到向量中,这是我的代码:
package application;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Vector;
public class Solution {
/******************************************
* Implementation Here
***************************************/
/*
* Implementation here: you need to implement the Breadth First Search
* Method
*/
/* Please refer the instruction document for this function in details */
public static LinkedHashSet<int[]> OPEN = new LinkedHashSet<int[]>();
public static HashSet<int[]> CLOSED = new HashSet<int[]>();
public static boolean STATE = false;
public static int empty;
public static void breadthFirstSearch(int[] num, int m, Vector solution1) {
//int[] start = num;
int[] start = {0,1,2,3,4,5,6,8,7};
m = 7;
int[] goal = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
int[] X;
int[] temp = {};
OPEN.add(start);
while (OPEN.isEmpty() == false) {
X = OPEN.iterator().next();
OPEN.remove(X);
for (int i = 0; i < X.length; i++) {
if (X[i] == m) {
empty = i;
System.out.println("empty = " + empty);
solution1.addElement(empty);
}
}
// get position of ZERO or EMPTY SPACE
if (compareArray(X, goal)) {
System.out.println("SUCCESS");
for(Object i : solution1) {
System.out.println((int)i);
}
print(X);
break;
} else {
//print(X);
System.out.println("------");
// generate child nodes
CLOSED.add(X);
int[][] y = new int[4][9];
for (int i = 0; i < 4; i++)
for (int i1 = 0; i1 < 9; i1++)
y[i][i1] = X[i1];
temp = up(y[0], empty);
if (temp != null) {
OPEN.add(temp);
System.out.println("doing up");
System.out.println("added "+ empty);
// print(temp);
}
temp = left(y[1], empty);
if (temp != null) {
OPEN.add(temp);
System.out.println("doing left");
System.out.println("added "+ empty);
// print(temp);
}
temp = down(y[2], empty);
if (temp != null) {
OPEN.add(temp);
System.out.println("doing down");
System.out.println("added "+ empty);
// print(temp);
}
temp = right(y[3], empty);
if (temp != null) {
OPEN.add(temp);
System.out.println("doing right");
System.out.println("added "+ empty);
// print(temp);
}
if (OPEN.isEmpty()) {
// System.out.println("Ending loop");
}
}
}
}
public static void print(int[] arr) {
System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);
System.out.println(arr[3] + " " + arr[4] + " " + arr[5]);
System.out.println(arr[6] + " " + arr[7] + " " + arr[8]);
System.out.println("---------");
}
public static boolean compareArray(int[] a, int[] b) {
for (int i = 0; i < a.length; i++)
if (a[i] != i)
return false;
return true;
}
public static int[] up(int[] s, int p) {
boolean b = false;
int[] str = s;
if (p > 3) {
int temp = str[p - 3];
str[p - 3] = str[p];
str[p] = temp;
b = true;
}
// Eliminates child of X if its on OPEN or CLOSED
int[] t = new int[9];
for (int i = 0; i < str.length; i++)
t[i] = str[i];
if (!OPEN.contains(t) && CLOSED.contains(t) == false && b)
return str;
else {
System.out.println("returning null for up");
return null;
}
}
public static int[] down(int[] s, int p) {
int[] str = s;
boolean b = false;
if (p < 6) {
int temp = str[p + 3];
str[p + 3] = str[p];
str[p] = temp;
b = true;
}
int[] t = new int[9];
for (int i = 0; i < str.length; i++)
t[i] = str[i];
if (!OPEN.contains(str) && CLOSED.contains(str) == false && (b))
return str;
else {
System.out.println("returning null for down");
return null;
}
}
public static int[] left(int[] s, int p) {
int[] str = s;
boolean b = false;
if (p != 0 && p != 3 && p != 6) {
int temp = str[p - 1];
str[p - 1] = str[p];
str[p] = temp;
b = true;
}
// Eliminates child of X if its on OPEN or CLOSED
int[] t = new int[9];
for (int i = 0; i < str.length; i++)
t[i] = str[i];
if (!OPEN.contains(str) && CLOSED.contains(str) == false && (b))
return str;
else {
System.out.println("returning null for left");
return null;
}
}
public static int[] right(int[] s, int p) {
boolean b = false;
int[] str = s;
if (p != 2 && p != 5 && p != 8) {
int temp = str[p + 1];
str[p + 1] = str[p];
str[p] = temp;
b = true;
}
// Eliminates child of X if its on OPEN or CLOSED
int[] t = new int[9];
for (int i = 0; i < str.length; i++)
t[i] = str[i];
if (!OPEN.contains(t) && CLOSED.contains(t) == false && (b))
return str;
else {
System.out.println("returning null for right");
return null;
}
}
}