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