HW4: formatted
This commit is contained in:
parent
5827c13df2
commit
ecfdcbf96b
1 changed files with 159 additions and 131 deletions
|
@ -1,4 +1,15 @@
|
|||
// vim: set ts=4 sw=4 et tw=80:
|
||||
// vim: set ts=3 sw=3 et tw=80:
|
||||
|
||||
/* Assignment 4 - Chess paths
|
||||
* Claudio Maggioni
|
||||
*
|
||||
* External sources used:
|
||||
* - cppreference.com
|
||||
* - DFS wikipedia implementation (adapted).
|
||||
*
|
||||
* The flag DEBUG, if defined, activates some debug messages printe on stdout.
|
||||
* This is disabled to comply with tests.
|
||||
*/
|
||||
|
||||
#include "roadmap.h"
|
||||
#include <iostream>
|
||||
|
@ -8,7 +19,7 @@
|
|||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define DEBUG 1
|
||||
class ConcreteRoadMap : public AbstractRoadMap {
|
||||
private:
|
||||
map< string, set<string> > adj;
|
||||
|
@ -24,28 +35,44 @@ class ConcreteRoadMap : public AbstractRoadMap {
|
|||
public:
|
||||
ConcreteRoadMap() : adj() {};
|
||||
|
||||
void addRoad(const string& city1, const string& city2) override {
|
||||
void addRoad(const string& city1, const string& city2) override;
|
||||
|
||||
void addRoad(const vector<string>& cities) override;
|
||||
|
||||
void clear() override;
|
||||
|
||||
int neighborCountForCity(const string& city) const override;
|
||||
|
||||
bool hasLoop() const override;
|
||||
|
||||
bool reachable(const string& c1, const string& c2) const override;
|
||||
|
||||
int countIslands() const override;
|
||||
};
|
||||
|
||||
|
||||
void ConcreteRoadMap::addRoad(const string& city1, const string& city2) {
|
||||
addPair(city1, city2);
|
||||
addPair(city2, city1);
|
||||
}
|
||||
}
|
||||
|
||||
void addRoad(const vector<string>& cities) override {
|
||||
void ConcreteRoadMap::addRoad(const vector<string>& cities) {
|
||||
for (size_t i = 0; i < cities.size() - 1; i++) {
|
||||
addRoad(cities[i], cities[i + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void clear() override {
|
||||
this->adj.clear();
|
||||
}
|
||||
void ConcreteRoadMap::clear() {
|
||||
adj.clear();
|
||||
}
|
||||
|
||||
int neighborCountForCity(const string& city) const override {
|
||||
int ConcreteRoadMap::neighborCountForCity(const string& city) const {
|
||||
auto i = this->adj.find(city);
|
||||
if (i == adj.end()) return -1;
|
||||
return i->second.size();
|
||||
}
|
||||
}
|
||||
|
||||
bool hasLoop() const override {
|
||||
bool ConcreteRoadMap::hasLoop() const {
|
||||
stack< std::pair<string, string> > s;
|
||||
set<string> discovered;
|
||||
bool search_comp;
|
||||
|
@ -76,15 +103,17 @@ class ConcreteRoadMap : public AbstractRoadMap {
|
|||
}
|
||||
}
|
||||
} else if (!search_comp) {
|
||||
// cout << v.first << " is discovered\n";
|
||||
#if DEBUG
|
||||
cout << v.first << " is discovered\n";
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool reachable(const string& c1, const string& c2) const override {
|
||||
bool ConcreteRoadMap::reachable(const string& c1, const string& c2) const {
|
||||
if (adj.find(c1) == adj.end() ||
|
||||
adj.find(c2) == adj.end()) {
|
||||
return false;
|
||||
|
@ -108,9 +137,9 @@ class ConcreteRoadMap : public AbstractRoadMap {
|
|||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int countIslands() const override {
|
||||
int ConcreteRoadMap::countIslands() const {
|
||||
int islands = 0;
|
||||
bool first;
|
||||
|
||||
|
@ -145,8 +174,7 @@ class ConcreteRoadMap : public AbstractRoadMap {
|
|||
}
|
||||
|
||||
return islands;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
AbstractRoadMap* createRoadMap() {
|
||||
return new ConcreteRoadMap();
|
||||
|
|
Reference in a new issue