]> ruin.nu Git - icfp05.git/commitdiff
implemented shortest path
authorMichael Andreen <harv@ruin.nu>
Fri, 24 Jun 2005 22:41:40 +0000 (22:41 +0000)
committerMichael Andreen <harv@ruin.nu>
Fri, 24 Jun 2005 22:41:40 +0000 (22:41 +0000)
bot/bot.cpp
bot/bot.h

index 8946d03f6feba2764407b3f0dedd7174a6c0acc6..2829030b3dfe4b26cc2652ffb667759892d7131e 100644 (file)
@@ -1,6 +1,8 @@
 #include <iostream>
 #include <sstream>
 #include <iterator>
+#include <queue>
+#include <utility>
 #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<std::string> Bot::shortestPath(std::string from, std::string to, std::string type){
+       
+       priority_queue<vector<string>, vector<vector<string> >, greater<vector<string> > > pq;
+
+       vector<string> v;
+       v.push_back(from);
+
+       pq.push(v);
+       hash_map<string,bool> settled;
+
+       while(!pq.empty()){
+               const vector<string>& w = pq.top();
+               if (w.back() == to)
+                       return w;
+               settled[w.back()] = true;
+               Intersection& inter = _intersections[w.back()];
+               for (hash_map<string,StreetType>::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<string>();
+}
index 416fe63a000e9ee68f9f0998a2023266c6048f4c..483cfd5f6418e4850e91312319cd2d034c05e816 100644 (file)
--- 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<std::string> shortestPath(std::string from, std::string to, std::string type);
 
        __gnu_cxx::hash_map<std::string, Intersection> _intersections;
        __gnu_cxx::hash_map<std::string, Player> _players;