嘿,我想创建一个程序来解决文件中的迷宫,并在我使用vector<string>
的程序中加载文件,它显示了SIGSEGV,Segmentation fault。
#include <bits/stdc++.h>
using namespace std;
vector<string> labyrinth;
bool visited[100][100];
void printlav(vector<string> s)
{
for(int i = 0; i < s.size(); i++)
{
for(int j = 0; j < s.at(i).size(); j ++)
{
cout << s[i][j];
}
cout << "\n";
}
}
vector<string> solve(vector<string> s,int x,int y)
{
printlav(s);
if(s[x][y] == 'K')
{
return s;
}
if(s[x - 1][y] != '#' && !visited[x - 1][y])
{
s[x - 1][y] = '+';
visited[x - 1][y] = 1;
s = solve(s, x - 1, y);
}
if(s[x + 1][y] != '#' && !visited[x + 1][y])
{
s[x + 1][y] = '+';
visited[x + 1][y] = 1;
s = solve(s, x + 1, y);
}
if(s[x][y -1] != '#' && !visited[x][y - 1])
{
s[x][y - 1] = '+';
visited[x][y - 1] = 1;
s = solve(s, x, y - 1);
}
if(s[x][y + 1] != '#' && !visited[x][y + 1])
{
s[x][y + 1] = '+';
visited[x][y + 1] = 1;
s = solve(s, x, y + 1);
}
}
int main()
{
string line;
int row = 0, rs,ks;
ifstream file_("write_here.txt");
if(file_.is_open())
{
while(getline(file_, line))
{
labyrinth.push_back(line);
for(int i = 0; i < line.size(); i++)
{
if(line[i] == 'P')
{
rs = row;
ks = i;
}
}
row ++;
}
labyrinth = solve(labyrinth, rs, ks);
for(int i = 0; i < labyrinth.size(); i++)
{
for(int k = 0; k < labyrinth[i].size(); k++)
{
cout << labyrinth[i][k];
}
}
file_.close();
}
else
cout << "File isn't open";
}
答案 0 :(得分:0)
此代码中存在很多问题,但我想分段库来自数组索引超出范围。
需要考虑的一些事项: