gpt4 book ai didi

java - 队列删除不作为 FIFO

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:33:27 24 4
gpt4 key购买 nike

我遇到了队列问题。在我的代码中,队列当前正在添加项目,但在删除项目时遇到问题。我有 3 个类 Point 和 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 和 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 类检查了这段代码,但结果是一样的。

最佳答案

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

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

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

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

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

关于java - 队列删除不作为 FIFO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48650427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com