Can someone explain this code to me?

Hey I'm pretty new to programming, and for the semester we have an extra credit group project. It's about codingames's skynet revolution episode 1.

<https://www.codingame.com/ide/puzzle/skynet-revolution-episode-1>

My group got the solution to the problem, but I really want to understand it, so here is the code can someone explain it to me.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;


struct Node // data structure node
{
    int A;
    bool GateWay = false;     // gate way virus is trying to get to
    bool pathvisited = false;      //paths we have visited
    vector<Node*> connections;     // links between the nodes
};

vector<Node> nodes;     // vector for nodes
vector<Node*> getGatewayLinks(Node* node) // node pointer for get gateway
{
    vector<Node*> nodes; // vector for nodes
    for (auto& connection : node->connections) // for-loop using variable connection scope resolution to look for total connections in the node
    {
        if (connection->GateWay)   // if connection scope points to gateway / exit
            nodes.push_back(connection); //add connection to the end of vector nodes
    }

    return nodes;     // return the vector nodes
}

pair<int, int> findRoute(Node* first)
{
     for (auto& node : nodes)  // finds movement of the virus
     node.pathvisited = false;     // return false when virus is not found because it moved away. 
     bool Location = false;   // location of virus
     pair<int, int> PlanB;    // if fails activate planB

    queue<Node*> pathway;     // Queue pathway
    pathway.push(first);      // add to pathway 
    while (!pathway.empty())  // if pathway is not empty 
    {
        Node* node = pathway.front();   // node = last element of the queue
        pathway.pop();   // pop/ remove the last element of the queue
        node->pathvisited = true;  //   

        vector<Node*> gatewayLinks = getGatewayLinks(node);
        if (gatewayLinks.size() > 1)
        {
            return {node->A, gatewayLinks[0]->A};
        }
        else if (gatewayLinks.size() == 1)
        {
            if (!Location)
            {
                Location = true;
                PlanB = {node->A, gatewayLinks[0]->A};
            }

            for (auto& neighbor : node->connections)
            {
                if (!neighbor->pathvisited)
                    pathway.push(neighbor);
            }
        }
        else 
        {
            if (!Location)
            {
                for (auto& neighbor : node->connections)
                {
                    if (!neighbor->pathvisited)
                    pathway.push(neighbor);
                }
            }
        }
    }
    if (Location)
        return PlanB;
}

int main()
{
    int N; // the total number of nodes in the level, including the gateways
    int L; // the number of links
    int E; // the number of exit gateways
    cin >> N >> L >> E; cin.ignore();

    nodes.resize(N);

    for (int i = 0; i < N; ++i)
        nodes[i].A = i;

    for (int i = 0; i < L; i++)
     {
        int N1; // N1 and N2 defines a link between these nodes
        int N2;
        cin >> N1 >> N2; cin.ignore();
        nodes[N1].connections.push_back(&nodes[N2]);
        nodes[N2].connections.push_back(&nodes[N1]);
     }

    for (int i = 0; i < E; i++)
     {
        int EI; // the index of a gateway node
        cin >> EI; cin.ignore();
        nodes[EI].GateWay = true;
    }

    // game loop
    while (1)
     {
        int SI; // The index of the node on which the Skynet agent is positioned this turn
        cin >> SI; cin.ignore();
        pair<int, int> connection = findRoute(&nodes[SI]);
        cout << connection.first << " " << connection.second << endl;
    }
}

Last edited on
I'm not going to go through that line by line. If there are specific lines you don't understand, please post the specific lines you don't understand.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Mainly this part and some lines in the main.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
pair<int, int> findRoute(Node* first)
{
     for (auto& node : nodes)  // finds movement of the virus
     node.pathvisited = false;     // return false when virus is not found because it moved away. 
     bool Location = false;   // location of virus
     pair<int, int> PlanB;    // if fails activate planB

    queue<Node*> pathway;     // Queue pathway
    pathway.push(first);      // add to pathway 
    while (!pathway.empty())  // if pathway is not empty 
    {
        Node* node = pathway.front();   // node = last element of the queue
        pathway.pop();   // pop/ remove the last element of the queue
        node->pathvisited = true;  //   

        vector<Node*> gatewayLinks = getGatewayLinks(node);
        if (gatewayLinks.size() > 1)
        {
            return {node->A, gatewayLinks[0]->A};
        }
        else if (gatewayLinks.size() == 1)
        {
            if (!Location)
            {
                Location = true;
                PlanB = {node->A, gatewayLinks[0]->A};
            }

            for (auto& neighbor : node->connections)
            {
                if (!neighbor->pathvisited)
                    pathway.push(neighbor);
            }
        }
        else 
        {
            if (!Location)
            {
                for (auto& neighbor : node->connections)
                {
                    if (!neighbor->pathvisited)
                    pathway.push(neighbor);
                }
            }
        }
    }
    if (Location)
        return PlanB;
}
when dealing with someone else's uncommented gibberish code you have a few options.

What I usually do is throw it away if it does not work out of the box for the problem I was trying to solve. Almost nothing is so complex that spending time figuring out unclear code is worth the time.

If that is not an option, and you have to figure it out, the next step is to treat it like a debugging problem. Pull it apart, add print statements, fill it with data, run it and see what results it produces, and what intermediate results it produces, etc. Exercise it with all kinds of test cases until you get a feel for what it is doing.

You have a better option not always available: why not ask your friends who wrote it how it works?! What was your contribution to the team, anyway? Surely you had some role in the algorithm development or design?




Last edited on
ask your friends who wrote it how it works

This. Communication within a group is important.
Topic archived. No new replies allowed.