This repository was archived by the owner on Apr 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnakeGraph.cpp
More file actions
78 lines (63 loc) · 2.18 KB
/
Copy pathsnakeGraph.cpp
File metadata and controls
78 lines (63 loc) · 2.18 KB
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
#include <iostream>
#include "snakeGraph.h"
// These make sure rows and columns are valid
#define validRow(r) (r >= 0 && r < height)
#define validCol(c) (c >= 0 && c < width)
#define INVALID_VALUE 1
// Changing this macro will change which cells can start or
// end an edge in the graph
#define canEnter(r,c) (validRow(r) && validCol(c) && \
(grid[(r)*width+c] != INVALID_VALUE))
SnakeGraph::SnakeGraph() { } // for the derived types
// Convert a 2d array (height x width) into a graph
// This constructor assumes the graph is undirected and
// unweighted
SnakeGraph::SnakeGraph(const int *grid, int width, int height)
{
for (int index = 0 ; index < height * width ; index++)
{
int row = index / width;
int col = index % width;
// The canEnter macro decides if a cell can originate and edge
if (!canEnter(row, col)) continue;
// Make sure that valid cells will show up in the vertex list
// These cells may not have any edges incident
if (vertices.find(index) == vertices.end())
vertices[index] = set<int>{};
// The grid represents a graph with possible edges in the four
// cardinal directions {UP, DOWN, RIGHT, LEFT}
if (canEnter(row-1, col)) addEdge(index, index-width);
if (canEnter(row+1, col)) addEdge(index, index+width);
if (canEnter(row, col+1)) addEdge(index, index+1);
if (canEnter(row, col-1)) addEdge(index, index-1);
}
numVertices = vertices.size();
numEdges = 0;
for (int v : Vertices())
numEdges += adj(v).size();
numEdges /= 2;
}
// Return the number of vertices in the graph
int SnakeGraph::V() const { return numVertices ; }
// Return the number of edges in the graph
int SnakeGraph::E() const { return numEdges ; }
// Return the set of vertices in the graph
// Since we are returning a set, the vertices will
// be ordered
set<int> SnakeGraph::Vertices() const
{
set<int> retVal;
for (auto &kv : vertices)
retVal.insert(kv.first);
return retVal;
}
// Return the adjacency set for the vertex v
set<int> SnakeGraph::adj(int v)
{
return vertices[v];
}
void SnakeGraph::addEdge(int v, int w)
{
vertices[v].insert(w);
vertices[w].insert(v);
}