]> ruin.nu Git - icfp05.git/blob - robber/robber.cpp
made the shortest path algorithm more modular and tuned the robber more
[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                 if (conInter.type == bank){
39                         cerr << "FOUND A BANK" << endl;
40                         list<string> l = shortestPath(_location, _type, FindCop(_players, 3));
41                         if (l.size() > 0){
42                                 cerr << "Cop " << l.size() << " intersections away." << endl;
43                                 goodness = 0;
44                         }else if (_banks[street->first] > 0){
45                                 cerr << "No cop close to bank" << endl;
46                                 return street->first;
47                         }
48                 }
49                 int curx = inter.x;
50                 int cury = inter.y;
51
52                 int newx = conInter.x;
53                 int newy = conInter.y;
54                 for (hash_map<string, Player>::const_iterator player = _players.begin();
55                                 player != _players.end(); ++player){
56                         if (player->first == _name)
57                                 continue;
58
59                         int px = _intersections[player->second.location].x;
60                         int py = _intersections[player->second.location].y;
61
62                         double dist1 = sqrt(pow((curx-px),2.0)+pow((cury-py),2.0));
63                         double dist2 = sqrt(pow((newx-px),2.0)+pow((newy-py),2.0));
64                         /*cerr << "Original distance: " << dist1 << endl;
65                         cerr << "New distance: " << dist2 << endl;
66                         */
67
68
69                         if (dist2 < 50 && dist2 < dist1)
70                                 goodness *= 0.95;
71
72                         if (player->second.type == cop_foot && dist2 <= 3)
73                                 goodness /= 100;
74                         else if (player->second.type == cop_car && dist2 <= 2)
75                                 goodness /= 100;
76
77
78                 }
79                 
80                 cerr << "Street: " << street->first << " goodness: " << goodness << endl;
81                 streets[street->first] = goodness;
82         }
83         streets[_oldLocation] /= 10;
84
85         for(hash_map<string,int>::const_iterator bank = _banks.begin();
86                         bank != _banks.end(); ++bank){
87                 //cerr << "Handling bank at: " << bank->first << endl;
88                 if (bank->second > 0){
89                         list<string> l = shortestPath(_location, _type, SimpleSPGoal(bank->first));
90                         if (l.size() < 2)
91                                 continue;
92                         list<string>::iterator i = l.begin();
93                         if (*++i == "53-and-kimbark")
94                                 cerr << "We should NOT find 53-and-kimbark here" << endl;
95                         streets[*i] += bank->second/(pow(5.0,double(l.size())));
96
97                 }
98         }
99
100         /*cerr << "Using route: ";
101         copy(v.begin(), v.end(), ostream_iterator<string>(cerr, " : "));
102         cerr << endl;
103         */
104
105         string destination;
106         double goodness = 0;
107         for (hash_map<string,double>::const_iterator dest = streets.begin();
108                         dest != streets.end(); ++dest){
109                 cerr << "Goodness: " << dest->second << endl;
110                 if (dest->second > goodness){
111                 cerr << "New Goodness: " << dest->second << endl;
112                         goodness = dest->second;
113                         destination = dest->first;
114                 }
115         }
116         _oldLocation = _location;
117                 
118         return destination;
119         
120 }
121
122 int main(){
123         Robber robber("robber");
124         robber.play();
125
126         return 0;
127 }