bool Astar::unWalk( int x,int y) { if (_MAP[x][y] == 1) return true; return false; }
时间: 2024-05-25 22:09:56 浏览: 11
这段代码是 A* 寻路算法中的一个函数,用于判断某个节点是否可以通过(即是否是障碍物)。如果该节点是障碍物,则返回 true,否则返回 false。
其中,x 和 y 表示节点的坐标,在地图中的表示是 MAP[x][y]。如果 MAP[x][y] 的值为 1,则表示该节点是障碍物,反之则不是。
该函数的命名比较不规范,建议改为 isWalkable 或者 isObstacle。
相关问题
Grid::Grid() { x = -1; y = -1; } Grid::Grid(Grid& g) { x = g.x; y = g.y; } Grid::Grid(int _x, int _y) { x = _x; y = _y; } Grid& Grid::operator=(Grid& grid) { x = grid.x; y = grid.y; return *this; } bool Grid::operator==(Grid& grid) { return (x == grid.x) && (y == grid.y); } bool Grid::operator!=(Grid& grid) { return !(*this == grid); }
这段代码是 C++ 语言编写的,实现了一个 Grid 类,该类表示一个二维坐标系中的点。类内部包含了默认构造函数、拷贝构造函数、带参数构造函数、赋值运算符重载、等于运算符重载和不等于运算符重载等方法。
1. 默认构造函数:初始化 x 和 y 坐标为 -1;
2. 拷贝构造函数:根据给定的 Grid 对象 g,创建一个新的 Grid 对象,并将其 x 和 y 坐标设置为 g 的 x 和 y 坐标;
3. 带参数构造函数:根据给定的 x 和 y 坐标,创建一个新的 Grid 对象,并将其 x 和 y 坐标设置为给定的坐标;
4. 赋值运算符重载:将一个 Grid 对象的 x 和 y 坐标赋值给另一个 Grid 对象,并返回被赋值的对象自身的引用;
5. 等于运算符重载:判断两个 Grid 对象的 x 和 y 坐标是否相等,返回布尔值;
6. 不等于运算符重载:判断两个 Grid 对象的 x 和 y 坐标是否不相等,返回布尔值。
不用pair对象改成广度优先搜索bool dfs(int to_x, int to_y, int box_x, int box_y, int people_x, int people_y) { visit2[people_x][people_y] = 1; if (to_x == people_x && to_y == people_y) { flag1 = true; return true; } if (!flag1) { for (int i = 0; i < 4; i++) { int xx = people_x + dx[i]; int yy = people_y + dy[i]; if (xx<1 || yy<1 || xx>n || yy>m || (xx == box_x && yy == box_y) || board[xx][yy] == 1 || visit2[xx][yy]) continue; dfs(to_x, to_y, box_x, box_y, xx, yy); if (flag1) return true; } } return false; }
如果你想将代码改成不使用pair对象的形式,可以使用两个分开的数组来表示坐标。下面是改写后的代码:
```cpp
bool bfs(int to_x, int to_y, int box_x, int box_y, int people_x, int people_y)
{
queue<int> qx, qy;
qx.push(people_x);
qy.push(people_y);
visit2[people_x][people_y] = 1;
while (!qx.empty() && !qy.empty())
{
int cur_x = qx.front();
int cur_y = qy.front();
qx.pop();
qy.pop();
if (cur_x == to_x && cur_y == to_y)
{
flag1 = true;
return true;
}
for (int i = 0; i < 4; i++)
{
int xx = cur_x + dx[i];
int yy = cur_y + dy[i];
if (xx < 1 || yy < 1 || xx > n || yy > m || (xx == box_x && yy == box_y) || board[xx][yy] == 1 || visit2[xx][yy])
continue;
qx.push(xx);
qy.push(yy);
visit2[xx][yy] = 1;
}
}
return false;
}
```
这个改写后的代码使用了两个分开的队列 `qx` 和 `qy` 来分别存储横纵坐标。在每次取出队列中的一个点时,同时取出对应的横纵坐标进行处理。其他逻辑与之前给出的代码相同。希望这个改写对你有帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)