Skip to content

Commit

Permalink
separação
Browse files Browse the repository at this point in the history
  • Loading branch information
srcid committed Oct 8, 2018
1 parent 1453087 commit 19c19d2
Show file tree
Hide file tree
Showing 6 changed files with 412 additions and 353 deletions.
23 changes: 23 additions & 0 deletions action.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef ACTION_H
#define ACTION_H

#include <string>

#include "state.h"

using namespace std;

struct Action {
string act;

Action(State boarding, State unboarding) {
act = "From " + stateToString(boarding) + " to " + stateToString(unboarding);
}

Action(State begin) {
act = "Begin in " + stateToString(begin);
}
};


#endif // ACTION_H
358 changes: 5 additions & 353 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,359 +1,11 @@
#include <bits/stdc++.h>

using namespace std;

enum State { arad = 0, zerind, oradea, sibiu, fagaras, rimnicu, pitesti, craiova, drobeta, mehadia, lugoj,
timisoara, bucharest, giurgiu, urziceni, hirsova, eforie, vaslui, iasi, neamt };

string stateToString(State c) {
switch(c) {
case arad:
return "Arad";
case zerind:
return "Zerind";
case oradea:
return "Oradeia";
case sibiu:
return "Sibiu";
case fagaras:
return "Fagaras";
case rimnicu:
return "Rimnincu Vilcea";
case pitesti:
return "Pitesti";
case craiova:
return "Craiova";
case drobeta:
return "Drobeta";
case mehadia:
return "Mehadia";
case lugoj:
return "Logoj";
case timisoara:
return "Timisoara";
case bucharest:
return "Bucharest";
case giurgiu:
return "Giurgiu";
case urziceni:
return "Urzicent";
case hirsova:
return "Hisova";
case eforie:
return "Eforie";
case vaslui:
return "Vaslui";
case iasi:
return "Iasi";
case neamt:
return "Neamt";
default:
return "no entry";
}
}

State stringToState(string s) {
if(s == "Arad" || s == "arad")
return arad;
if(s == "Zerind" || s == "zerind")
return zerind;
if(s == "Oradea" || s == "oradea")
return oradea;
if(s == "Sibiu" || s == "sibiu")
return sibiu;
if(s == "Fagaras" || s == "fagaras")
return fagaras;
if(s == "Rimnicu Vilcea" || s == "rimnicu")
return rimnicu;
if(s == "Pitesti" || s == "pitesti")
return pitesti;
if(s == "Cravoia" || s == "cravoia")
return craiova;
if(s == "Drobeta" || s == "drobeta")
return drobeta;
if(s == "Mehadia" || s == "mehadia")
return mehadia;
if(s == "Lugoj" || s == "lugoj")
return lugoj;
if(s == "Timisoara" || s == "timisoara")
return timisoara;
if(s == "Bucharest" || s == "bucharest")
return bucharest;
if(s == "Giurgiu" || s == "giurgiu")
return giurgiu;
if(s =="Urziceni" || s == "urziceni")
return urziceni;
if(s == "Hirsova" || s == "hirsova")
return hirsova;
if(s == "Eforie" || s == "eforie")
return eforie;
if(s == "Vaslui" || s == "vaslue")
return vaslui;
if(s == "Iasi" || s == "iasi")
return iasi;
if(s == "Neamt" || s == "neamt")
return neamt;

cout << "entrada inválida." << endl;
exit(0);
}

void printTowns(vector<pair<State, int>> v) {
for(pair<State, int> p : v)
cout << "Cidade: " << stateToString(p.first) << endl << "Distância: " << p.second << endl;
return;
}

void printTown(pair<State, int> p) {
cout << "Cidade: " << stateToString(p.first) << endl << "Distância: " << p.second << endl;
return;
}

struct Action {
string act;

Action(State boarding, State unboarding) {
act = "From " + stateToString(boarding) + " to " + stateToString(unboarding);
}

Action(State begin) {
act = "Begin in " + stateToString(begin);
}
};

struct Node {
Node *dad;
State s;
Action *a;
int costPath;
int hCostPath;

Node(Node *_dad, State _s, Action *_a, int _cost, int _hCostPath) {
dad = _dad;
s = _s;
a = _a;
costPath = _cost;
hCostPath = _hCostPath;
}

~Node() {
delete a;
}

};

