
代码运行出来长这个样子,我写了3,4,个小时了,还是有问题,哪位朋友可以帮我看看吗,超级感谢
下面是我的代码
#include <iostream>
#include <cstdlib>
#include<ctime>
#include<algorithm>
using namespace std;
int map[100][100];
int v[100][100];
//0未访问,1 访问
int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };
//初始化迷宫
void initMaze(int row, int column)
{
for (int i = 0; i < row + 2; i++)
{
for (int j = 0; j < column + 2; j++)
{
if (i == 0 || i == row + 1 || j == 0 || j == column + 1)
{
map[i][j] = 1;
}
else
{
map[i][j] = 1;
v[i][j] = 0;
}
}
}
}
//DFS递归生成迷宫(带自然回溯)
void dfs(int x, int y,int row,int column)
{
v[x][y] = 1;
map[x][y] = 0;
int dirs[4] = { 0,1,2,3 };
for (int i = 0; i < 4; i++)
{
int r = rand() % 4;
swap(dirs[i], dirs[r]);
}
for (int i = 0; i < 4; i++)
{
//int k = rand() % 4;
int dir = dirs[i];
int nx = x + dx[dir] * 2;
int ny = y + dy[dir] * 2;
if (nx > 0 && nx <= row && ny > 0 && ny <= column && v[nx][ny] == 0)
{
map[x + dx[dir]][y + dy[dir]] = 0;
dfs(nx, ny, row, column);
}
}
}
//打印迷宫
void printmaze(int row,int column)
{
for (int i = 0; i < row + 2; i++)
{
for (int j = 0; j < column + 2; j++)
{
if (i ==1 && j == 0)
{
cout << "S";
}
else if (i == row && j == column + 1)
{
cout << "E";
}
else if (map[i][j] == 1)
cout << "■";
else
{
cout << " ";
}
}
cout << endl;
}
}
int main()
{
srand(time(0));
cout << "请输入迷宫的行数(<98)" << endl;
int row;
cin >> row;
cout << "请输入迷宫的列数(<98)" << endl;
int column;
cin >> column;
initMaze(row,column);
dfs(1, 1, row, column);
printmaze(row, column);
return 0;
}