以下程序在VS和VScode上的运行方式完全不同:
我的VScode的配置环境是MinGW。
我能够在VScode上运行正确的结果,但是在VS和
上却是错误的其输出有问题。
#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,-1},1} };
int n;
int mazeArr[5][5] = {
{0,1,1},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);
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,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}
};*/
if (mazeArr[0][0] == 1 || mazeArr[n - 1][n - 1] == 1)
{
cout << "无解";
exit(1);
}
BFS();
//DFS();
system("pause");
}
这是在VScode上运行的结果: (https://i.loli.net/2019/11/11/NaifV2YlhBcRGkW.png)
这是在VS上运行的结果: (https://i.loli.net/2019/11/11/peEMkjOtgx72WvJ.jpg)
由于我没有足够的等级而无法使用图像,所以我不得不问
两个编译c ++文件的编译器之间的区别。