【问题标题】:Queue remove not working as FIFO队列删除不能作为 FIFO 工作
【发布时间】:2018-02-06 19:20:19
【问题描述】:

我遇到了队列问题。在我的代码中,队列当前正在添加项目,但在删除项目时遇到问题。我有 3 类点和队列,C。点类保存 x、坐标和距离的值。而队列类是用数组实现的。

这是我的功能代码。

public class wormhole {
    public static int [][] ara=new int [21][21];
    public static boolean[][] visited=new boolean[21][21];
    public static int [][] ans=new int[21][21];
    public static int [] nx={1,-1,0,0};
    public static int [] ny={0,0,1,-1};
    public static int n;


    public static boolean is_safe(int x,int y){
        if(x>=0 && x<n&& y>=0&&y<n && ara[x][y]==1&& !visited[x][y])
            return true;
        return false;
    }
    public static  void BFS(int x,int y){
        Queue queue=new Queue(1000);
        Point in1=new Point(x,y,0);
        in1.x=x;
        in1.y=y;
        in1.dis=0;
        queue.add(in1);
        visited[x][y]=true;


        while(!queue.is_Empty(queue)) {
            Point r = queue.remove();
            int a = r.x;
            int b = r.y;
            int d = r.dis;

           System.out.printf("remove %d %d %d\n",a,b,d);

            ans[a][b] = d;
            for (int i = 0; i < 4; i++) {
                int nxx = a + nx[i];
                int nyy = b + ny[i];
                if (is_safe(nxx, nyy)) {

                    visited[a][b] = true;
                    in1.x = nxx;
                    in1.y = nyy;
                    in1.dis = d + 1;
                    System.out.printf("add %d %d %d\n",in1.x,in1.y,in1.dis);
                    queue.add(in1);
                }
            }
        }
    }
 public static void main(String [] args){
        Scanner in = new Scanner(System.in);
        n=in.nextInt();



        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                ara[i][j]=in.nextInt();
            }
        }

        int q=in.nextInt();

        C rare[]=new C[q];

        for(int i=0;i<q;i++)
        {

            int a,b;
            a=in.nextInt();
            b=in.nextInt();

            rare[i]=new C(a,b);

        }
BFS(0,2);
}
}

我的 Point 和 Queue 类如下所示。

class Point{
    public int x,y,dis;
    Point(int x,int y,int dis){
        this.x=x;
        this.y=y;
        this.dis=dis;
    }
}
class Queue {
    int front, rear, size, capacity;
    Point[] ara;

    public Queue(int capacity) {
        this.capacity = capacity;
        front = this.size = 0;
        rear = capacity - 1;
        ara = new Point[this.capacity];
    }

    public boolean is_Full(Queue queue) {
        return (queue.size == queue.capacity);

    }

    public boolean is_Empty(Queue queue) {
        return (queue.size == 0);
    }

    public void add(Point item) {
        if (is_Full(this))
            return;
        this.rear = (this.rear + 1) % this.capacity;
        ara[this.rear] = item;
        this.size = this.size + 1;
    }

    public Point remove() {
        if (is_Empty(this))
            return null;
        Point item = this.ara[this.front];
        this.front = (this.front + 1) % this.capacity;
        this.size = this.size - 1;
        return item;

    }
    public Point  front() {
        if(is_Empty(this))
            return new Point(-1,-1,-1);
        return(this.ara[this.front]);
    }
    public Point rear(){
        if(is_Empty(this))
            return new Point(-1,-1,-1);
        return(this.ara[this.rear]);
    }
}
class C{
    public int x,y;
    C(int x,int y){
        this.x=x;
        this.y=y;
    }
}

示例输入和输出

 8
    0 0 1 0 0 1 0 0
    0 0 1 0 1 1 1 1
    0 0 1 0 1 0 0 1
    0 0 1 1 1 0 0 0
    0 1 0 0 1 1 1 1
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    3
    0 2
    1 7
    4 7

output
remove 0 2 0
1.add 1 2 1
remove 1 2 1
2.add 2 2 2
remove 2 2 2
3.add 3 2 3
remove 3 2 3
4.add 3 3 4
remove 3 3 4
5.add 3 4 5
remove 3 4 5
6.add 4 4 6
7.add 2 4 6
remove 2 4 6
8.add 1 4 7
remove 1 4 7
9.add 1 5 8
remove 1 5 8
10.add 0 5 9
11.add 1 6 9
remove 1 6 9
12.add 1 7 10
remove 1 7 10
13.add 2 7 11
remove 2 7 11
remove 2 7 11
remove 2 7 11

队列正在正确插入项目。但是在第 6 次和第 7 次插入之后,它删除了唯一的第 7 项。在最后阶段,还有一些不必要的删除。我找不到任何理由这样做。谁能帮帮我?

注意:我也用 java 的默认 Queue 类检查了这段代码,但结果是一样的。

【问题讨论】:

    标签: java queue


    【解决方案1】:

    您的队列(或任何队列)出现意外行为以及执行 BFS 的意外行为的原因是您没有将“new”对象添加到队列.

    在 BFS 开始时,您恰好创建了 1 个点:

    Point in1=new Point(x,y,0);
    

    然后在整个 BFS 中,您一直在操作 同一个对象。当在您的 BFS 中,这成为一个问题,图中有一个点有多个边(输出中的点 #6)。当你覆盖同一个点时,Queue 已经丢失了 Nodes 的正确状态来遍历。

    解决方案是每次添加到队列时创建一个新点(x,y,d)

    【讨论】:

      【解决方案2】:

      您可以使用ArrayDeque,您可以在两端添加/删除。

      【讨论】:

        猜你喜欢
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        • 2017-01-27
        • 2011-08-26
        • 1970-01-01
        • 2020-10-08
        • 2018-06-20
        • 2014-12-08
        相关资源
        最近更新 更多