]> ruin.nu Git - icfp05.git/blob - robber/robber.cpp
changed the shortest path algorithm to support reverse search
[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 = 0;
37                 Intersection& conInter = _intersections[street->first];
38
39                 list<string> closestFootCop = shortestPath(street->first, cop_foot, FindPlayer(_players, cop_foot, 6), true);
40                 list<string> closestCarCop = shortestPath(street->first, cop_car, FindPlayer(_players, cop_car, 5), true);
41
42                 unsigned int closestCop = 0;
43                 bool copInCar = false;
44
45                 if (closestCarCop.size() < closestFootCop.size() && closestCarCop.size() > 0){
46                         closestCop = closestCarCop.size();
47                         copInCar = true;
48                 }else 
49                         closestCop = closestFootCop.size();
50
51                 if (closestCop > 0 && closestCop < 3){
52                         cerr << "Cop " << closestCop << " intersections away." << endl;
53                         continue;
54                 }
55
56                 vector<double> banks;
57                 for(hash_map<string,int>::const_iterator bank = _banks.begin();
58                                 bank != _banks.end(); ++bank){
59                         //cerr << "Handling bank at: " << bank->first << endl;
60                         if (bank->second > 0){
61                                 list<string> l = shortestPath(street->first, _type, SimpleSPGoal(bank->first));
62                                 if (l.size() < 1)
63                                         continue;
64                                 //list<string>::iterator i = l.begin();
65                                 //++i;
66                                 banks.push_back(bank->second/(pow(l.size(),4.0)));
67                         }
68                 }
69                 sort(banks.begin(),banks.end(),greater<double>());
70
71                 for (unsigned int i = 0; i < 2 && i < banks.size();++i)
72                         goodness += banks[i];
73
74                 cerr << "Goodness before cop: " << goodness << endl;
75                 if (closestCop > 2){
76                         cerr << "Cop " << closestCop << " intersections away." << endl;
77                         goodness *= 1 - 1/(copInCar ? closestCop : closestCop - 1);
78                 }
79                 cerr << "Goodness after cop: " << goodness << endl;
80
81                 if (conInter.type == bank){
82                         cerr << "FOUND A BANK" << endl;
83                         if (closestCop > 0 && closestCop < 4)
84                                 continue;
85                         else if (_banks[street->first] > 0){
86                                 cerr << "No cop close to bank" << endl;
87                                 return street->first;
88                         }
89                 }
90                 
91                 cerr << "Street: " << street->first << " goodness: " << goodness << endl;
92                 streets[street->first] = goodness;
93         }
94         streets[_oldLocation] /= 10;
95
96
97
98         /*cerr << "Using route: ";
99         copy(v.begin(), v.end(), ostream_iterator<string>(cerr, " : "));
100         cerr << endl;
101         */
102
103         string destination = _location;
104         double goodness = 0;
105         for (hash_map<string,double>::const_iterator dest = streets.begin();
106                         dest != streets.end(); ++dest){
107                 cerr << "Goodness: " << dest->second << endl;
108                 if (dest->second > goodness){
109                 cerr << "New Goodness: " << dest->second << endl;
110                         goodness = dest->second;
111                         destination = dest->first;
112                 }
113         }
114         _oldLocation = _location;
115                 
116         return destination;
117         
118 }
119
120 int main(){
121         Robber robber("robber");
122         robber.play();
123
124         return 0;
125 }