迷宫代码不解决迷宫,只打印



我编写的代码有问题,我正在使用类模板和堆栈来解决问题。在我实现能够从文件中读取迷宫的代码之前,该算法运行得很好。现在它不解决迷宫,只输出"堆栈已满!无法推动"并打印迷宫。我坚持的算法是这样的:

  1. 定义给定的二维int数组M以及两个将存储行的int堆栈以及我们检查过的位置的列索引
  2. 搜索数组M以定位条目2,从而找到您的起点
  3. 推送当前位置(即行和列索引(到((堆栈上
  4. 检查当前位置存储的值是否为a 3;如果是,我们就完了。如果没有,请将其设置为2
  5. 按顺序检查当前位置的四个邻居(例如,从北顺时针方向(。如果有是1或3,将该位置弹出到堆栈上并移动到那里接下来
  6. 如果没有邻居存储1,则我们处于死胡同:将该位置的值设置为0,然后从堆栈中弹出新位置

如有任何帮助,我们将不胜感激!

#include <iostream>
#include <fstream>
#include <string>
#define MAX_STACK 10
void PrintMaze(int **Maze, int M, int N);
template <class T>
class MyStack {
private:
T *contents;
int top, maxsize;
public:
~MyStack(void);
MyStack (void);
MyStack (unsigned int StackSize);
bool checkEmpty(void);
bool checkFull(void);
void push(T c);
T pop(void );
int scon(void);
};
template <class T> MyStack<T>::~MyStack(void)
{
delete [] contents;
}
template <class T> MyStack<T>::MyStack(void)
{
top=0;
contents = new T [MAX_STACK];
maxsize = MAX_STACK;
}
template <class T> MyStack<T>::MyStack(unsigned int StackSize)
{
top=0;
maxsize = StackSize;
contents = new T[StackSize];
}
template <class T> bool MyStack<T>::checkEmpty(void)
{
return MAX_STACK == 0;
}
template <class T> bool MyStack<T>::checkFull(void)
{
return MAX_STACK == 10;
}
template <class T> void MyStack<T>::push(T c)
{
if(checkFull())
{
std::cout << "Stack is fulL! Can not push" << std::endl;
}
else
{
contents[top]=c;
top++;
}
}
template <class T> T MyStack<T>::pop(void)
{
top--;
return(contents[top]);
if(checkEmpty())
{
std::cout << "Stack empty! Can not pop" << std::endl;
return 0;
}
else
{
top--;
return(contents[top]);
}
}
template <class T> int MyStack<T>::scon(void)
{
return(*contents);
}
int main(void )
{
// Open the file
std::ifstream InFile;
InFile.open("C:/Users/Desktop/Computer Science/Maze6.txt");
if (InFile.fail())
{
std::cerr << "Error - cannot open Maze.txt" << std::endl;
return(1);
}
// Read the size of the maze
int Rows, Cols;
InFile >> Rows >> Cols;
std::cout << "The maze has " << Rows << " rows and " << Cols << " columns." << std::endl;
// Dynamically assign memory for the array
int **M = new int*[Rows];
for(int i = 0; i < Rows; ++i)
{
M[i] = new int[Cols];
}
/// Read in the data
for (int i=0; i<Rows; i++)
for (int j=0; j<Cols; j++)
{
char c;
InFile >> c;
M[i][j] = (int)(c-'0');
}
// Display the maze
std::cout << "Here is the maze: ";
PrintMaze(M, Rows, Cols);
//////////////////////////////
MyStack<int> s1;
MyStack<int> s2;
for(int Rows=0; Rows<sizeof(M); Rows++)
{
for(int Cols=0; Cols<sizeof(M); Cols++)
{
if(M[Rows][Cols] == 2)
{
s1.push(Rows);
s2.push(Cols);
if(Rows-1 == 1)
{
s1.push(Rows-1);
s2.push(Cols-1);
M[Rows-1][Cols] == 2;
}
if(Cols+1 == 1)
{
s1.push(Rows);
s2.push(Cols+1);
M[Rows][Cols+1] == 2;
}
if(Rows+1 == 1)
{
s1.push(Rows+1);
s2.push(Cols);
M[Rows-1][Cols] == 2;
}
if(Cols-1 == 1)
{
s1.push(Rows);
s2.push(Cols-1);
M[Rows][Cols-1] == 2;
}
else
{
M[Rows][Cols] = 0;
s1.pop();
s2.pop();
}
}
if(M[Rows][Cols] == 3)
{
std::cout << "Maze completed! Item found at row: " << Rows << " column: " << Cols << std::endl;
std::cout << "Column path: " << s2.scon() << std::endl;
std::cout << "Row path: " << s1.scon() << std::endl;
}
else
{
M[Rows][Cols] == 2;
}
}
}
//////////////////////////////

// Deallocate memory
for(int i =0; i<6; i++)
{
for(int j = 0; j<6; j++)
{
std::cout <<M[i][j] << ' ';
}
std::cout << std::endl;
}
return (0);
}
void PrintMaze(int **Maze, int M, int N)
{
std::cout << std::endl;
for(int i = 0; i<M; i++)
{
for(int j=0; j<N; j++)
{
std::cout << Maze[i][j];
}
std::cout << std::endl;
}
}

sizeof(M)是错误的,它会给你指针M本身的大小,而不是它所指向的大小。你需要显式使用RowsCols变量,使用它们的原始值,并使用不同的值来迭代行和列。

//////////////////////////////
MyStack<int> s1;
MyStack<int> s2;
for(int i=0; i<Rows; i++)
{
for(int j=0; j<Cols; j++)
{
// Use i and j as indexes...
}
}

最新更新