HW4: done
This commit is contained in:
parent
b3b4b52882
commit
5827c13df2
4 changed files with 147 additions and 38 deletions
|
@ -217,12 +217,12 @@ void knight_positions(struct search_status* s, struct piece_position* p) {
|
||||||
|
|
||||||
if (row >= 1 && col >= 2) consider(s, col - 2, row - 1);
|
if (row >= 1 && col >= 2) consider(s, col - 2, row - 1);
|
||||||
if (row >= 2 && col >= 1) consider(s, col - 1, row - 2);
|
if (row >= 2 && col >= 1) consider(s, col - 1, row - 2);
|
||||||
if (row >= 2 && col <= cs - 1) consider(s, col + 1, row - 2);
|
if (row >= 2 && col + 1 <= cs) consider(s, col + 1, row - 2);
|
||||||
if (row >= 1 && col <= cs - 2) consider(s, col + 2, row - 1);
|
if (row >= 1 && col + 2 <= cs) consider(s, col + 2, row - 1);
|
||||||
if (row <= rs - 1 && col <= cs - 2) consider(s, col + 2, row + 1);
|
if (row + 1 <= rs && col + 2 <= cs) consider(s, col + 2, row + 1);
|
||||||
if (row <= rs - 2 && col <= cs - 1) consider(s, col + 1, row + 2);
|
if (row + 2 <= rs && col + 1 <= cs) consider(s, col + 1, row + 2);
|
||||||
if (row <= rs - 2 && col >= 1) consider(s, col - 1, row + 2);
|
if (row + 2 <= rs && col >= 1) consider(s, col - 1, row + 2);
|
||||||
if (row <= rs - 1 && col >= 2) consider(s, col - 2, row + 1);
|
if (row + 1 <= rs && col >= 2) consider(s, col - 2, row + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void bishop_positions(struct search_status* s, struct piece_position* p) {
|
void bishop_positions(struct search_status* s, struct piece_position* p) {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
OBJECTS = roadmap.o
|
OBJECTS = roadmap.o
|
||||||
CFLAGS=-Wall -g
|
CFLAGS=-Wall -g
|
||||||
CXXFLAGS=-Wall -g
|
CXXFLAGS=-Wall -g -std=c++11
|
||||||
SHELL=/bin/bash
|
SHELL=/bin/bash
|
||||||
|
|
||||||
TIMEOUT=8
|
TIMEOUT=8
|
||||||
|
|
|
@ -1,44 +1,153 @@
|
||||||
|
// vim: set ts=4 sw=4 et tw=80:
|
||||||
|
|
||||||
#include "roadmap.h"
|
#include "roadmap.h"
|
||||||
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
#include <deque>
|
||||||
|
#include <stack>
|
||||||
|
|
||||||
using std::map;
|
using namespace std;
|
||||||
using std::set;
|
|
||||||
|
|
||||||
class ConcreteRoadMap : public AbstractRoadMap {
|
class ConcreteRoadMap : public AbstractRoadMap {
|
||||||
private:
|
private:
|
||||||
int cities;
|
map< string, set<string> > adj;
|
||||||
|
|
||||||
map< string, set<string> > adj;
|
/*
|
||||||
|
* Adds in the map for key city1 a new set with city2 inside if there is
|
||||||
|
* no prior set, otherwise it appends city2 into the existing set
|
||||||
|
*/
|
||||||
|
void addPair(const string& city1, const string& city2) {
|
||||||
|
this->adj[city1].insert(city2);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
public:
|
||||||
* Adds in the map for key city1 a new set with city2 inside if there is no
|
ConcreteRoadMap() : adj() {};
|
||||||
* prior set, otherwise it appends city2 into the existing set
|
|
||||||
*/
|
|
||||||
void addPair(const string& city1, const string& city2) {
|
|
||||||
this->adj[city1].insert(city2);
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
void addRoad(const string& city1, const string& city2) override {
|
||||||
ConcreteRoadMap() : adj(), cities(0) {};
|
addPair(city1, city2);
|
||||||
|
addPair(city2, city1);
|
||||||
|
}
|
||||||
|
|
||||||
void addRoad(const string& city1, const string& city2) override {
|
void addRoad(const vector<string>& cities) override {
|
||||||
addPair(city1, city2);
|
for (size_t i = 0; i < cities.size() - 1; i++) {
|
||||||
addPair(city2, city1);
|
addRoad(cities[i], cities[i + 1]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void addRoad(const vector<string>& cities) override {
|
void clear() override {
|
||||||
for (size_t i = 0; i < cities.size() - 1; i++) {
|
this->adj.clear();
|
||||||
addRoad(cities[i], cities[i + 1]);
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear() override {
|
int neighborCountForCity(const string& city) const override {
|
||||||
this->adj.clear();
|
auto i = this->adj.find(city);
|
||||||
}
|
if (i == adj.end()) return -1;
|
||||||
|
return i->second.size();
|
||||||
|
}
|
||||||
|
|
||||||
int neighborCountForCity(const string& city) const override {
|
bool hasLoop() const override {
|
||||||
auto i = this->adj.find(city);
|
stack< std::pair<string, string> > s;
|
||||||
return i->second.size();
|
set<string> discovered;
|
||||||
}
|
bool search_comp;
|
||||||
|
|
||||||
|
for (auto itr = adj.begin(); itr != adj.end(); ++itr) {
|
||||||
|
search_comp = true;
|
||||||
|
s.push(std::pair<string, string>(itr->first, itr->first));
|
||||||
|
while (!s.empty()) {
|
||||||
|
const std::pair<string, string> v = s.top();
|
||||||
|
#if DEBUG
|
||||||
|
std::cout << "Front is: " << v.first << " with parent " <<
|
||||||
|
v.second << "\n";
|
||||||
|
#endif
|
||||||
|
s.pop();
|
||||||
|
if (discovered.find(v.first) == discovered.end()) {
|
||||||
|
search_comp = false;
|
||||||
|
discovered.insert(v.first);
|
||||||
|
const auto i = this->adj.find(v.first);
|
||||||
|
const auto set = i->second;
|
||||||
|
for (auto j = set.begin(); j != set.end(); ++j) {
|
||||||
|
if (*j != v.second) {
|
||||||
|
#if DEBUG
|
||||||
|
cout << "Neighbor: " << *j << " parent: " <<
|
||||||
|
v.first << "\n";
|
||||||
|
#endif
|
||||||
|
s.push(std::pair<string, string>(*j,
|
||||||
|
v.first));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (!search_comp) {
|
||||||
|
// cout << v.first << " is discovered\n";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool reachable(const string& c1, const string& c2) const override {
|
||||||
|
if (adj.find(c1) == adj.end() ||
|
||||||
|
adj.find(c2) == adj.end()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
deque<string> s;
|
||||||
|
set<string> discovered;
|
||||||
|
s.push_back(c1);
|
||||||
|
while (!s.empty()) {
|
||||||
|
const string& v = s.front();
|
||||||
|
if (v == c2) return true;
|
||||||
|
s.pop_front();
|
||||||
|
|
||||||
|
discovered.insert(v);
|
||||||
|
const auto i = this->adj.find(v);
|
||||||
|
const auto set = i->second;
|
||||||
|
for (auto j = set.begin(); j != set.end(); ++j) {
|
||||||
|
if (discovered.find(*j) == discovered.end()) {
|
||||||
|
s.push_back(*j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int countIslands() const override {
|
||||||
|
int islands = 0;
|
||||||
|
bool first;
|
||||||
|
|
||||||
|
stack< std::pair<string, string> > s;
|
||||||
|
set<string> discovered;
|
||||||
|
|
||||||
|
for (auto itr = adj.begin(); itr != adj.end(); ++itr) {
|
||||||
|
s.push(std::pair<string, string>(itr->first, itr->first));
|
||||||
|
first = true;
|
||||||
|
while (!s.empty()) {
|
||||||
|
const std::pair<string, string>& v = s.top();
|
||||||
|
s.pop();
|
||||||
|
if (discovered.find(v.first) == discovered.end()) {
|
||||||
|
|
||||||
|
if (first) {
|
||||||
|
islands++;
|
||||||
|
}
|
||||||
|
|
||||||
|
discovered.insert(v.first);
|
||||||
|
const auto i = this->adj.find(v.first);
|
||||||
|
const auto set = i->second;
|
||||||
|
|
||||||
|
for (auto j = set.begin(); j != set.end(); ++j) {
|
||||||
|
if (*j != v.second) {
|
||||||
|
s.push(std::pair<string, string>(*j, v.first));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return islands;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
AbstractRoadMap* createRoadMap() {
|
||||||
|
return new ConcreteRoadMap();
|
||||||
|
}
|
||||||
|
|
Reference in a new issue