HW4: formatted
This commit is contained in:
parent
5827c13df2
commit
ecfdcbf96b
|
@ -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 "roadmap.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
@ -8,7 +19,7 @@
|
||||||
#include <stack>
|
#include <stack>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
#define DEBUG 1
|
||||||
class ConcreteRoadMap : public AbstractRoadMap {
|
class ConcreteRoadMap : public AbstractRoadMap {
|
||||||
private:
|
private:
|
||||||
map< string, set<string> > adj;
|
map< string, set<string> > adj;
|
||||||
|
@ -24,28 +35,44 @@ class ConcreteRoadMap : public AbstractRoadMap {
|
||||||
public:
|
public:
|
||||||
ConcreteRoadMap() : adj() {};
|
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(city1, city2);
|
||||||
addPair(city2, city1);
|
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++) {
|
for (size_t i = 0; i < cities.size() - 1; i++) {
|
||||||
addRoad(cities[i], cities[i + 1]);
|
addRoad(cities[i], cities[i + 1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear() override {
|
void ConcreteRoadMap::clear() {
|
||||||
this->adj.clear();
|
adj.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
int neighborCountForCity(const string& city) const override {
|
int ConcreteRoadMap::neighborCountForCity(const string& city) const {
|
||||||
auto i = this->adj.find(city);
|
auto i = this->adj.find(city);
|
||||||
if (i == adj.end()) return -1;
|
if (i == adj.end()) return -1;
|
||||||
return i->second.size();
|
return i->second.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hasLoop() const override {
|
bool ConcreteRoadMap::hasLoop() const {
|
||||||
stack< std::pair<string, string> > s;
|
stack< std::pair<string, string> > s;
|
||||||
set<string> discovered;
|
set<string> discovered;
|
||||||
bool search_comp;
|
bool search_comp;
|
||||||
|
@ -76,7 +103,9 @@ class ConcreteRoadMap : public AbstractRoadMap {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (!search_comp) {
|
} else if (!search_comp) {
|
||||||
// cout << v.first << " is discovered\n";
|
#if DEBUG
|
||||||
|
cout << v.first << " is discovered\n";
|
||||||
|
#endif
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,7 +113,7 @@ class ConcreteRoadMap : public AbstractRoadMap {
|
||||||
return false;
|
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() ||
|
if (adj.find(c1) == adj.end() ||
|
||||||
adj.find(c2) == adj.end()) {
|
adj.find(c2) == adj.end()) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -110,7 +139,7 @@ class ConcreteRoadMap : public AbstractRoadMap {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int countIslands() const override {
|
int ConcreteRoadMap::countIslands() const {
|
||||||
int islands = 0;
|
int islands = 0;
|
||||||
bool first;
|
bool first;
|
||||||
|
|
||||||
|
@ -146,7 +175,6 @@ class ConcreteRoadMap : public AbstractRoadMap {
|
||||||
|
|
||||||
return islands;
|
return islands;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
AbstractRoadMap* createRoadMap() {
|
AbstractRoadMap* createRoadMap() {
|
||||||
return new ConcreteRoadMap();
|
return new ConcreteRoadMap();
|
||||||
|
|
Reference in New Issue
Block a user