首页 文章

Node迷宫导航问题c

提问于
浏览
0

我创建了一个节点的“迷宫”,其节点定义为:

class Node
{
   public:
      Node(string newName);
      Node();
      void setNodeName(string newName);
      string getNodeName();
      void attachNewNode(Node *newNode, int direction);
      Node *getAttachedNode(int direction);
   private:
      string name;
      Node *attachedNodes[4];
};

Node::Node(string newName)
{
   name = newName;
}

Node::Node()
{};

void Node::setNodeName(string newName)
{
   name = newName;
}

string Node::getNodeName()
{
   return name;
}

void Node::attachNewNode(Node *newNode, int direction)
{
   attachedNodes[direction] = newNode;
}

Node* Node::getAttachedNode(int direction)
{
   return attachedNodes[direction];
}

我在格式中读取了一个文件:

9
A1
C3
A1 A2 B1 * *
A2 * B2 A1 *
A3 * B3 * *
B1 * * * A1
B2 B3 C2 * A2
B3 * * B2 A3
C1 C2 * * *
C2 C3 * C1 B2
C3 * * C2 *

其中9是要创建的节点数,A1是我们将开始导航的节点,C3是我们将尝试查找路径的节点,以下行表示节点本身以及它们与之关联的指针 . 例如:

A1 A2 B1 * *

表示节点A1的指针指向北方的节点A2,东方的B1,南方的空白,西方的空白 .

A2 * B2 A1 *

表示节点A2的指针指向北方的节点null,东方的B2,南方的A1,以及西方的null .

我有一个函数来构建存储在 Map 中的节点的“迷宫”,其中字符串键是节点本身的名称 . 我在 Map 上运行深度优先搜索,试图找到从起始节点(在文件中指定)到结束节点(也在文件中指定)的路径 .

我遇到了如何访问 Map 片段的麻烦 . 具体来说,当我尝试DFS代码时:

string getPath()
{
   string path;
   vector<string> attachedNodes;
   stack<string> pathNodes;
   pathNodes.push(startNode.getNodeName());

   while(!pathNodes.empty())
   {
      string temp = pathNodes.top();
      path += temp + " ";

      cout << "Printing Temp." << endl;
      cout << "Temp is: " << temp << " in this case." << endl;

      if(temp == endNode.getNodeName())
         return path;

      for(map<string,Node>::iterator it = rooms.begin(); it != rooms.end(); it++)
      {
         if(it->second.getNodeName() == temp)
         {
            attachedNodes.push_back(rooms[temp].getAttachedNode(1)->getNodeName());
            attachedNodes.push_back(rooms[temp].getAttachedNode(2)->getNodeName());
            attachedNodes.push_back(rooms[temp].getAttachedNode(3)->getNodeName());
            attachedNodes.push_back(rooms[temp].getAttachedNode(4)->getNodeName());
         }
      }

      pathNodes.pop();

      for(int i = 0; i < attachedNodes.size(); i++)
         pathNodes.push(attachedNodes[i]);
   }

   return path;
}

但是,我遇到了各种各样的问题 . 如果我编译并运行上面的代码,我会看到以下输出:

"Printing Temp."
" in this case."

注意“在这种情况下”前面的空白区域和缺少temp变量 . 如果我将.substr(0,2)添加到getPath()中的临时赋值中,我会看到无限循环!我如何访问字符串堆栈的top()一定有问题,对吧?救命!

编辑:主要方法的代码包括:

main()
{
    string file = getFileName(); //reads the file name from user input
    buildGraph(file); //builds the map

    cout << endl << "The nodes in the current map are:" << endl;
    for(map<string,Node>::iterator it = rooms.begin(); it != rooms.end(); it++)
    {
       cout << "current node: " << it->second.getNodeName() << endl;
       if(it->second.getAttachedNode(1)->getNodeName().length() < 3)
          cout << it->second.getAttachedNode(1)->getNodeName() << endl;
       if(it->second.getAttachedNode(2)->getNodeName().length() < 3)
          cout << it->second.getAttachedNode(2)->getNodeName() << endl;
       if(it->second.getAttachedNode(3)->getNodeName().length() < 3)
          cout << it->second.getAttachedNode(3)->getNodeName() << endl;
       if(it->second.getAttachedNode(4)->getNodeName().length() <  3)
          cout << it->second.getAttachedNode(4)->getNodeName() << endl;
    }
    cout << endl;

    cout << "Exited for in main." << endl << endl;

    string path = getPath();
}

1 回答

  • 0

    在这一行:

    attachedNodes.push_back(rooms[temp].getAttachedNode(1)->getNodeName());
    

    如果 getAttachedNode(1) 返回NULL,则将尝试在NULL对象上调用 getNodeName() . 这是不好的 .

    你需要首先检查NULL,这样做会有

    if (rooms[temp].getAttachedNode(1) != NULL)
        attachedNodes.push_back(rooms[temp].getAttachedNode(1)->getNodeName());
    

    但我甚至不确定你为什么要这样做 . 您正在循环 room 试图找到当前房间,然后当您找到它时,您仍然使用 Map 直接找到它 .

    你也永远不会清除你当地的 attachedNodes 变量,所以它会越来越大 . 您不需要 attachedNodes 局部变量,因为您可以使用房间的attachNodes列表 .

    假设attachNodes始终指向有效房间(或NULL),您可以将以下两个循环替换为:

    pathNodes.pop();
    
      for(int i = 0; i < 4; i++) {
         if (rooms[temp].getAttachedNode(i) != NULL) 
            pathNodes.push(rooms[temp].getAttachedNode(i));
      }
    

    这应该会让你更进一步,但你需要查看你的算法,你目前正在做的是将所有可用的附加节点推送到路径上 .

相关问题