Java 中的队列 - 我的实现有什么问题,我可以使用什么?
我正在尝试进行广度优先搜索来解决方块移动难题(将方块移动到空白空间直到解决的难题)。我的广度优先算法使用队列。不幸的是,它似乎只适用于向上和向下的情况,而不是向左或向右的情况:
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));
}
我认为这是我的队列的问题,其实现如下。我的队列有问题还是其他问题? Java API 是否有我可以使用的队列类?
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JDK 提供了 Queue 接口和许多实现,可以在
队列
文档。出于您的目的,
LinkedList
应该足够好了。The JDK provides a
Queue
interface and a number of implementations, which can be found in the "All Known Implementing Classes" section of theQueue
documentation.For your purposes, a
LinkedList
should probably be good enough.我想,问题出在“前”和“后”上。它们一开始需要指向同一个位置0,所以当它们相等时,队列为空。 “后”将指向要写入的位置(写入,然后向前移动),“前”将指向要读取的位置(读取然后移动)。当队列已满时,“后方”似乎前进得更快。到了最后,又会回到乞讨。因此,您需要执行 (rear + 1) % max == front 来检查是否可以安全地插入新项目。
另外,请考虑使用一些标准实现,正如 Siddiqui 先生建议的那样。
I guess, that the issue is with 'front' and 'rear'. They need to point to the same position 0 in the beginning, so when they are equal, the queue is empty. 'Rear' will point to position to write (write, and then shift forward) and 'front' to position to read (read and then shift). 'Rear' seems to move ahead faster while the queue is filling up. When it goest to the end, it will return to the begging. So you'll need to perform (rear + 1) % max == front to check, if you can safely insert a new item.
Also, consider to use some of the standart implementations, as Mr.Siddiqui suggested.