Java中的队列-What';我的实现有什么问题吗';我可以用哪一个?
我试图做一个广度优先的搜索来解决一个方块移动的难题(你把方块移动到一个空白区域直到它被解决)。我的广度优先算法使用队列。不幸的是,它似乎只适用于上下两种情况,而不适用于左右两种情况:Java中的队列-What';我的实现有什么问题吗';我可以用哪一个?,java,queue,breadth-first-search,sliding-tile-puzzle,Java,Queue,Breadth First Search,Sliding Tile Puzzle,我试图做一个广度优先的搜索来解决一个方块移动的难题(你把方块移动到一个空白区域直到它被解决)。我的广度优先算法使用队列。不幸的是,它似乎只适用于上下两种情况,而不适用于左右两种情况: if (up) { int[][] current = copy(v.state); current[x][y] = current[x - 1][y]; curr
if (up)
{
int[][] current = copy(v.state);
current[x][y] = current[x - 1][y];
current[x - 1][y] = 0;
State w = new State(current);
w.distance = v.distance + 1;
w.path = v;
System.out.println(q.insert(w));
}
if (down)
{
int[][] current = copy(v.state);
current[x][y] = current[x + 1][y];
current[x + 1][y] = 0;
State w = new State(current);
w.distance = v.distance + 1;
w.path = v;
System.out.println(q.insert(w));
}
if (left)
{
int[][] current = copy(v.state);
current[x][y] = current[x][y - 1];
current[x][y - 1] = 0;
State w = new State(current);
w.distance = v.distance + 1;
w.path = v;
System.out.println(q.insert(w));
}
if (right)
{
int[][] current = copy(v.state);
current[x][y] = current[x][y + 1];
current[x][y + 1] = 0;
State w = new State(current);
w.distance = v.distance + 1;
w.path = v;
System.out.println(q.insert(w));
}
我认为这是我的队列的一个问题,其实现如下。我的队列有问题吗,还是另一个问题?JavaAPI是否有我可以使用的队列类
public class ArrayQueue {
State[] items;
int maxSize;
int front;
int rear;
int numItems;
public ArrayQueue(int max)
{
items = new State[max];
maxSize = max;
front = 0;
rear = -1;
numItems = 0;
}
public boolean insert(State item)
{
if (isFull()) return false;
rear = (rear + 1) % items.length;
items[rear] = item;
return true;
}
public State remove()
{
if (isEmpty()) return null;
State removed = items[front];
front = (front + 1) % items.length;
return removed;
}
public boolean isFull()
{
if ((front + 1) % maxSize == rear)
return true;
else
return false;
}
public boolean isEmpty()
{
if ((rear + 1) % maxSize == front)
return true;
else
return false;
}
}
以下是复制方法:
public static int[][] copy(int[][] input) //This method is necessary because we are trying to clone a multi-dimensional array.
{ //Just using clone() will copy the outer arrays but they will be arrays of references to the original inner arrays.
int[][] output = new int[input.length][];
for (int i = 0; i < input.length; i++)
output[i] = input[i].clone();
return output;
}
publicstatic int[]]copy(int[]]input)//此方法是必需的,因为我们正在尝试克隆多维数组。
{//仅使用clone()将复制外部数组,但它们将是对原始内部数组的引用数组。
int[][]输出=新的int[input.length][];
for(int i=0;i
JDK提供了一个队列
接口和许多实现,可以在“所有已知的实现类”部分找到
就您的目的而言,a可能已经足够好了。我想,问题在于“前”和“后”。它们需要在开始时指向相同的位置0,因此当它们相等时,队列为空。”“后”将指向写入位置(写入,然后向前移动),而“前”将指向读取位置(读取,然后向前移动)。”“后方”似乎在排队时向前移动得更快。当它走到尽头时,它会回到乞讨的地方。因此,如果可以安全地插入新项目,则需要执行(后+1)%max==前检查
也考虑使用一些标准化的实现,正如Siddiqui先生建议的。< /P>怎么样?java API有一个我可以使用的队列类吗?你找过吗?这些都是在线的,你应该把它们标记为书签。如果你的队列适用于向上/向下,但不适用于左/右,那么为什么你会怀疑你的队列呢?对我来说,向上/向下案例和左/右案例之间最显著的区别是前者将正方形从一个
int[]
移动到另一个,后者在同一int[]
内移动正方形。这让我怀疑问题是否真的存在于你的copy
方法中;如果它只做一个深度复制,你会看到这两种情况之间的不同行为。@HotLicks,没有必要这么嘲笑。当我搜索Google时,我看到的只是一个接口,而当我试图使用它时,我得到了错误消息“无法实例化Queue类型”。Queue q=new LinkedList();是我用的。谢谢