java-队列删除不能用作FIFO

我面临队列问题.在我的代码中,队列当前正在添加项目,但是在删除项目时遇到问题.我有3个Point and Queue,C类.Point类保存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 and 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个项目.在最后阶段,还有一些不必要的删除.我找不到任何理由如此行事.谁能帮我吗?

N.B:我也用Java的默认Queue类检查了此代码,但结果是相同的.

最佳答案

您的队列(或与此相关的任何队列)出现意外行为的原因,因此在执行BFS时出现意外行为的原因是,您没有将“新”对象添加到队列中.

 

在BFS的开始处,您将精确地创建1点:

 

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

然后在整个BFS中,您将继续操纵同一对象.当在BFS中,图中的某个点存在多个边(输出中的点#6)时,这将成为问题.当您覆盖同一点时,队列丢失了要遍历的节点的正确状态.

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


Inm小程序商店

 

Inm小程序商店收录了最新,最热门的微信小程序和微信小游戏,是国内内容最丰富的集小程序游戏、小程序分发、小程序推广为一体的综合性小程序门户网站之一。


Vultr中文网

 

最低 $2.5/月 的VPS, 稳定, 可靠

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值