迷宫的递归算法?

时间:2015-04-24 03:41:21

标签: c++

有人对如何处理此算法有任何建议吗?首先,我要说这是一个家庭作业。其次,我不是要求答案。相反,我要求指导或一点启动。我做了谷歌搜索,我找到了这个有用的网站https://www.cs.bu.edu/teaching/alg/maze/ 。我试图将他们所拥有的内容复制到我的代码中。因此,在用户输入起点后,我应检查是否允许我向上,向下,向左或向右移动。正确?

屏幕截图:http://imgur.com/vVpG83Q

所以假设用户输入3 2(行列)作为起始位置。它会he起来,看到它是'+',这意味着关闭。然后它会检查正确,这是'O',所以它将移动到那里。

有关算法的任何建议吗?同样,我不是要求解决方案。我只是要求有人指导我或者只是帮助我开始。任何帮助表示赞赏。谢谢。

Main.cpp的

    #include <fstream>
    #include <iostream>
    #include <string>
    #include "Maze.h"
    int main()
    {
      using namespace std;
      ifstream inFile;
      string fileName;
      int row, col;
      cout << "Enter file name" << endl;
      cin >> fileName;
      inFile.open(fileName.c_str());
      Maze maze(inFile);
      maze.Print();
      cout << "Enter row and col of starting position; " << endl<< "negative row stops the processing." << endl;
      cin >> row;
      while (row >= 0)
      {
        Maze anotherMaze = maze;
        cin >> col;
        if (anotherMaze.TryToEscape(row, col))
          cout << "Free" << endl;

        else
          cout << "Trapped" << endl;
        cout << "Enter row and col of starting position; " << endl<< "negative row stops the processing." << endl;
        cin >> row;
      }
      return 0;
    }

Maze.cpp

    #include "Maze.h"

#include <iostream>
#include <fstream>
#include <string>

Maze::Maze(std::ifstream& inFile)
{
  using namespace std;
  int rowIndex, colIndex;
  inFile >> maxRows >> maxCols;
  string row;
  for (rowIndex = 1; rowIndex <= maxRows; rowIndex++)
  {
    inFile >> row;
    for (colIndex = 1; colIndex <= maxCols; colIndex++)
      maze[rowIndex][colIndex] = row[colIndex-1];
    maze[rowIndex][0] = '+';
    maze[rowIndex][maxCols+1] = '+';
  }
  for (colIndex = 0; colIndex <= maxCols+1; colIndex++)
  {
    maze[0][colIndex] = '+';
    maze[maxRows+1][colIndex] = '+';
  }
}

Maze::Maze (const Maze& anotherMaze)
{
  maxRows = anotherMaze.maxRows;
  maxCols = anotherMaze.maxCols;
  for (int rowIndex = 0; rowIndex <= maxRows+1; rowIndex++)
    for (int colIndex = 0; colIndex <= maxCols+1; colIndex++)
      maze[rowIndex][colIndex] = anotherMaze.maze[rowIndex][colIndex];
}

void Maze::Print()
{
  using namespace std;
  int rowIndex, colIndex;

  cout << "Maze" << endl;
  for (rowIndex = 1; rowIndex <= maxRows; rowIndex++)
  {
    for (colIndex = 1; colIndex <= maxCols; colIndex++)
      cout << " " << maze[rowIndex][colIndex];
    cout << endl;
  }
}

void Try(char[][10], int row, int col, bool& free);

bool Maze::TryToEscape(int startRow, int startCol)
{
  bool free = false;
  Try(maze, startRow, startCol, free);
  return free;
}

void Try(char maze[][10] , int row, int col, bool& free)
{
  if (!free && (maze[row][col]) != '*' && (maze[row][col]) != '+')
    if (maze[row][col] == 'E')
      free = true;
    else
    {
      maze[row][col] = '*';
      Try(maze, row+1, col, free);
      if (!free)
        Try(maze, row-1, col, free);
      if (!free)
        Try(maze, row, col+1, free);
      if (!free)
        Try(maze, row,col-1, free);
    }
}

0 个答案:

没有答案