使用节点导航迷宫

时间:2017-05-09 22:03:21

标签: graph nodes

下图称为图形。圆圈称为节点,线条称为边缘。边连接两个节点。您可以将图表解释为房间和通道的迷宫。节点可以被认为是房间,边缘将一个房间连接到另一个房间。请注意,每个节点在下图中最多有四个边(N,S,E,W)。

使用对Node类实例的引用编写一个实现上面迷宫的程序。图中的每个圆都对应一个Node实例。边缘对应于将一个节点连接到另一个节点的链接,并且可以在Node中表示为引用另一个Node类的实例变量。在节点A中启动用户。用户的目标是在节点L中完成。 enter image description here

这是我的代码

#include <iostream>
#include <string>
using namespace std;

class Node
{
public:
    Node() { north = south = east = west = NULL; visited = false; }
    Node(char newID) { north = south = east = west = NULL; visited = false; ID = newID; }
    char getID() { return ID; }
    void setConnections(Node *n, Node *s, Node *e, Node *w)
    {
        north = n; south = s; west = w; east = e;
    }
    Node* getNorth() { return north; }
    Node* getSouth() { return south; }
    Node* getEast()  { return east; }
    Node* getWest()  { return west; }
    void setVisited()
    {
        visited = true;
    }
    // Only needed for AUTO-SOLVE
    bool getVisited() { return visited; }
    // Only needed for AUTO-SOLVE
private:
    char ID;
    bool visited;
    // Only needed for AUTO-SOLVE
    Node *north, *south, *east, *west;
};

int main()
{
    Node *A = new Node('A');
    Node *B = new Node('B');
    Node *C = new Node('C');
    Node *D = new Node('D');
    Node *E = new Node('E');
    Node *F = new Node('F');
    Node *G = new Node('G');
    Node *H = new Node('H');
    Node *I = new Node('I');
    Node *J = new Node('J');
    Node *K = new Node('K');
    Node *L = new Node('L');

    A->setConnections(NULL, E, B, NULL);
    B->setConnections(NULL, F, NULL, A);
    C->setConnections(NULL, G, D, NULL);
    D->setConnections(NULL, NULL, NULL, C);
    E->setConnections(A, I, NULL, NULL);
    F->setConnections(B, NULL, G, NULL);
    G->setConnections(C, K, H, F);
    H->setConnections(NULL, L, NULL, G);
    I->setConnections(E, NULL, J, NULL);
    J->setConnections(NULL, NULL, NULL, I);
    K->setConnections(G, NULL, NULL, NULL);
    L->setConnections(H, NULL, NULL, NULL);

    Node*current = A;
    cout << "You are in room " << current->getID() << " of a maze of twisty little passages, all like. You can go: " << endl;
    if (current->getNorth())
        cout << "north (n) | ";
    if (current->getSouth())
        cout << "south (s) | ";
    if (current->getEast())
        cout << "east (e) | ";
    if (current->getWest())
        cout << "west (w) | ";

    string go;
    cin >> go;

    if (go == "n")
        current = current->getNorth();
    else if (go == "s")
        current = current->getSouth();
    else if (go == "e")
        current = current->getEast();
    else
        current = current->getWest();
    while (current->getID() != 'L')
    {
        cout << "You are in room " << current->getID() << " of a maze of twisty little passages, all like. You can go: " << endl;
        if (current->getNorth()){
            cout << "north (n) | ";
            current = current->getNorth();
        }
        if (current->getSouth()){
            cout << "south (s) | ";
            current = current->getSouth();
        }
        if (current->getEast()){
            cout << "east (e) | ";
            current = current->getEast();
        }
        if (current->getWest()){
            cout << "west (w) | ";
            current = current->getWest();
        }
        cin >> go;
    }
    if (current->getID() == 'L')
        cout << "Congratulations! You made it.";

    return 0;
}

我只是不知道什么是错的,因为看起来我的功能不起作用。这是输出: enter image description here

谢谢

0 个答案:

没有答案