From 9363db6b1b8322c4f424ce44db173f251336066e Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Fri, 24 Jun 2005 22:41:40 +0000 Subject: [PATCH] implemented shortest path --- bot/bot.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ bot/bot.h | 1 + 2 files changed, 43 insertions(+) diff --git a/bot/bot.cpp b/bot/bot.cpp index 8946d03..2829030 100644 --- a/bot/bot.cpp +++ b/bot/bot.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include "bot.h" using namespace std; @@ -47,7 +49,9 @@ void Bot::play(){ _type = _players[_name].type; _location = _players[_name].location; getline(cin, input); + cerr << "New turn" << endl; move(turn()); + cerr << "Done with turn." << endl; } } @@ -98,10 +102,12 @@ void Bot::buildGraph(){ istringstream street(input); string from; street >> from; + street >> from; string to; street >> to; string type; street >> type; + cerr << "Street between: " << from << " and " << to << " of type: " << type << endl; if (type == "foot"){ _intersections[from].connections[to] = both; Intersection& inter = _intersections[to]; @@ -163,6 +169,7 @@ void Bot::updateWorld(){ } void Bot::move(std::string location){ + cerr << "Moving to: " << location << endl; cout << "mov: " << location << " " << _type << endl; } @@ -176,6 +183,41 @@ T value(std::string input){ } std::string Bot::turn(){ + cerr << "Using stupid stand still Bot::turn" << endl; return _location; } +std::vector Bot::shortestPath(std::string from, std::string to, std::string type){ + + priority_queue, vector >, greater > > pq; + + vector v; + v.push_back(from); + + pq.push(v); + hash_map settled; + + while(!pq.empty()){ + const vector& w = pq.top(); + if (w.back() == to) + return w; + settled[w.back()] = true; + Intersection& inter = _intersections[w.back()]; + for (hash_map::const_iterator street = inter.connections.begin(); + street != inter.connections.end(); ++street){ + if (settled.find(street->first) == settled.end()) + continue; + bool car = type == "cop-car"; + if ( (car && (street->second == car || street->second == both)) || + (!car && (street->second == foot || street->second == both))){ + v = w; + v.push_back(street->first); + pq.push(v); + } + } + + pq.pop(); + } + + return vector(); +} diff --git a/bot/bot.h b/bot/bot.h index 416fe63..483cfd5 100644 --- a/bot/bot.h +++ b/bot/bot.h @@ -66,6 +66,7 @@ class Bot { void updateWorld(); virtual std::string turn(); void move(std::string location); + std::vector shortestPath(std::string from, std::string to, std::string type); __gnu_cxx::hash_map _intersections; __gnu_cxx::hash_map _players; -- 2.39.2