for循环中二维网格迷宫的显示周期



我有一个在迷宫中找到最短路径的BFS算法。我需要输出迷宫中有周期的最短路径。我用空间和墙壁设置了迷宫。墙是第254页。我的问题是,我不知道如何显示周期的路径,因为我的迷宫是一个不同的大小比我的路径向量。有人知道如何显示路径的句号吗?我所说的显示函数是显示函数,而不是displayMaze函数。下面是代码:

#include "Grid.h"
#include <cstdlib>
#include "conio.h"
using namespace std;

int main(int argc, char** argv) {
int sx, sy, tx, ty;
while(1)
{
Grid g;
g.displayMaze();
cout << "Enter start row: ";
cin >> sx;
if (sx == -1)break;
cout << "Enter start column: ";
cin >> sy;
cout << "Enter target row: ";
cin >> tx;
cout << "Enter target column: ";
cin >> ty;
g.bfs(sx, sy, tx, ty);
g.display();
cout << "Press any key to go again:nnn";
getch();
}
return 0;
}

网格头

#ifndef GRID_H
#define GRID_H
struct Node
{
bool isWall = false;
bool isVisited = false;
Node * parent = nullptr;
vector<Node*> cn;
};
class Grid {
public:
static const int SIZE = 20;
Node * maze[SIZE][SIZE];
Node * start = nullptr;
Node * target = nullptr;
vector<Node*> path;
string mazeFile =
"x x x x x x x x x x x x x x x x x x x x "
"x o o o x o x o o o o o o o x o x o o x "
"x x x o o o x x x o x x x o o o x o o x "
"x o x o x o o o x o o o x o x o o o x x "
"x o o o x x x o o o x o o o x x x o o x "
"x o x x x o x x x o o o x x x o x x x x "
"x o o x x o x x x x x o o o x o x o o x "
"x o o x x o x x x x x o o o o o o o o x "
"x x o x x o o o o o x x o x x o o o o x "
"x o o o o o x o x o o o o o o o x o x x "
"x x o o x o x x x x x x o o x o x o x x "
"x o o o x o x o o o o x o o x o x o o x "
"x x x o o o x x x o x x x o o o x x x x "
"x o x o x o o o x o x o x o x o o o x x "
"x o o o x x x o o o x o x o x x x o o x "
"x o x x x o x x x o o o x o x o x x x x "
"x o o o x o x o o o x o o o x o x o o x "
"x o o o o o o o o x x o o o o o o o o x "
"x o o x x o o o o x x x o x x o o o o x "
"x x x x x x x x x x x x x x x x x x x x "
;
Grid();
void displayMaze();
void display();
void bfs(int startRow, int startCol, int targetRow, int targetCol);
Grid(const Grid& orig);
virtual ~Grid();
private:
};
#endif /* GRID_H */

网格来源

#include "Grid.h"
Grid::Grid() {
stringstream ss(mazeFile, ios::in);
for(int i = 0; i < SIZE; i++)
{
for(int j = 0; j < SIZE; j++)
{
char buffer;
Node * n = new Node();
ss >> buffer;
if(buffer == 'x')
{
n->isWall = true;
}
maze[i][j] = n;
}
}

//connect the nodes
for(int i = 0; i < SIZE; i++)
{
for(int j = 0; j < SIZE; j++)
{
if ((i - 1) >= 0)
{
maze[i][j]->cn.push_back(maze[i-1][j]);
}
if ((i + 1) <= SIZE -1)
{
maze[i][j]->cn.push_back(maze[i+1][j]);
}
if ((j - 1) >= 0)
{
maze[i][j]->cn.push_back(maze[i][j - 1]);
}
if ((j + 1) <= SIZE -1)
{
maze[i][j]->cn.push_back(maze[i][j + 1]);
}
}
}
}
void Grid::displayMaze()
{
cout << "t0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0n";
for(int i = 0; i < SIZE; i++)
{
cout << i+1 << "t";
for(int j = 0; j < SIZE; j++)
{
if(maze[i][j]->isWall)
{
cout << char(254) << " ";
}
else
{
cout << "  ";
}
}
cout << endl;
}
}
void Grid::bfs(int startRow, int startCol, int targetRow, int targetCol)
{
start = maze[startRow][startCol];
start->isWall = maze[startRow][startCol]->isWall;
target = maze[targetRow][targetCol];
if(start->isWall == true)
{
cout << "Start is a wall" << endl;
return;
}
if(target->isWall == true)
{
cout << "Target is wall" << endl;
return;
}
Node * current = start;
int m = current->cn.size();
queue<Node *> myQueue;
myQueue.push(current);
while(!myQueue.empty())
{
current = myQueue.front();
if(current == target)
{
break;
}
for(int i = 0; i < current->cn.size(); i++)
{
if(current->cn[i]->isVisited == false)
{
myQueue.push(current->cn[i]);
current->cn[i]->isVisited = true;
current->cn[i]->parent = current;
}
if (current->cn[i] == target)
{
break;
}
}
myQueue.pop();
}
while(current != start)
{
path.push_back(current);
current = current->parent;
}
}
void Grid::display()
{
cout << "t1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0n";
for(int i = 0; i < SIZE; i++)
{
cout << i << "t";
for(int j = 0; j < SIZE; j++)
{
if(maze[i][j]->isWall)
{
cout << char(254) << " ";
}
else if(!maze [i][j]->isWall)
{
cout << "  ";
}
else if(maze[i][j] == path[i])
{
cout << "." << " ";
}
}
cout << endl;
}
}
Grid::Grid(const Grid& orig) {
}
Grid::~Grid() {
}

在路径向量中存储每个点的位置。如果你有一个字符串迷宫中位置的矢量,你只需要改变路径矢量给出的字符串迷宫中每个位置的一个点。

应用

是一个简单的for循环