struct Compar {
bool operator() (const Node *x, const Node *y) const {
return x->costPath > y->costPath;
}
};

struct HCompar {
bool operator() (const Node *x, const Node *y) const {
return x->hCostPath > y->hCostPath;
}
};

struct Model {
State boarding, unboarding;
map<State, vector<pair<State, int>>> * cities;
unordered_map<State, int> *sld;

Model(State _boarding, State _unboarding, map<State, vector<pair<State, int>>> * _cities, unordered_map<State, int> *_sld) {
boarding = _boarding;
unboarding = _unboarding;
cities = _cities;
sld = _sld;
}

~Model() {
delete cities;
}

bool wasVisited(vector<State> *t, State s) {
for(State elem : (*t)) {
if(elem == s)
return true;
}
return false;
}

Node * dfs() {

stack<Node *> edg;
vector<State> expl;

edg.push(new Node( nullptr,
boarding,
new Action(boarding),
0,
0)); // the heuristic campus isn't used in this search

while(!edg.empty()) {
Node *current = edg.top();
expl.push_back(current->s);
edg.pop();

for(pair<State, int> each : cities->at(current->s)) {
Node *n = new Node(current,
each.first,
new Action(current->s, each.first),
each.second + current->costPath,
0); // the heuristic campus isn't used in this search

if(n->s == unboarding) {
return n;
}
else {
if(!wasVisited(&expl, n->s)) {
edg.push(n);
}
}
}
}

return nullptr;
}

Node * bfs() {

queue<Node *> edg;
vector<State> expl;

edg.push( new Node( nullptr,
boarding,
new Action(boarding),
0,
0)); // the heuristic campus isn't used in this search

while(!edg.empty()) {
Node * current = edg.front();
edg.pop();

expl.push_back(current->s);

if(current->s == unboarding)
return current;

for(pair<State, int> each : cities->at(current->s)) {
if(!wasVisited(&expl, each.first)) {
edg.push( new Node( current,
each.first,
new Action(current->s, each.first),
each.second + current->costPath,
0)); // the heuristic campus isn't used in this search
}

}
}

return nullptr;
}

Node * ucs() {
priority_queue<Node *, vector<Node *>, Compar > edg;
vector<State> expl;

edg.push( new Node( nullptr,
boarding,
new Action(boarding),
0,
0));

while(!edg.empty()) {
Node * current = edg.top();
edg.pop();

expl.push_back(current->s);

if(current->s == unboarding)
return current;

for(pair<State, int> each : cities->at(current->s)) {
if(!wasVisited(&expl, each.first)) {
edg.push( new Node( current,
each.first,
new Action(current->s, each.first),
each.second + current->costPath, 0));
}

}
}

return nullptr;
}

Node * searchGreedy() {

priority_queue< Node *, vector<Node *>, HCompar> edg;
vector<State> expl;

edg.push( new Node( nullptr,
boarding,
new Action(boarding),
0,
sld->at(boarding)));

while(!edg.empty()) {
Node * current = edg.top();
edg.pop();

expl.push_back(current->s);

if(current->s == unboarding)
return current;

for(pair<State, int> each : cities->at(current->s)) {
if(!wasVisited(&expl, each.first)) {
edg.push( new Node( current,
each.first,
new Action(current->s, each.first),
each.second + current->costPath,
sld->at(current->s)));
}

}
}
return nullptr;
}


Node * starSearch() {

priority_queue< Node *, vector<Node *>, HCompar> edg;
vector<State> expl;

edg.push( new Node( nullptr,
boarding,
new Action(boarding),
0,
sld->at(boarding)));

while(!edg.empty()) {
Node * current = edg.top();
edg.pop();

expl.push_back(current->s);

if(current->s == unboarding)
return current;

for(pair<State, int> each : cities->at(current->s)) {
if(!wasVisited(&expl, each.first)) {
edg.push( new Node( current,
each.first,
new Action(current->s, each.first),
each.second + current->costPath,
each.second + sld->at(current->s)));
}

}
}
return nullptr;
}

};
#include "state.h"
#include "action.h"
#include "node.h"
#include "model.h"

using namespace std;

void printPath(Node *n) {

Expand Down
Loading

0 comments on commit 19c19d2

Please sign in to comment.