]> ruin.nu Git - icfp05.git/blobdiff - botsrc/bot.cpp
shortest path optimized
[icfp05.git] / botsrc / bot.cpp
index 28e8004a2167997f2b5a8bbe629a486ac03f6711..e9950a6ac3e810f33c90b2bda3606c4668fd5ecd 100644 (file)
@@ -33,14 +33,16 @@ void Bot::play(){
                return;
        getline(cin, input);
        _name = value<string>(input);
-       cerr << "Got name: " << _name << endl;
+       //cerr << "Got name: " << _name << endl;
 
        getPlayers();
 
-       cerr << "Got players, building graph." << endl;
+       //cerr << "Got players, building graph." << endl;
        getline(cin, input);
        buildGraph();
 
+       preGamePreparations();
+
        while (true){
                getline(cin, input);
                if (input == "game-over")
@@ -48,9 +50,9 @@ void Bot::play(){
                updateWorld();
                _type = _players[_name].type;
                _location = _players[_name].location;
-               cerr << "New turn" << endl;
+               //cerr << "New turn" << endl;
                move(turn());
-               cerr << "Done with turn." << endl;
+               //cerr << "Done with turn." << endl;
        }
 }
 
@@ -83,35 +85,42 @@ void Bot::buildGraph(){
        if (input != "nod\\")
                return;
 
-       cerr << "Getting intersections" << endl;
+       //cerr << "Getting intersections" << endl;
        while (true){
                getline(cin, input);
                if (input == "nod/")
                        break;
                istringstream node(input);
                node >> input;
+               string name;
+               node >> name;
+               Intersection& inter = _intersections[name];
                node >> input;
-               Intersection& inter = _intersections[input];
-               node >> input;
-               if (input == "bank")
+               if (input == "bank"){
                        inter.type = bank;
-               else if (input == "hq")
+                       _banks[name] = 0;
+               }
+               else if (input == "hq"){
                        inter.type = hq;
-               else if (input == "robber-start")
+                       _copHq = name;
+               }
+               else if (input == "robber-start"){
                        inter.type = robber_start;
+                       _robberLocation = name;
+               }
                else
                        inter.type = ordinary;
                node >> inter.x;
                node >> inter.y;
        }
 
-       cerr << "Number of intersections: " << _intersections.size() << endl;
+       //cerr << "Number of intersections: " << _intersections.size() << endl;
 
        getline(cin, input);
        if (input != "edg\\")
                return;
 
-       cerr << "Getting streets" << endl;
+       //cerr << "Getting streets" << endl;
        int streets = 0;
        while (true){
                getline(cin, input);
@@ -135,7 +144,7 @@ void Bot::buildGraph(){
                }else
                        _intersections[from].connections[to] = car;
        }
-       cerr << "Number of streets: " << streets << endl;
+       //cerr << "Number of streets: " << streets << endl;
        getline(cin, input);
 }
 
@@ -177,6 +186,7 @@ void Bot::updateWorld(){
        getline(cin,input);
        _smell = value<int>(input);
 
+       _robberLocation = "";
        getline(cin,input);
        while (true){
                getline(cin, input);
@@ -190,13 +200,15 @@ void Bot::updateWorld(){
                player >> pl.location;
                player >> input;
                pl.type = _playerTypes[input];
+               if (pl.type == robber)
+                       _robberLocation = pl.location;
        }
        //cerr << "Number of players: " << _players.size() << endl;
        getline(cin, input);
 }
 
 void Bot::move(std::string location){
-       cerr << "Moving to: " << location << endl;
+       //cerr << "Moving to: " << location << endl;
        cout << "mov: " << location << " " << _playerTypeNames[_type] << endl;
 }
 
@@ -236,12 +248,11 @@ std::list<std::string> Bot::shortestPath(const std::string& from, PlayerType typ
 
        int g = 0;
        bool hascar = type == cop_car;
+       ;
+       SPInfo* n = 0;
        while(!pq.empty()){
                node = pq.top();
                pq.pop();
-               //cerr << "Vector with size: " << w.size() << endl;
-               //cerr << "Looking at: " << node->name << endl;
-               //copy(w.begin(), w.end(), ostream_iterator<string>(cerr, " : "));
 
                g = goal(node);
                if (g < 0)
@@ -257,19 +268,24 @@ std::list<std::string> Bot::shortestPath(const std::string& from, PlayerType typ
                }
                node->settled = true;
 
-               Intersection& inter = _intersections[node->name];
-               for (hash_map<string,StreetType>::const_iterator street = inter.connections.begin();
-                               street != inter.connections.end(); ++street){
+               hash_map<string,Intersection>::const_iterator intersection = _intersections.find(node->name);
+               if (intersection == _intersections.end()){ //Sanity check, should never be true..
+                       cerr << "BUG: Could not find intersection: " << node->name << endl;
+                       continue;
+               }
+               for (hash_map<string,StreetType>::const_iterator street = intersection->second.connections.begin();
+                               street != intersection->second.connections.end(); ++street){
                        hash_map<string,SPInfo>::iterator newNode = nodes.find(street->first);
                        bool travelStreet = false;
                        if (hascar){
                                if (reverse){
                                        if (street->second != foot){
-                                               hash_map<string,StreetType>::const_iterator st = _intersections[street->first].connections.find(node->name);
-                                               if (st != _intersections[street->first].connections.end())
-                                                       travelStreet = st->second != foot;
-                                               else
-                                                       travelStreet = false;
+                                               hash_map<string,Intersection>::const_iterator newInter = _intersections.find(street->first);
+                                               if (newInter != _intersections.end()){
+                                                       hash_map<string,StreetType>::const_iterator st = newInter->second.connections.find(node->name);
+                                                       if (st != newInter->second.connections.end())
+                                                               travelStreet = st->second != foot;
+                                               }
                                        }else
                                                travelStreet = true;
                                }else
@@ -279,7 +295,6 @@ std::list<std::string> Bot::shortestPath(const std::string& from, PlayerType typ
                        }
                        if (travelStreet){
                                //cerr << "Adding street: " << street->first << endl;
-                               SPInfo* n = 0;
                                if (newNode == nodes.end()){
                                        n = &nodes[street->first];
                                        n->name = street->first;