简体   繁体   English

穿越迷宫

[英]Traversing a Maze

I am creating a maze game that is to be traversed and solved by the machine. 我正在创建一个迷宫游戏,它将被机器遍历和解决。 I have created a maze class that contains the starting and ending positions of the maze as well as the maze itself which is contained in a 2d vector of bools. 我创建了一个迷宫类,其中包含迷宫的开始和结束位置以及迷宫本身,该迷宫本身包含在二维向量布尔中。 What I am getting tripped up on is how to actually code moving up and down and across the maze to get to the finish. 我被绊倒的是如何实际编码上下移动以及穿过迷宫到达终点的代码。 My starting point is [11][4] in the maze and our professor has told us the best way to move about is to check all 4 locations around the current position and if its true (aka it is a path and not a wall) push it onto the stack. 我的出发点是迷宫中的[11] [4],我们的教授告诉我们最好的移动方法是检查当前位置周围的所有4个位置,以及是否正确(即是路径而不是墙)将其推入堆栈。 I understand conceptually what this means but I can't visualize how to code it properly, any help would be appreciated. 我从概念上了解这意味着什么,但我无法想象如何正确编写代码,我们将不胜感激。 FYI, there is a location struct that simplifies how to express a location. 仅供参考,有一个位置结构可简化如何表达位置。

struct Location  {
    friend std::ostream &operator <<(std::ostream &os, const Location &location) {
        os << "(" << location.x << ", " << location.y << ")";
        return os;
    }
    bool operator ==(const Location &rhs) const {return x == rhs.x && y == rhs.y;}
    bool operator !=(const Location &rhs) const {return !(*this == rhs);}
    operator bool() const {return x >= 0;}
    Location(int x=-1, int y=-1) : x(x), y(y) {}
    int x, y;
};

class Maze;

Maze load(std::string filename);

class Maze {
    friend std::ostream &operator <<(std::ostream &os, Maze &maze);
    friend Maze load(std::string filename);
  public:
    Maze(std::vector<std::vector<bool> > specifics, const Location &startPos, const           Location &endPos);
    bool solve();
    //void run();
  private:
    bool contains(const Location &location) const;
    bool isPath(const Location &location) const;
    int height() {return spec.size();}
    int width() {return spec[0].size();}
    std::vector<std::vector<bool> > spec;
    Location start, finish;
    Location current;
};


bool Maze::solve() {
    stack<Location> location; //vector to hold location objects/paths
    Location current; //will hold current position in maze
    current = start; //set current to start position in beginning
    location.push(current); //push first maze location (start) onto vector
    //cout << current;
    ///need to set current to top of stack; while traversing maze current is top of stack and if current hits (==) finish return true
    while (!location.empty()) //while location still has values inside
    {
        current=location.top();//set current to top of stack
        cout << current << endl;
        cout << spec[6][4];
        if (current==finish) //if current == finish the maze is solved
            return true;
        // for loop to start moving around... but how?        
    }
}

here is a pseudo code; 这是一个伪代码;

1. push the starting location in a stack ( you can use std::stack)
2. while end location is not reached and stack is not empty
    2.1  search all 4 surrounding locations of *top* location one by one
           if any of them is a path, push it to stack, go 2.1
           if none of them is a path, pop and remove top element from stack. go 2


3. if stack if empty and end location is not reached, maze can not be solved.

Edit 编辑

This is roughly the code (note, I did not compile it) 这大致就是代码(注意,我没有编译)

bool solve(Location currentLocation, Location endLocation)
{
  if(currentLocation == endLocation)
  {
    return true;
  }

  Location newLoc1(currentLocation.x-1,currentLocation.y-1); 
  if(contains(newLoc) && isPath(newLoc))
  {
    return solve(newLoc,endLocation);
  }

  Location newLoc2(currentLocation.x+1,currentLocation.y-1);
  if(contains(newLoc) && isPath(newLoc))
  {
    return solve(newLoc,endLocation);
  }

  Location newLoc3(currentLocation.x-1,currentLocation.y+1);
  if(contains(newLoc) && isPath(newLoc))
  {
    return solve(newLoc,endLocation);
  }

  Location newLoc4(currentLocation.x+1,currentLocation.y+1);
  if(contains(newLoc) && isPath(newLoc))
  {
    return solve(newLoc,endLocation);
  }

  return false;
}

You may need to add additional logic. 您可能需要添加其他逻辑。 And obviously you will go to infinite loop, since I did not checked if any cell was visited earlier before entering it. 显然,您将进入无限循环,因为在进入单元格之前我没有检查过是否曾访问过任何单元格。 Somehow you have to remember it (may be a visited list). 您必须以某种方式记住它(可能是访问列表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM