]> ruin.nu Git - icfp05.git/blob - robber/robber.cpp
robber didn't know as much as I thought, avoid intersections with cops two intersecti...
[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                         if (l.size() < 3)
42                                 continue;
43                         goodness *= 1 - 1/l.size();
44                 }
45
46                 if (conInter.type == bank){
47                         cerr << "FOUND A BANK" << endl;
48                         if (l.size() > 0 && l.size() < 4)
49                                 continue;
50                         else if (_banks[street->first] > 0){
51                                 cerr << "No cop close to bank" << endl;
52                                 return street->first;
53                         }
54                 }
55                 if (goodness == 0)
56                         continue;
57                 vector<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_back(bank->second/(pow(l.size(),2.0)));
68                         }
69                 }
70                 sort(banks.begin(),banks.end());
71                 for (unsigned int i = 0; i < 2 && i < banks.size();++i)
72                         goodness += banks[i];
73                 cerr << "Street: " << street->first << " goodness: " << goodness << endl;
74                 streets[street->first] = goodness;
75         }
76         streets[_oldLocation] /= 10;
77
78
79
80         /*cerr << "Using route: ";
81         copy(v.begin(), v.end(), ostream_iterator<string>(cerr, " : "));
82         cerr << endl;
83         */
84
85         string destination;
86         double goodness = 0;
87         for (hash_map<string,double>::const_iterator dest = streets.begin();
88                         dest != streets.end(); ++dest){
89                 cerr << "Goodness: " << dest->second << endl;
90                 if (dest->second > goodness){
91                 cerr << "New Goodness: " << dest->second << endl;
92                         goodness = dest->second;
93                         destination = dest->first;
94                 }
95         }
96         _oldLocation = _location;
97                 
98         return destination;
99         
100 }
101
102 int main(){
103         Robber robber("robber");
104         robber.play();
105
106         return 0;
107 }