]> ruin.nu Git - icfp05.git/blob - robber/robber.cpp
priority queue instead of a vector which is later sorted
[icfp05.git] / robber / robber.cpp
1 #include "robber.h"
2 #include <iostream>
3 #include <iterator>
4 #include <queue>
5 #include <cmath>
6
7 using namespace std;
8 using namespace __gnu_cxx;
9
10 struct FindPlayer : SPGoal{
11         int _limit;
12         const hash_map<string, Player>& _players;
13         PlayerType _type;
14         FindPlayer(const hash_map<string, Player>& players, PlayerType type, int limit = 0) : _players(players), _type(type), _limit(limit){}
15         int operator()(const SPInfo* node) const{
16                 if (_limit > 0 && node->cost >= _limit)
17                         return -1;
18                 for(hash_map<string, Player>::const_iterator player = _players.begin();
19                                 player != _players.end(); ++player){
20                         if (player->second.type == _type && player->second.location == node->name){
21                                 return 1;
22                         }
23                 }
24                 return 0;
25         }
26 };
27
28 string Robber::turn(){
29         hash_map<string,double> streets;
30         Intersection& inter = _intersections[_location];
31         for (hash_map<string,StreetType>::const_iterator street = inter.connections.begin();
32                street != inter.connections.end(); ++street){
33                 if (street->second == car){
34                         cerr << "Discarding: " << street->first << " since car is needed" << endl;
35                         continue;
36                 }
37                 double goodness = 0;
38                 Intersection& conInter = _intersections[street->first];
39
40                 list<string> closestFootCop = shortestPath(street->first, cop_foot, FindPlayer(_players, cop_foot, 6), true);
41                 list<string> closestCarCop = shortestPath(street->first, cop_car, FindPlayer(_players, cop_car, 5), true);
42
43                 unsigned int closestCop = 0;
44                 bool copInCar = false;
45
46                 if (closestCarCop.size() < closestFootCop.size() && closestCarCop.size() > 0){
47                         closestCop = closestCarCop.size();
48                         copInCar = true;
49                 }else 
50                         closestCop = closestFootCop.size();
51
52                 if (closestCop > 0 && closestCop < 3){
53                         cerr << "Cop " << closestCop << " intersections away." << endl;
54                         continue;
55                 }
56
57                 priority_queue<double> banks;
58                 for(hash_map<string,int>::const_iterator bank = _banks.begin();
59                                 bank != _banks.end(); ++bank){
60                         //cerr << "Handling bank at: " << bank->first << endl;
61                         if (bank->second > 0){
62                                 list<string> l = shortestPath(street->first, _type, SimpleSPGoal(bank->first));
63                                 if (l.size() < 1)
64                                         continue;
65                                 //list<string>::iterator i = l.begin();
66                                 //++i;
67                                 banks.push(bank->second/(pow(l.size(),4.0)));
68                         }
69                 }
70                 //sort(banks.begin(),banks.end(),greater<double>());
71
72                 for (unsigned int i = 0; i < 2 && banks.size() > 0;++i){
73                         goodness += banks.top();
74                         banks.pop();
75                 }
76
77                 cerr << "Goodness before cop: " << goodness << endl;
78                 if (closestCop > 2){
79                         cerr << "Cop " << closestCop << " intersections away." << endl;
80                         goodness *= 1 - 1/(copInCar ? closestCop : closestCop - 1);
81                 }
82                 cerr << "Goodness after cop: " << goodness << endl;
83
84                 if (conInter.type == bank){
85                         cerr << "FOUND A BANK" << endl;
86                         if (closestCop > 0 && closestCop < 4)
87                                 continue;
88                         else if (_banks[street->first] > 0){
89                                 cerr << "No cop close to bank" << endl;
90                                 return street->first;
91                         }
92                 }
93                 
94                 cerr << "Street: " << street->first << " goodness: " << goodness << endl;
95                 streets[street->first] = goodness;
96         }
97         streets[_oldLocation] /= 10;
98
99
100
101         /*cerr << "Using route: ";
102         copy(v.begin(), v.end(), ostream_iterator<string>(cerr, " : "));
103         cerr << endl;
104         */
105
106         string destination = _location;
107         double goodness = 0;
108         for (hash_map<string,double>::const_iterator dest = streets.begin();
109                         dest != streets.end(); ++dest){
110                 cerr << "Goodness: " << dest->second << endl;
111                 if (dest->second > goodness){
112                 cerr << "New Goodness: " << dest->second << endl;
113                         goodness = dest->second;
114                         destination = dest->first;
115                 }
116         }
117         _oldLocation = _location;
118                 
119         return destination;
120         
121 }
122
123 int main(){
124         Robber robber("robber");
125         robber.play();
126
127         return 0;
128 }