-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.h
96 lines (79 loc) · 1.48 KB
/
cell.h
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
#pragma once
#include <unordered_set>
#include "distances.h"
class Cell
{
public:
int row;
int col;
Cell* north{nullptr};
Cell* south{nullptr};
Cell* east{nullptr};
Cell* west{nullptr};
//std::unordered_map<Cell*, bool> links;
std::unordered_set<Cell*> links;
Cell(int r, int c): row{r}, col{c} {};
void link(Cell*, bool);
void unlink(Cell*, bool);
std::unordered_set<Cell*> getLinks();
bool isLinked(Cell*);
Distances getDistances();
};
// TODO how to declare class functions?
void Cell::link(Cell* cell, bool bidi=true)
{
links.insert(cell);
if (bidi)
{
cell->links.insert(this);
}
}
void Cell::unlink(Cell* cell, bool bidi=true)
{
links.erase(cell);
if (bidi)
{
cell->links.erase(this);
}
}
std::unordered_set<Cell*> Cell::getLinks()
{
return links;
}
bool Cell::isLinked(Cell* cell)
{
std::unordered_set<Cell*>::iterator it;
it = this->links.find(cell);
if (it == this->links.end())
{
return false;
}
else
{
return true;
}
}
Distances Cell::getDistances()
{
Distances dists(this);
std::unordered_set<Cell*> frontier;
frontier.insert(this);
while (frontier.size() > 0)
{
//std::cout << frontier.size() << std::endl;
std::unordered_set<Cell*> new_frontier;
for (auto const& cell: frontier)
{
for (auto const& linked: cell->getLinks())
{
if (dists.isExplored(linked) == false)
{
dists[linked] = dists[cell] + 1;
new_frontier.insert(linked);
}
}
}
frontier = new_frontier;
}
return dists;
}