Java中的队列 - 我的实现有什么问题,我可以使用什么?

时间:2011-11-29 01:12:24

标签: java queue breadth-first-search 8-puzzle

我正在尝试进行广度优先搜索以解决方形移位拼图(将方块移动到空白空间直到它被解决的那个)。我的广度优先算法使用队列。不幸的是,它似乎只适用于UP和DOWN案例,而不是LEFT或RIGHT案例:

                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;
}

2 个答案:

答案 0 :(得分:2)

JDK提供了Queue接口和许多实现,可以在Queue documentation的“所有已知实现类”部分找到。

出于您的目的,LinkedList应该足够好了。

答案 1 :(得分:0)

我猜,问题在于“前方”和“后方”。他们需要在开头指向相同的位置0,所以当它们相等时,队列为空。 'Rear'将指向写入(写入,然后向前移动)和“front”到位置读取(读取然后移位)的位置。当队列填满时,“后方”似乎更快地向前移动。当它走到尽头时,它将回到乞讨。因此,如果您可以安全地插入新项目,则需要执行(后+ 1)%max ==前检查。

另外,考虑使用一些标准实现,正如Mr.Siddiqui建议的那样。