【发布时间】:2020-06-14 22:02:04
【问题描述】:
我正在编写代码以解决使用类模板和堆栈的迷宫问题。在我实现能够从文件中读取迷宫的代码之前,该算法运行良好。现在它没有解决迷宫,它只输出“堆栈已满!无法推送”并打印迷宫。我坚持的算法是这样的:
- 按给定定义二维 int 数组 M 上面,还有两个将存储行的 int 堆栈 和我们检查过的位置的列索引。
- 搜索数组 M 以找到条目 2 以找到您的 起点
- 推送当前位置(即行列 index) 到 () 堆栈上。
- 检查存储在当前位置的值是否为 一个 3;如果是,我们就完成了。如果不是,请将其设置为 2。
- 按顺序检查当前位置的四个邻居(例如,从北顺时针方向)。如果任何一个是 1 或 3,将该位置弹出到堆栈上并移动到那里 下一步。
- 如果没有邻居存储 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;
}
}
【问题讨论】:
-
无关:如果您不需要编写自己的堆栈和动态数组,强烈考虑使用 C++ 标准库中的
std::stack和std::vector。 -
是的,它需要编写我自己的堆栈和动态数组。谢谢。
标签: c++ class templates stack maze