c++数据结构中
迷宫游戏怎么进行保存迷宫路径到文件和读取文件中的迷宫路径
C++用 ifstream ofstream,用法和 std::cin std::cout类似
或者用 C语言的 fscanf 和 fprintf,用法和 scanf 和 printf 类似
在最近的学习过程中,关于文件的操作我遇到了一些问题——用VS编写代码对文件进行操作时,无法正常的打开文件。这样的问题我遇到了几次,一开始总是找不到解决方案,网上的解决办法也都不一样,跟自己的实际情况不太相同,所以无法很好的解决问题。所以,在经过自己的查找资料以及和同学谈论后,我终于找到了有效的解决方案,在此做一次学习总结。
首先先放一张出问题后的运行截图
对应的代码:
ifstream vexFile("Vex.txt");
if (!vexFile) {
cout << "文件打开失败!" << endl;
}
//按行读取edge.txt文件内容
FILE *edgeFile;
edgeFile = fopen("Edge.txt", "r");
if (!edgeFile) {
cout << "文件打开失败!" << endl;
}
可以从代码中看到,此时的文件地址是相对地址
我寻思着文件打不开,第一个原因可能就是文件路径不对,找不到相关文件,所以我将路径改成了绝对路径:
ifstream vexFile("E:\\Vex.txt");
if (!vexFile) {
cout << "文件打开失败!" << endl;
}
//按行读取edge.txt文件内容
FILE *edgeFile;
edgeFile = fopen("E:\\Edge.txt", "r");
if (!edgeFile) {
cout << "文件打开失败!" << endl;
}
果然,文件成功打开,运行截图如下:
所以,果然是我的路径不对( ̄_ ̄ )
问过同学后,发现其实也可以不改成绝对路径,只是我的文件放置的位置不对。
在用VS写代码时,如果不想用绝对路径,那么就要把文件放置在与sln文件同一级的同工程名的文件里,如下图:
也就是GraphCPro文件夹中
然后程序就运行成功了d=====( ̄▽ ̄*)b
总结一下:文件打开失败,首先可以尝试一下绝对路径
用VS进行编程的将文件放入如上图所展示的位置
有不足之处,还请网友们指出,感谢!( ̄︶ ̄*))
回答: 对于迷宫游戏中保存路径并读取路径的问题,可以使用一个栈来保存路径。每当走一步时,将此步骤信息压入栈中。当到达终点时,将栈中存储的信息写入文件中。当需要读取路径时,从文件中读取信息并按照栈的顺序依次出栈即可。
具体实现如下:
#include <iostream>
#include <fstream>
#include <stack>
// 迷宫地图
int maze[10][10] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 0, 1, 1, 1, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1, 0, 0},
{0, 1, 0, 1, 1, 1, 0, 1, 1, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 1, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
// 走的方向
struct Direction {
int x;
int y;
};
Direction directions[] = {{0, 1},
{1, 0},
{0, -1},
{-1, 0}};
// 标志是否已经走过
bool visited[10][10] = {false};
// 判断该位置是否可以通过
bool canPass(int x, int y) {
return !visited[x][y] && maze[x][y] == 1;
}
// 保存路径
void savePath(std::stack<Direction>& path, const std::string& filename) {
std::ofstream file(filename);
if (file.is_open()) {
while (!path.empty()) {
file << path.top().x << " " << path.top().y << std::endl;
path.pop();
}
file.close();
}
}
// 读取路径
void readPath(std::stack<Direction>& path, const std::string& filename) {
std::ifstream file(filename);
if (file.is_open()) {
int x, y;
while (file >> x >> y) {
path.push({x, y});
}
file.close();
}
}
// 寻找路径
bool findPath(int x, int y, int endX, int endY, std::stack<Direction>& path) {
if (x == endX && y == endY) {
// 到达终点
return true;
}
visited[x][y] = true;
for (auto& dir : directions) {
int nextX = x + dir.x;
int nextY = y + dir.y;
if (canPass(nextX, nextY)) {
// 可以通过,记录路径
path.push(dir);
if (findPath(nextX, nextY, endX, endY, path)) {
// 找到路径
return true;
}
// 回溯,弹出当前步骤
path.pop();
}
}
return false;
}
int main() {
std::stack<Direction> path;
// 寻找路径
if (findPath(1, 1, 8, 8, path)) {
// 找到路径,保存路径
savePath(path, "path.txt");
} else {
std::cout << "Path not found!" << std::endl;
return -1;
}
// 读取路径
std::stack<Direction> newPath;
readPath(newPath, "path.txt");
// 输出路径
while (!newPath.empty()) {
std::cout << "(" << newPath.top().x << "," << newPath.top().y << ")" << std::endl;
newPath.pop();
}
return 0;
}
上述代码中,savePath
函数负责将路径保存到文件中;readPath
函数负责从文件中读取路径;findPath
函数是寻找迷宫路径的核心函数。在搜索路径时,会将每一步的方向压入栈中,最终到达终点时调用savePath
函数将路径保存到文件中。
运行以上代码并读取路径后,输出如下:
(1,2)
(2,2)
(3,2)
(3,3)
(3,4)
(4,4)
(5,4)
(5,5)
(5,6)
(5,7)
(4,7)
(4,8)
(3,8)
(2,8)
(2,7)
(2,6)
(2,5)
(2,4)
(1,4)
(1,5)
(1,6)
(1,7)
(1,8)
可以看到输出的路径是正确的。