9 using namespace __gnu_cxx;
12 //cerr << "New turn" << endl;
16 //cerr << "Getting information " << endl;
25 if (input != "nowinner:"){
26 string winner = value<string>(input);
28 ++_winningPlans[winner];
34 void Cop::preGamePreparations(){
35 //cerr << "Preparing.." << endl;
36 _copTargets[_name].first = _robberLocation;
37 _copTargets[_name].second = cop_foot;
39 priority_queue<pair<int,string> > banks;
40 //cerr << "Number of banks: " << endl;
41 for(hash_map<string,int>::const_iterator bank = _banks.begin();
42 bank != _banks.end(); ++bank){
43 //cerr << "Finding bank: " << bank->first << endl;
44 list<string> bankRoute = shortestPath(_robberLocation, robber, SimpleSPGoal(bank->first));
45 //cerr << "Pushing bank" << endl;
46 banks.push(pair<int,string>(-bankRoute.size(), bank->first));
48 //cerr << "Done finding banks.." << endl;
49 priority_queue<pair<int,string> > banks2;
50 for (int i = 0; i < 4 && banks.size() > 0;++i){
51 const pair<int,string>& bank = banks.top();
52 //cerr << "Going to bank: " << bank.second << ", distance" << -bank.first << endl;
53 list<string> bankRouteFoot = shortestPath(_copHq, cop_foot, SimpleSPGoal(bank.second));
54 list<string> bankRouteCar = shortestPath(_copHq, cop_car, SimpleSPGoal(bank.second));
55 banks2.push(pair<int,string>(bankRouteCar.size() - bankRouteFoot.size(), bank.second));
58 for (hash_map<string,Player>::const_iterator player = _players.begin();
59 player != _players.end(); ++player){
60 cerr << "Inital plan for player: " << player->first << endl;
61 if (player->second.type == robber){
62 _robber = player->first;
64 }else if (banks2.size() <= 0 || player->first == _name)
67 const pair<int,string>& bank = banks2.top();
68 _copTargets[player->first].first = bank.second;
69 //cerr << "Sending: " << player->first << " to: " << bank.second;
70 if (banks2.size() <= 2)
71 _copTargets[player->first].second = cop_car;
73 _copTargets[player->first].second = cop_foot;
74 //cerr << " on: " << _copTargets[player->first].second << endl;
75 _winningPlans[player->first] = 0;
78 _maybeRobber.push_back(hash_map<string,int>());
81 void Cop::sendInformation(){
82 _maybeRobber.push_back(hash_map<string,int>());
83 hash_map<string,int>& maybeRobber = _maybeRobber.back();
84 int possibilities = 0;
85 if (_robberLocation.empty()){
87 hash_map<string, Intersection>::const_iterator intersection = _intersections.find(_location);
89 for (hash_map<string,StreetType>::const_iterator adj = intersection->second.connections.begin();
90 adj != intersection->second.connections.end(); ++adj){
93 hash_map<string,int>::iterator location = maybeRobber.find(adj->first);
94 if (location == maybeRobber.end()){
95 hash_map<string, Intersection>::const_iterator intersection = _intersections.find(adj->first);
96 int neigh = maybeAtNeighbor(intersection->second);
98 maybeRobber[adj->first] = 100 + neigh;
101 maybeRobber[adj->first] = -100;
105 maybeRobber[adj->first] = -100;
106 hash_map<string, Intersection>::const_iterator intersection = _intersections.find(adj->first);
107 for (hash_map<string,StreetType>::const_iterator adj = intersection->second.connections.begin();
108 adj != intersection->second.connections.end(); ++adj){
109 hash_map<string,int>::iterator location = maybeRobber.find(adj->first);
110 if (location == maybeRobber.end()){
112 maybeRobber[adj->first] = -100;
114 hash_map<string, Intersection>::const_iterator intersection = _intersections.find(adj->first);
115 int neigh = maybeAtNeighbor(intersection->second);
117 maybeRobber[adj->first] = 100 + neigh;
121 maybeRobber[adj->first] = -100;
128 maybeRobber[_robberLocation] = 100;
130 hash_map<string, Intersection>::const_iterator intersection = _intersections.find(_location);
132 for (hash_map<string,StreetType>::const_iterator adj = intersection->second.connections.begin();
133 adj != intersection->second.connections.end(); ++adj){
134 maybeRobber[adj->first] = -100;
135 for (hash_map<string,StreetType>::const_iterator adj = intersection->second.connections.begin();
136 adj != intersection->second.connections.end(); ++adj){
137 hash_map<string,int>::iterator location = maybeRobber.find(adj->first);
138 if (location != maybeRobber.end())
139 maybeRobber[adj->first] = -100;
146 cout << "inf\\" << endl;
147 for (hash_map<string,int>::iterator location = maybeRobber.begin();
148 location != maybeRobber.end(); ++location){
149 if (location->second > 0)
150 location->second /= possibilities;
151 cout << "inf: " << _robber << " " << location->first << " " << _playerTypeNames[robber] << " "
152 << _world << " " << location->second << endl;
154 cout << "inf/" << endl;
157 void Cop::getInformation(){
159 if (_robberLocation.empty()){
160 hash_map<string,int>& maybeRobber = _maybeRobber.back();
163 if (input == "from/")
173 istringstream inf(input);
181 if (type != _playerTypeNames[robber])
190 hash_map<string,int>::iterator location = maybeRobber.find(loc);
191 if (location == maybeRobber.end()){
192 hash_map<string, Intersection>::const_iterator intersection = _intersections.find(loc);
193 if (intersection != _intersections.end())
194 continue; //The intersection doesn't exist, so ignore it.
195 int neigh = maybeAtNeighbor(intersection->second);
197 maybeRobber[loc] = certainty + neigh;
199 maybeRobber[loc] = -200;
200 }else if (location->second > 0)
201 location->second += certainty;
207 }while(input != "from/");
209 _maybeRobber.pop_front();
213 int Cop::maybeAtNeighbor(const Intersection& intersection){
214 int possibility = -1;
215 for (hash_map<string,StreetType>::const_iterator adj = intersection.connections.begin();
216 adj != intersection.connections.end(); ++adj){
217 hash_map<string,int>::iterator oldLoc = _maybeRobber.front().find(adj->first);
218 if (oldLoc == _maybeRobber.front().end() || oldLoc->second > 0){
219 if (possibility < 0){
222 if (oldLoc != _maybeRobber.front().end())
223 possibility += oldLoc->second;
229 void Cop::sendPlan(){
230 //cerr << "Planning" << endl;
231 string secondLocation = _robberLocation;
232 if (_robberLocation.empty()){
233 priority_queue<pair<int,string> > positions;
234 for (hash_map<string,int>::iterator location = _maybeRobber.front().begin();
235 location != _maybeRobber.front().end(); ++location){
236 if (location->second > 0)
237 positions.push(pair<int,string>(location->second,location->first));
240 if (positions.size() > 0){
241 _robberLocation = positions.top().second;
242 cerr << "Sending myself to: " << _robberLocation << ", probability of robber: " << positions.top().first << endl;
244 if (positions.size() > 1){
246 secondLocation = positions.top().second;
247 cerr << "Sending another cop to: " << secondLocation << ", probability of robber: " << positions.top().first << endl;
250 if (!_robberLocation.empty()){
251 //cerr << "Robber found at " << _robberLocation << endl;
252 _copTargets[_name].first = _robberLocation;
254 if (!secondLocation.empty()){
255 list<string> closestFootCop = shortestPath(_robberLocation, cop_foot, FindPlayer(_players, cop_foot), true);
256 list<string> closestCarCop = shortestPath(_robberLocation, cop_car, FindPlayer(_players, cop_car, closestFootCop.size() - 1), true);
257 PlayerType copType = cop_foot;
259 if (closestCarCop.size() > 0){
261 copLocation = closestCarCop.back();
263 copLocation = closestFootCop.back();
266 for (hash_map<string,Player>::const_iterator player = _players.begin();
267 player != _players.end(); ++player){
268 if (player->first == _name || player->second.type == robber)
270 if (player->second.type == copType && player->second.location == copLocation)
271 _copTargets[player->first].first = _robberLocation;
275 //cerr << "Sending plan." << endl;
276 cout << "plan\\" << endl;
277 for(hash_map<string, pair<string, PlayerType> >::iterator player = _copTargets.begin();
278 player != _copTargets.end(); ++player){
279 Player& pl = _players[player->first];
280 if (pl.type != player->second.second
281 && pl.location != _copHq)
282 player->second.second = pl.type;
283 list<string> playerRoute = shortestPath(pl.location, player->second.second, SimpleSPGoal(player->second.first));
284 if (playerRoute.size() > 1){
285 list<string>::iterator i = playerRoute.begin();
287 if (*i == _robberLocation || !(playerRoute.size() == 2 && _intersections[*i].type == bank)){
288 if (player->first == _name)
290 //cerr << "Sending " << player->first << " to " << *i << " by " << player->second.second << endl;
291 cout << "plan: " << player->first << " " << *i << " " << _playerTypeNames[player->second.second] << " " << _world+1 << endl;
296 cout << "plan/" << endl;
299 void Cop::getPlans(){
306 }while(input != "from/");
312 cout << "vote\\" << endl;
313 cout << "vote: " << _name << endl;
314 priority_queue<pair<int,string> > players;
315 for (hash_map<string,int>::const_iterator player = _winningPlans.begin();
316 player != _winningPlans.end(); ++player){
317 players.push(pair<int,string>(-player->second, player->first));
319 while (players.size() > 0){
320 const pair<int,string>& player = players.top();
321 cout << "vote: " << player.second << endl;
322 //cerr << "voted for " << player.second << " with " << player.first << " previously won plans" << endl;
325 cout << "vote/" << endl;