]> ruin.nu Git - icfp05.git/blob - robber/robber.cpp
tuned robber a bit
[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 FindCop : SPGoal{
10         int _limit;
11         const hash_map<string, Player>& _players;
12         FindCop(const hash_map<string, Player>& players, int limit = 0) : _players(players), _limit(limit){}
13         int operator()(const SPInfo* node) const{
14                 if (_limit > 0 && node->cost >= _limit)
15                         return -1;
16                 for(hash_map<string, Player>::const_iterator player = _players.begin();
17                                 player != _players.end(); ++player){
18                         if (player->second.type != robber && player->second.location == node->name){
19                                 return 1;
20                         }
21                 }
22                 return 0;
23         }
24 };
25
26 string Robber::turn(){
27         hash_map<string,double> streets;
28         Intersection& inter = _intersections[_location];
29         for (hash_map<string,StreetType>::const_iterator street = inter.connections.begin();
30                street != inter.connections.end(); ++street){
31                 if (street->second == car){
32                         cerr << "Discarding: " << street->first << " since car is needed" << endl;
33                         continue;
34                 }
35                 double goodness = 10;
36                 Intersection& conInter = _intersections[street->first];
37
38                 list<string> l = shortestPath(street->first, _type, FindCop(_players, 5));
39                 if (l.size() > 0){
40                         cerr << "Cop " << l.size() << " intersections away." << endl;
41                         goodness *= 1 - 1/(6 - l.size());
42                 }
43
44                 if (conInter.type == bank){
45                         cerr << "FOUND A BANK" << endl;
46                         if (l.size() > 0 && l.size() < 4)
47                                 goodness = 0;
48                         else if (_banks[street->first] != 0){
49                                 cerr << "No cop close to bank" << endl;
50                                 return street->first;
51                         }
52                 }
53                 int curx = inter.x;
54                 int cury = inter.y;
55
56                 int newx = conInter.x;
57                 int newy = conInter.y;
58                 for (hash_map<string, Player>::const_iterator player = _players.begin();
59                                 player != _players.end(); ++player){
60                         if (player->first == _name)
61                                 continue;
62
63                         int px = _intersections[player->second.location].x;
64                         int py = _intersections[player->second.location].y;
65
66                         double dist1 = sqrt(pow((curx-px),2.0)+pow((cury-py),2.0));
67                         double dist2 = sqrt(pow((newx-px),2.0)+pow((newy-py),2.0));
68                         /*cerr << "Original distance: " << dist1 << endl;
69                         cerr << "New distance: " << dist2 << endl;
70                         */
71
72
73                         if (dist2 < 50 && dist2 < dist1)
74                                 goodness *= 0.95;
75
76                         if (player->second.type == cop_foot && dist2 <= 3)
77                                 goodness /= 100;
78                         else if (player->second.type == cop_car && dist2 <= 2)
79                                 goodness /= 100;
80
81
82                 }
83                 
84                 cerr << "Street: " << street->first << " goodness: " << goodness << endl;
85                 streets[street->first] = goodness;
86         }
87         streets[_oldLocation] /= 10;
88
89         for(hash_map<string,int>::const_iterator bank = _banks.begin();
90                         bank != _banks.end(); ++bank){
91                 //cerr << "Handling bank at: " << bank->first << endl;
92                 if (bank->second > 0){
93                         list<string> l = shortestPath(_location, _type, SimpleSPGoal(bank->first));
94                         if (l.size() < 2)
95                                 continue;
96                         list<string>::iterator i = l.begin();
97                         if (*++i == "53-and-kimbark")
98                                 cerr << "We should NOT find 53-and-kimbark here" << endl;
99                         streets[*i] += bank->second/(pow(5.0,double(l.size())));
100
101                 }
102         }
103
104         /*cerr << "Using route: ";
105         copy(v.begin(), v.end(), ostream_iterator<string>(cerr, " : "));
106         cerr << endl;
107         */
108
109         string destination;
110         double goodness = 0;
111         for (hash_map<string,double>::const_iterator dest = streets.begin();
112                         dest != streets.end(); ++dest){
113                 cerr << "Goodness: " << dest->second << endl;
114                 if (dest->second > goodness){
115                 cerr << "New Goodness: " << dest->second << endl;
116                         goodness = dest->second;
117                         destination = dest->first;
118                 }
119         }
120         _oldLocation = _location;
121                 
122         return destination;
123         
124 }
125
126 int main(){
127         Robber robber("robber");
128         robber.play();
129
130         return 0;
131 }