C++ BFS实施工作不正常
这是我的BFS实现,它总是返回0。我应该基本上找到这张地图的最短路径,1-表示这是一堵墙,0-表示没有墙。我错在哪里了C++ BFS实施工作不正常,c++,algorithm,shortest-path,C++,Algorithm,Shortest Path,这是我的BFS实现,它总是返回0。我应该基本上找到这张地图的最短路径,1-表示这是一堵墙,0-表示没有墙。我错在哪里了 class node { public: int x,y; }; int bfs(int x, int y) { node start; int result = 0; start.x = 0; start.y = 0; std::queue<node> search_queue; bool visited[4][4
class node { public: int x,y; };
int bfs(int x, int y)
{
node start;
int result = 0;
start.x = 0;
start.y = 0;
std::queue<node> search_queue;
bool visited[4][4];
int map[4][4] = {
{0,1,0,1},
{0,0,0,0},
{1,0,1,0},
{0,1,0,0}
};
for(int i = 0 ; i < 4; i ++)
{
for(int j = 0 ; j < 4; j++)
{
visited[i][j] = false;
}
}
search_queue.push(start);
while(!search_queue.empty())
{
node top = search_queue.front();
search_queue.pop();
if (visited[top.x][top.y]) continue;
if (map[top.x][top.y] == 1) continue;
if (top.x < 0 || top.x >= 4) continue;
if (top.y < 0 || top.y >= 4) continue;
visited[top.x][top.y] = true;
result++;
node temp;
temp.x = top.x+1;
temp.y=top.y;
search_queue.push(temp);
temp.x = top.x-1;
temp.y=top.y;
search_queue.push(temp);
temp.x = top.x;
temp.y=top.y+1;
search_queue.push(temp);
temp.x = top.x;
temp.y=top.y-1;
search_queue.push(temp);
}
return result;
}
类节点{public:intx,y;};
内部bfs(内部x,内部y)
{
节点启动;
int结果=0;
start.x=0;
start.y=0;
std::队列搜索\队列;
布尔访问了[4][4];
int map[4][4]={
{0,1,0,1},
{0,0,0,0},
{1,0,1,0},
{0,1,0,0}
};
对于(int i=0;i<4;i++)
{
对于(int j=0;j<4;j++)
{
访问[i][j]=错误;
}
}
搜索队列。推送(启动);
而(!search_queue.empty())
{
node top=search_queue.front();
搜索_queue.pop();
如果(访问[top.x][top.y])继续;
如果(map[top.x][top.y]==1)继续;
如果(top.x<0 | | top.x>=4)继续;
如果(top.y<0 | | top.y>=4)继续;
访问[top.x][top.y]=true;
结果++;
节点温度;
温度x=顶部x+1;
温度y=顶部y;
搜索队列推送(临时);
温度x=顶部x-1;
温度y=顶部y;
搜索队列推送(临时);
温度x=顶部x;
温度y=顶部y+1;
搜索队列推送(临时);
温度x=顶部x;
温度y=顶部y-1;
搜索队列推送(临时);
}
返回结果;
}
我从main这样调用它:coutinstrumentpath
有趣的一点是,它到达[3][3]
并继续;你似乎没有明确的目标。这是额外计数的一个原因(与预期的7相比)。[2][1]
和[0][2]
的死胡同占了另外两个。基本上,当你沿着一条死胡同走到尽头时,你需要减少result
指定终点
边界检查在查看后发生了更改
得到正确答案
检测路径
有趣的一点是,它到达[3][3]
并继续;你似乎没有明确的目标。这是额外计数的一个原因(与预期的7相比)。[2][1]
和[0][2]
的死胡同占了另外两个。基本上,当你沿着一条死胡同走到尽头时,你需要减少result
指定终点
边界检查在查看后发生了更改
得到正确答案
给定的代码生成10。经过一些修改,下面是一个示例。一个修改是将输入x
,y
设置为起点,我想这就是Jonathan Leffler上面指出的意图。第二个修改是范围检查现在在推入队列之前进行,因此while循环修改如下:
while(!search_queue.empty())
{
node top = search_queue.front();
search_queue.pop();
if (visited[top.x][top.y]) continue;
if (map[top.x][top.y] == 1) continue;
visited[top.x][top.y] = true;
result++;
node temp;
temp.y = top.y;
if (top.x < 3)
{
temp.x = top.x + 1;
search_queue.push(temp);
}
if (top.x > 0)
{
temp.x = top.x - 1;
search_queue.push(temp);
}
temp.x = top.x;
if (top.y < 3)
{
temp.y = top.y + 1;
search_queue.push(temp);
}
if (top.y > 0)
{
temp.y = top.y - 1;
search_queue.push(temp);
}
}
while(!search_queue.empty())
{
node top=search_queue.front();
搜索_queue.pop();
如果(访问[top.x][top.y])继续;
如果(map[top.x][top.y]==1)继续;
访问[top.x][top.y]=true;
结果++;
节点温度;
温度y=顶部y;
如果(顶部x<3)
{
温度x=顶部x+1;
搜索队列推送(临时);
}
如果(顶部x>0)
{
温度x=顶部x-1;
搜索队列推送(临时);
}
温度x=顶部x;
如果(顶部y<3)
{
温度y=顶部y+1;
搜索队列推送(临时);
}
如果(顶部y>0)
{
温度y=顶部y-1;
搜索队列推送(临时);
}
}
现在,假设起点在范围内(您可以为此添加另一个检查),此循环将始终在范围内移动,并且不会将超出范围的点放入队列,从而节省了大量计算
此外,在初始写入条件时,您在范围检查之前访问数组已访问的和映射,这可能会产生不良结果
最重要的是,如果您的目标是通过此地图找到最短路径,则此算法不适用。Number10
是从(0,0)开始可以访问的所有位置的数量。这不是通往任何地方的最短路径。您需要的是一个最短路径算法,因为这里的图权重是正的,所以需要一个简单的选项
这只需要对代码进行一些修改,但我将此留给您。基本上,您需要将访问的数组替换为整数数组距离
,指定从起始位置到每个点的最小距离,初始化为“无穷大”,并且仅递减。您的队列必须被优先级队列替换,以便通过增加距离访问点 给定的代码生成10。经过一些修改,下面是一个示例。一个修改是将输入x
,y
设置为起点,我想这就是Jonathan Leffler上面指出的意图。第二个修改是范围检查现在在推入队列之前进行,因此while循环修改如下:
while(!search_queue.empty())
{
node top = search_queue.front();
search_queue.pop();
if (visited[top.x][top.y]) continue;
if (map[top.x][top.y] == 1) continue;
visited[top.x][top.y] = true;
result++;
node temp;
temp.y = top.y;
if (top.x < 3)
{
temp.x = top.x + 1;
search_queue.push(temp);
}
if (top.x > 0)
{
temp.x = top.x - 1;
search_queue.push(temp);
}
temp.x = top.x;
if (top.y < 3)
{
temp.y = top.y + 1;
search_queue.push(temp);
}
if (top.y > 0)
{
temp.y = top.y - 1;
search_queue.push(temp);
}
}
while(!search_queue.empty())
{
node top=search_queue.front();
搜索_queue.pop();
如果(访问[top.x][top.y])继续;
如果(map[top.x][top.y]==1)继续;
访问[top.x][top.y]=true;
结果++;
节点温度;
温度y=顶部y;
如果(顶部x<3)
{
温度x=顶部x+1;
搜索队列推送(临时);
}
如果(顶部x>0)
{
温度x=顶部x-1;
搜索队列推送(临时);
}
温度x=顶部x;
如果(顶部y<3)
{
温度y=顶部y+1;
search_queue.push(
#include <iostream>
#include <queue>
class node { public: int x, y; };
int bfs(int bx, int by, int ex, int ey)
{
node start;
int result = 0;
start.x = bx;
start.y = by;
std::queue<node> search_queue;
bool visited[4][4];
int map[4][4] =
{
{0, 1, 0, 1},
{0, 0, 0, 0},
{1, 0, 1, 0},
{0, 1, 0, 0}
};
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
visited[i][j] = false;
}
}
search_queue.push(start);
while (!search_queue.empty())
{
node top = search_queue.front();
search_queue.pop();
if (top.x < 0 || top.x >= 4)
continue;
if (top.y < 0 || top.y >= 4)
continue;
if (visited[top.x][top.y])
continue;
if (map[top.x][top.y] == 1)
continue;
visited[top.x][top.y] = true;
std::cout << "visit: [" << top.x << "][" << top.y << "]\n";
result++;
if (top.x == ex && top.y == ey)
break;
node temp;
temp.x = top.x + 1;
temp.y = top.y;
search_queue.push(temp);
temp.x = top.x - 1;
temp.y = top.y;
search_queue.push(temp);
temp.x = top.x;
temp.y = top.y + 1;
search_queue.push(temp);
temp.x = top.x;
temp.y = top.y - 1;
search_queue.push(temp);
}
return result;
}
int main()
{
std::cout << bfs(0, 0, 3, 3);
}
visit: [0][0]
visit: [1][0]
visit: [1][1]
visit: [2][1]
visit: [1][2]
visit: [0][2]
visit: [1][3]
visit: [2][3]
visit: [3][3]
9
#include <iostream>
#include <queue>
class node { public: int x, y, l; };
int bfs(int bx, int by, int ex, int ey)
{
node start;
int result = 0;
start.x = bx;
start.y = by;
start.l = 1;
std::queue<node> search_queue;
bool visited[4][4];
int map[4][4] =
{
{0, 1, 0, 1},
{0, 0, 0, 0},
{1, 0, 1, 0},
{0, 1, 0, 0}
};
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
visited[i][j] = false;
}
}
search_queue.push(start);
while (!search_queue.empty())
{
node top = search_queue.front();
search_queue.pop();
if (visited[top.x][top.y])
continue;
if (map[top.x][top.y] == 1)
continue;
if (top.x < 0 || top.x >= 4)
continue;
if (top.y < 0 || top.y >= 4)
continue;
visited[top.x][top.y] = true;
std::cout << "visit: [" << top.x << "][" << top.y << "] = " << top.l << "\n";
result = top.l;
if (top.x == ex && top.y == ey)
break;
node temp;
temp.l = top.l + 1;
temp.x = top.x + 1;
temp.y = top.y;
search_queue.push(temp);
temp.x = top.x - 1;
temp.y = top.y;
search_queue.push(temp);
temp.x = top.x;
temp.y = top.y + 1;
search_queue.push(temp);
temp.x = top.x;
temp.y = top.y - 1;
search_queue.push(temp);
}
return result;
}
int main()
{
std::cout << bfs(0, 0, 3, 3) << std::endl;
}
visit: [0][0] = 1
visit: [1][0] = 2
visit: [1][1] = 3
visit: [2][1] = 4
visit: [1][2] = 4
visit: [0][2] = 5
visit: [1][3] = 5
visit: [2][3] = 6
visit: [3][3] = 7
7
while(!search_queue.empty())
{
node top = search_queue.front();
search_queue.pop();
if (visited[top.x][top.y]) continue;
if (map[top.x][top.y] == 1) continue;
visited[top.x][top.y] = true;
result++;
node temp;
temp.y = top.y;
if (top.x < 3)
{
temp.x = top.x + 1;
search_queue.push(temp);
}
if (top.x > 0)
{
temp.x = top.x - 1;
search_queue.push(temp);
}
temp.x = top.x;
if (top.y < 3)
{
temp.y = top.y + 1;
search_queue.push(temp);
}
if (top.y > 0)
{
temp.y = top.y - 1;
search_queue.push(temp);
}
}
std::queue<pair<node,int> > search_queue;
node top = search_queue.front().first;
int current_length = search_queue.front().second;
// if (top == end) return current_length; (this is the value you are interested in)
search_queue.add(make_pair(temp, current_length + 1));
class Queue:
def __init__(self):
self.items=[]
def enqueue(self,item):
self.items.append(item)
def dequeue(self):
return self.items.pop(0)
def isEmpty(self):
return self.items==[]
def size(self):
return len(self.items)
class Vertex:
def __init__(self,id):
self.id=id
self.color="white"
self.dist=None
self.pred=None
self.sTime=0
self.fTime=0
self.connectedTo={}
def addNeighbor(self,nbr,weight):
self.connectedTo[nbr]=weight
def __str__(self):
return str(self.id)+"connectedTo"+str([x for x in self.connectedTo])
def getConnection(self):
return self.connectedTo.keys()
def getId(self):
return self.id
def getWeight(self,nbr):
return self.connectedTo[nbr]
def setColor(self,c):
self.color=c
def getColor(self):
return self.color
def setDistance(self,d):
self.dist=d
def getDistance(self):
return self.dist
def setPred(self,u):
self.pred=u
def getPred(self):
return self.pred
def getsTime(self):
return self.sTime
def getfTime(self):
return self.fTime
def setsTime(self,t):
self.sTime=t
def setfTime(self,t):
self.fTime=t
class Graph:
def __init__(self):
self.vertList={}
self.numVertices=0
self.time=0
def addVertex(self,id):
self.numVertices=self.numVertices+1
newVertex=Vertex(id)
self.vertList[id]=newVertex
def getVertex(self,id):
if id in self.vertList:
return self.vertList[id]
else:
return None
def __contains__(self,id):
return id in self.vertList
def addEdge(self,u,v,weight=0):
if u not in self.vertList:
self.addVertex(u)
if v not in self.vertList:
self.addVertex(v)
self.vertList[u].addNeighbor(self.vertList[v],weight)
def getVertices(self):
return self.vertList.keys()
def __iter__(self):
return iter(self.vertList.values())
def bfs(self,start):
self.vertList[start].setDistance(0)
self.vertList[start].setColor("gray")
q=Queue()
q.enqueue(self.vertList[start])
while(q.size()>0):
u=q.dequeue()
u.setColor("black")
for v in u.connectedTo:
if v.getColor()=="white":
v.setColor("gray")
v.setDistance(u.getDistance()+1)
v.setPred(u)
q.enqueue(v)
print"Id ",u.getId()," color ",u.getColor()," Distance ",u.getDistance()