The following program runs completely differently on VS and VScode:
My VScode's configuration environment is MinGW.
I was able to run the correct result on VScode, but it was wrong on VS and
its output was problematic.
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 101;
int visit[maxn][maxn];
//int mazeArr[maxn][maxn];
int stepArr[4][2] = { {-1,0}, {1,0}, {0,-1}, {0,1} };
int n;
int mazeArr[5][5] = {
{0,0,0,0,0},
{0,1,0,1,0},
{0,1,1,1,0},
{0,1,0,0,1},
{0,0,0,0,0}
};
struct Node {
int x;
int y;
int step;
Node* parent;
Node(int x1, int y1, int step1) :x(x1), y(y1), step(step1) {}
};
bool isVisited(vector<Node> closed, struct Node* b)
{
for (int i = 0; i < closed.size(); i++)
{
if (b->x == closed[i].x && b->y == closed[i].y)
{
return true;
}
}
return false;
}
void BFS()
{
Node node(0, 0, 0);
queue<Node> q;//open表,队列存储
while (!q.empty())
q.pop();
q.push(node);
vector<Node> visited;//closed表
visited.push_back(node);
Node target(n - 1, n - 1, 0);
while (!q.empty())
{
Node* p = &q.front();
cout << "Node p的parent:"<< p->parent << endl;
//cout<<"q.front.x"<<q.front().x<< q.front().y <<endl;
q.pop();
if (p->x == n - 1 && p->y == n - 1)//若是到达终点
{
target.parent = p->parent;
cout << "target.parent:"<< target.parent << endl;
target.x = p->x;
target.y = p->y;
cout << "step"<< p->step << endl;
break;
}
for (int i = 0; i < 4; i++)
{
int x = p->x + stepArr[i][0];
int y = p->y + stepArr[i][1];
Node next(x, y, p->step + 1);
if (x >= 0 && y >= 0 && x < n && y < n && !isVisited(visited, &next) && mazeArr[x][y] == 0)
{
//Node next(x, y, p.step+1);
next.parent = p;
// cout<<"next的parent:"<<next.parent<<endl;
visited.push_back(next);
q.push(next);
// cout<<"q.front"<<q.front().x<< q.front().y <<endl;
}
}
}
//回溯路径
Node* re = ⌖
vector<Node*> result;
for (int i = 0; i < 8; i++)
{
cout << "re:"<< re->x << ""<< re->y <<endl;
result.push_back(re);
re = re->parent;
}
//路径输出
for (int i = result.size() - 1; i >= 0; i--)
{
cout << "("<< result[i]->x << ","<< result[i]->y << ")"<< "";
}
}
void main()
{
n = 5;
/* int mazeArr[5][5] = {
{0,0,0,0,0},
{0,1,0,1,0},
{0,1,1,1,0},
{0,1,0,0,1},
{0,0,0,0,0}
};*/
if (mazeArr[0][0] == 1 || mazeArr[n - 1][n - 1] == 1)
{
cout << "无解";
exit(1);
}
BFS();
//DFS();
system("pause");
}
This is the result of running on VScode: (https://i.loli.net/2019/11/11/NaifV2YlhBcRGkW.png)
This is the result of running on VS: (https://i.loli.net/2019/11/11/peEMkjOtgx72WvJ.jpg)
Since I can't use images because I don't have enough levels, I have to ask
the difference between the two compilers compiling c++ files.