[英]Breadth-First Search 8 Tile Game
我创建了基于广度优先搜索的8格游戏。 让我解释一下游戏:
您将得到一个由8个整数组成的数组:
012
348
675
并给定一个整数m,该整数告诉您哪个图块为空白,在本示例中,假设它为8。您只能使用所需的方法将此图块与该图块直接在其上方,从右下方到下方的位置交换对数组进行排序,如下所示:
012
345
678
然后,您需要将空白空间纳入向量中。 因此,在上面的示例中,向量将为5,8,因为空格在排序时从pos#5开始,到pos#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;
}
}
}
此代码是完全一样的一个,我最近回答了,检查这并相应地更新你的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.