X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=bot%2Fbot.cpp;fp=bot%2Fbot.cpp;h=8450a6321c0c462f99d4b142be4de931a27d78e5;hb=f42b544842d9b9f21da3898225a70dec23c57a36;hp=1c2b4dccf9adaf079abcdb2038aecf96ebd24020;hpb=c587459e89e9d1123425df5b42a367e7bf439e64;p=icfp05.git diff --git a/bot/bot.cpp b/bot/bot.cpp index 1c2b4dc..8450a63 100644 --- a/bot/bot.cpp +++ b/bot/bot.cpp @@ -2,21 +2,25 @@ #include #include #include +#include #include #include "bot.h" using namespace std; using namespace __gnu_cxx; -struct VectComp{ - bool operator()(const vector& v1, const vector& v2){ - return v1.size() > v2.size(); - } -}; -Bot::Bot(string name, string type){ + +Bot::Bot(const string& name, PlayerType type){ _name = name; _type = type; + _playerTypeNames[robber] = "robber"; + _playerTypeNames[cop_foot] = "cop-foot"; + _playerTypeNames[cop_car] = "cop-car"; + + _playerTypes["robber"] = robber; + _playerTypes["cop-foot"] = cop_foot; + _playerTypes["cop-car"] = cop_car; /* priority_queue, vector >, VectComp> pq; @@ -56,7 +60,7 @@ Bot::Bot(string name, string type){ } void Bot::play(){ - cout << "reg: " << _name << " " << _type << endl; + cout << "reg: " << _name << " " << _playerTypeNames[_type] << endl; string input; getline(cin, input); @@ -68,15 +72,15 @@ void Bot::play(){ //robber and 5 cops getline(cin, input); - _players[value(input)].type = "robber"; + _players[value(input)]; getline(cin, input); - _players[value(input)].type = "cop-foot"; + _players[value(input)]; getline(cin, input); - _players[value(input)].type = "cop-foot"; + _players[value(input)]; getline(cin, input); - _players[value(input)].type = "cop-foot"; + _players[value(input)]; getline(cin, input); - _players[value(input)].type = "cop-foot"; + _players[value(input)]; cerr << "Got players, building graph." << endl; getline(cin, input); @@ -149,7 +153,7 @@ void Bot::buildGraph(){ street >> to; string type; street >> type; - cerr << "Street between: " << from << " and " << to << " of type: " << type << endl; + //cerr << "Street between: " << from << " and " << to << " of type: " << type << endl; if (type == "foot"){ _intersections[from].connections[to] = both; Intersection& inter = _intersections[to]; @@ -205,14 +209,15 @@ void Bot::updateWorld(){ //cerr << "Player: " << input << endl; Player& pl = _players[input]; player >> pl.location; - player >> pl.type; + player >> input; + pl.type = _playerTypes[input]; } //cerr << "Number of players: " << _players.size() << endl; } void Bot::move(std::string location){ cerr << "Moving to: " << location << endl; - cout << "mov: " << location << " " << _type << endl; + cout << "mov: " << location << " " << _playerTypeNames[_type] << endl; } template @@ -229,43 +234,77 @@ std::string Bot::turn(){ return _location; } -std::vector Bot::shortestPath(std::string from, std::string to, std::string type){ - - priority_queue, vector >, VectComp > pq; +struct SPInfo{ + string name; + bool settled; + SPInfo* parent; + int cost; +}; - vector v; - v.push_back(from); +struct SPInfoComp{ + bool operator()(const SPInfo* v1, const SPInfo* v2){ + return v1->cost > v2->cost; + } +}; - pq.push(v); - hash_map settled; +std::list Bot::shortestPath(const std::string& from, const std::string& to, PlayerType type){ + + priority_queue, SPInfoComp > pq; + hash_map nodes; + + SPInfo* node = &nodes[from]; + node->name = from; + node->settled = false; + node->parent = 0; + node->cost = 0; + pq.push(node); while(!pq.empty()){ - const vector w = pq.top(); + node = pq.top(); pq.pop(); //cerr << "Vector with size: " << w.size() << endl; //cerr << "Looking at: " << w.back() << endl; //copy(w.begin(), w.end(), ostream_iterator(cerr, " : ")); - if (w.back() == to) - return w; - settled[w.back()] = true; - Intersection& inter = _intersections[w.back()]; + if (node->name == to){ + list l; + front_insert_iterator > ii(l); + do{ + *ii++ = node->name; + node = node->parent; + }while (node != 0); + return l; + } + node->settled = true; + + Intersection& inter = _intersections[node->name]; for (hash_map::const_iterator street = inter.connections.begin(); street != inter.connections.end(); ++street){ - if (settled.find(street->first) != settled.end()) - continue; - //cerr << "Adding: " << street->first << endl; - bool hascar = type == "cop-car"; + hash_map::iterator newNode = nodes.find(street->first); + bool hascar = type == cop_car; if ( (hascar && (street->second == car || street->second == both)) || (!hascar && (street->second == foot || street->second == both))){ - - v = w; - v.push_back(street->first); - pq.push(v); + SPInfo* n = 0; + if (newNode == nodes.end()){ + n = &nodes[street->first]; + n->name = street->first; + n->settled = false; + n->parent = 0; + n->cost = 10000; + }else if (newNode->second.settled) + continue; + else + n = &newNode->second; + + if (n->cost > node->cost + 1){ + n->cost = node->cost + 1; + n->parent = node; + pq.push(n); + } } } } - return vector(); + return list(); }