简体   繁体   English

将数组转换为节点

[英]Convert array to nodes

Let me start off with saying that I have very basic knowledge of nodes and graphs. 首先,我要说我对节点和图有非常基本的了解。

My goal is to make a solver for a maze which is stored as an array. 我的目标是为存储为数组的迷宫制作求解器。 I know exactly how to implement the algorithm for solving (I'm actually implementing a couple of them) but what my problem is, is that I am very confused on how to implement the nodes that the solver will use in each empty cell. 我确切地知道如何实现求解算法(实际上我正在实现其中的几个),但是我的问题是,我对如何实现求解器将在每个空单元格中使用的节点感到非常困惑。

Here is an example array: 这是一个示例数组:

char maze[5][9] = 
        "#########",
        "#  #    #",
        "# ## ## #",
        "#     # #",
        "#########"

My solver starts at the top left and the solution (exit) is at the bottom right. 我的求解器从左上方开始,解决方案(退出)在右下方。

I've read up on how nodes work and how graphs are implemented, so here is how I think I need to make this: 我已经阅读了有关节点如何工作以及如何实现图的知识,所以我认为我需要这样做:

  • Starting point will become a node 起点将成为一个节点
  • Each node will have as property the column and the row number 每个节点将具有列和行号作为属性
  • Each node will also have as property the visited state 每个节点还将具有访问状态作为属性
  • Visited state can be visited , visited and leads to dead end , not visited 访问状态可以被visited ,被visited and leads to dead endnot visited
  • Every time a node gets visited, every directly adjacent, empty and not visited cell becomes the visited node's child 每次访问节点时,每个直接相邻的,空白且未访问的单元都将成为访问节点的子节点
  • Every visited node gets put on top of the solutionPath stack (and marked on the map as '*') 每个访问的节点都放在solutionPath堆栈的顶部(并在地图上标记为“ *”)
  • Every node that led to a dead end is removed from the stack (and marked on the map as '~') 每个导致死角的节点都将从堆栈中删除(并在地图上标记为“〜”)

Example of finished maze: 成品迷宫的示例:

"#########",
"#*~#****#",
"#*##*##*#",
"#****~#*#",
"#########"

Basically my question is, am I doing something really stupid here with my way of thinking (since I am really inexperienced with nodes) and if it is could you please explain to me why? 基本上,我的问题是,我在这里用我的思维方式做的事真的很愚蠢(因为我对节点真的没有经验),如果可以,请向我解释原因? Also if possible provide me other websites to check which implement examples of graphs on real world applications so I can get a better grasp of it. 另外,如果可能,请提供其他网站,以检查在实际应用程序中实现了图形示例的网站,以便我可以更好地掌握它。

The answer really depends on what you find most important in the problem. 答案确实取决于您发现问题中最重要的内容。 If you're searching for efficiency and speed - you're adding way too many nodes. 如果您正在寻找效率速度 -您添加的节点太多了。 There's no need for so many. 不需要那么多。

The efficient method 高效的方法

Your solver only needs nodes at the start and end of the path, and at every possible corner on the map. 您的求解器仅需要路径起点和终点以及地图上每个可能角点的节点。 Like this: 像这样:

"#########",
"#oo#o  o#",
"# ## ## #",
"#o  oo#o#",
"#########"

There's no real need to test the other places on the map - you'll either HAVE TO walk thru them, or won't have need to even bother testing. 真正不需要测试地图上的其他地方-您要么必须步行穿过它们,要么甚至不必费心测试。

If it helps you - I got a template digraph class that I designed for simple graph representation. 如果有帮助,那么-我设计了一个模板digraph类,用于简单的图形表示。 It's not very well written, but it's perfect for showing the possible solution. 它写的不是很好,但是非常适合显示可能的解决方案。

#include <set>
#include <map>

template <class _nodeType, class _edgeType>
class digraph
{
public:
    set<_nodeType> _nodes;
    map<pair<unsigned int,unsigned int>,_edgeType> _edges;
};

I use this class to find a path in a tower defence game using the Dijkstra's algorithm. 我使用此类通过Dijkstra算法在塔防游戏中找到路径。 The representation should be sufficient for any other algorithm tho. 该表示对于任何其他算法都应该足够。

Nodes can be of any given type - you'll probably end up using pair<unsigned int, unsigned int> . 节点可以是任何给定的类型-您最终可能会使用pair<unsigned int, unsigned int> The _edges connect two _nodes by their position in the set. _edges连接两个_nodes通过他们的position在集合。

The easy to code method 易于编码的方法

On the other hand - if you're looking for an easy to implement method - you just need to treat every free space in the array as a possible node. 另一方面,如果您正在寻找一种易于实现的方法,则只需将数组中的每个可用空间都视为一个可能的节点。 And if that's what you're looking for - there's no need for designing a graph, because the array represents the problem in a perfect way. 而且,如果这就是您要寻找的-则无需设计图形,因为数组可以完美地表示问题。

You don't need dedicated classes to solve it this way. 您不需要专用的类来解决此问题。

bool myMap[9][5]; //the array containing the map info. 0 = impassable, 1 = passable
vector<pair<int,int>> route; //the way you need to go
pair<int,int> start = pair<int,int>(1,1); //The route starts at (1,1)
pair<int,int> end = pair<int,int>(7,3); //The road ends at (7,3)

route = findWay(myMap,start,end); //Finding the way with the algorithm you code

Where findWay has a prototype of vector<pair<int,int>> findWay(int[][] map, pair<int,int> begin, pair<int,int> end) , and implements the algorithm you desire. findWay具有vector<pair<int,int>> findWay(int[][] map, pair<int,int> begin, pair<int,int> end)的原型,其中findWay vector<pair<int,int>> findWay(int[][] map, pair<int,int> begin, pair<int,int> end)并实现您所需的算法。 Inside the function you'll probably need another two dimensional array of type bool, that indicates which places were tested. 在函数内部,您可能需要另一个二维类型为bool的数组,该数组指示要测试的位置。

When the algorithm finds a route, you usually have to read it in reverse, but I guess it depends on the algorithm. 当算法找到路由时,通常必须反向读取它,但是我猜它取决于算法。

In your particular example, myMap would contain: 在您的特定示例中,myMap将包含:

bool myMap[9][5] = {0,0,0,0,0,0,0,0,0,
                    0,1,1,0,1,1,1,1,0,
                    0,1,0,0,1,0,0,1,0,
                    0,1,1,1,1,1,0,1,0,
                    0,0,0,0,0,0,0,0,0};

And findWay would return a vector containing (1,1),(1,2),(1,3),(2,3),(3,3),(4,3),(4,2),(4,1),(5,1),(6,1),(7,1),(7,2),(7,3) findWay将返回一个vector(1,1),(1,2),(1,3),(2,3),(3,3),(4,3),(4,2),(4,1),(5,1),(6,1),(7,1),(7,2),(7,3)

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

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