]> ruin.nu Git - icfp05.git/blob - copsrc/cop.cpp
aed3aeb2b963d9aa6120370dcaa7ee84f56ed34e
[icfp05.git] / copsrc / cop.cpp
1 #include "cop.h"
2 #include <iostream>
3 #include <sstream>
4 #include <iterator>
5 #include <queue>
6 #include <cmath>
7
8 using namespace std;
9 using namespace __gnu_cxx;
10
11 void Cop::preGamePreparations(){
12         //cerr << "Preparing.." << endl;
13         _copTargets[_name].first = _robberLocation;
14         _copTargets[_name].second = cop_foot;
15
16         priority_queue<pair<int,string> > banks;
17         //cerr << "Number of banks: " << endl;
18         for(hash_map<string,int>::const_iterator bank = _banks.begin();
19                                 bank != _banks.end(); ++bank){
20                 //cerr << "Finding bank: " << bank->first << endl;
21                 list<string> bankRoute  = shortestPath(_robberLocation, robber, SimpleSPGoal(bank->first));
22                 //cerr << "Pushing bank" << endl;
23                 banks.push(pair<int,string>(-bankRoute.size(), bank->first));
24         }
25         //cerr << "Done finding banks.." << endl;
26         priority_queue<pair<int,string> > banks2;
27         for (int i = 0; i < 4 && banks.size() > 0;++i){
28                 const pair<int,string>& bank = banks.top();
29                 //cerr << "Going to bank: " << bank.second << ", distance" << -bank.first << endl;
30                 list<string> bankRouteFoot = shortestPath(_copHq, cop_foot, SimpleSPGoal(bank.second));
31                 list<string> bankRouteCar = shortestPath(_copHq, cop_car, SimpleSPGoal(bank.second));
32                 banks2.push(pair<int,string>(bankRouteCar.size() - bankRouteFoot.size(), bank.second));
33                 banks.pop();
34         }
35         for (hash_map<string,Player>::const_iterator player = _players.begin();
36                         player != _players.end(); ++player){
37                 cerr << "Inital plan for player: " << player->first << endl;
38                 if (player->second.type == robber){
39                         _robber = player->first;
40                         continue;
41                 }else if (banks2.size() <= 0 || player->first == _name)
42                         continue;
43
44                 const pair<int,string>& bank = banks2.top();
45                 _copTargets[player->first].first = bank.second;
46                 //cerr << "Sending: " << player->first << " to: " << bank.second;
47                 if (banks2.size() <= 2)
48                         _copTargets[player->first].second = cop_car;
49                 else
50                         _copTargets[player->first].second = cop_foot;
51                 //cerr << " on: " << _copTargets[player->first].second << endl;
52                 _winningPlans[player->first] = 0;
53                 banks2.pop();
54         }
55         _maybeRobber.push_back(hash_map<string,int>());
56 }
57
58 void Cop::sendInformation(){
59         _maybeRobber.push_back(hash_map<string,int>());
60         hash_map<string,int>& maybeRobber = _maybeRobber.back();
61         int possibilities = 0;
62         if (_robberLocation.empty()){
63
64                 hash_map<string, Intersection>::const_iterator intersection = _intersections.find(_location);
65
66                 for (hash_map<string,StreetType>::const_iterator adj = intersection->second.connections.begin();
67                                 adj != intersection->second.connections.end(); ++adj){
68
69                         if (_smell == 1){
70                                 hash_map<string,int>::iterator location = maybeRobber.find(adj->first);
71                                 if (location == maybeRobber.end()){
72                                         hash_map<string, Intersection>::const_iterator intersection = _intersections.find(adj->first);
73                                         int neigh = maybeAtNeighbor(intersection->second);
74                                         if (neigh > -1){
75                                                 maybeRobber[adj->first] = 100 + neigh;
76                                                 ++possibilities;
77                                         }else
78                                                 maybeRobber[adj->first] = -100;
79                                 }
80                         }else{
81                                 if (_smell == 0)
82                                         maybeRobber[adj->first] = -100;
83                                 hash_map<string, Intersection>::const_iterator intersection = _intersections.find(adj->first);
84                                 for (hash_map<string,StreetType>::const_iterator adj = intersection->second.connections.begin();
85                                                 adj != intersection->second.connections.end(); ++adj){
86                                         hash_map<string,int>::iterator location = maybeRobber.find(adj->first);
87                                         if (location == maybeRobber.end()){
88                                                 if (_smell == 0)
89                                                         maybeRobber[adj->first] = -100;
90                                                 else {
91                                                         hash_map<string, Intersection>::const_iterator intersection = _intersections.find(adj->first);
92                                                         int neigh = maybeAtNeighbor(intersection->second);
93                                                         if (neigh > -1){
94                                                                 maybeRobber[adj->first] = 100 + neigh;
95                                                                 ++possibilities;
96                                                         }
97                                                         else
98                                                                 maybeRobber[adj->first] = -100;
99                                                 }
100                                         }
101                                 }
102                         }
103                 }
104         } else {
105                 maybeRobber[_robberLocation] = 100;
106                 possibilities = 1;
107                 hash_map<string, Intersection>::const_iterator intersection = _intersections.find(_location);
108
109                 for (hash_map<string,StreetType>::const_iterator adj = intersection->second.connections.begin();
110                                 adj != intersection->second.connections.end(); ++adj){
111                         maybeRobber[adj->first] = -100;
112                         for (hash_map<string,StreetType>::const_iterator adj = intersection->second.connections.begin();
113                                         adj != intersection->second.connections.end(); ++adj){
114                                 hash_map<string,int>::iterator location = maybeRobber.find(adj->first);
115                                 if (location != maybeRobber.end())
116                                         maybeRobber[adj->first] = -100;
117                         }
118
119                 }
120
121
122         }
123         cout << "inf\\" << endl;
124         for (hash_map<string,int>::iterator location = maybeRobber.begin();
125                         location != maybeRobber.end(); ++location){
126                 if (location->second > 0)
127                         location->second /= possibilities;
128                 cerr << "World: " << _world << endl;;
129                 cout << "inf: " << _robber << " " << location->first << " " << _playerTypeNames[robber] << " "
130                         << _world << " " << location->second << endl;
131         }
132         cout << "inf/" << endl;
133 }
134
135 void Cop::getInformation(){
136         string input;
137         if (_robberLocation.empty()){
138                 hash_map<string,int>& maybeRobber = _maybeRobber.back();
139                 while (true){
140                         getline(cin, input);
141                         if (input == "from/")
142                                 break;
143
144                         getline(cin, input);
145                         getline(cin, input);
146
147                         while (true){
148                                 getline(cin, input);
149                                 if (input == "inf/")
150                                         break;
151                                 istringstream inf(input);
152                                 inf >> input;
153                                 string bot;
154                                 inf >> bot;
155                                 string loc;
156                                 inf >> loc;
157                                 string type;
158                                 inf >> type;
159                                 if (type != _playerTypeNames[robber])
160                                         continue;
161                                 int world;
162                                 inf >> world;
163                                 if (world != _world)
164                                         continue;
165                                 int certainty;
166                                 inf >> certainty;
167
168                                 hash_map<string,int>::iterator location = maybeRobber.find(loc);
169                                 if (location == maybeRobber.end()){
170                                         hash_map<string, Intersection>::const_iterator intersection = _intersections.find(loc);
171                                         if (intersection != _intersections.end())
172                                                 continue; //The intersection doesn't exist, so ignore it.
173                                         int neigh = maybeAtNeighbor(intersection->second);
174                                         if (neigh >= 0)
175                                                 maybeRobber[loc] = certainty + neigh;
176                                         else
177                                                 maybeRobber[loc] = -200;
178                                 }else if (location->second > 0)
179                                         location->second += certainty;
180                         }
181                 }
182         }else{
183                 do{
184                         getline(cin,input);
185                 }while(input != "from/");
186         }
187         _maybeRobber.pop_front();
188         
189 }
190
191 int Cop::maybeAtNeighbor(const Intersection& intersection){
192         int possibility = -1;
193         for (hash_map<string,StreetType>::const_iterator adj = intersection.connections.begin();
194                         adj != intersection.connections.end(); ++adj){
195                 hash_map<string,int>::iterator oldLoc = _maybeRobber.front().find(adj->first);
196                 if (oldLoc == _maybeRobber.front().end() || oldLoc->second > 0){
197                         if (possibility < 0){
198                                 possibility = 0;
199                         }
200                         if (oldLoc != _maybeRobber.front().end())
201                                 possibility += oldLoc->second;
202                 }
203         }
204         return possibility;
205 }
206
207 void Cop::sendPlan(){
208         //cerr << "Planning" << endl;
209         string secondLocation = _robberLocation;
210         if (_robberLocation.empty()){
211                 priority_queue<pair<int,string> > positions;
212                 for (hash_map<string,int>::iterator location = _maybeRobber.front().begin();
213                                 location != _maybeRobber.front().end(); ++location){
214                         if (location->second > 0)
215                                 positions.push(pair<int,string>(location->second,location->first));
216
217                 }
218                 if (positions.size() > 0){
219                         _robberLocation = positions.top().second;
220                         cerr << "Sending myself to: " << _robberLocation << ", probability of robber: " << positions.top().first << endl;
221                 }
222                 if (positions.size() > 1){
223                         positions.pop();
224                         secondLocation = positions.top().second;
225                         cerr << "Sending another cop to: " << secondLocation << ", probability of robber: " << positions.top().first << endl;
226                 }
227         }
228         if (!_robberLocation.empty()){
229                 //cerr << "Robber found at " << _robberLocation << endl;
230                 _copTargets[_name].first = _robberLocation;
231
232                 if (!secondLocation.empty()){
233                         list<string> closestFootCop = shortestPath(_robberLocation, cop_foot, FindPlayer(_players, cop_foot), true);
234                         list<string> closestCarCop = shortestPath(_robberLocation, cop_car, FindPlayer(_players, cop_car, closestFootCop.size() - 1), true);
235                         PlayerType copType = cop_foot;
236                         string copLocation;
237                         if (closestCarCop.size() > 0){
238                                 copType = cop_car;
239                                 copLocation = closestCarCop.back();
240                         }else
241                                 copLocation = closestFootCop.back();
242
243
244                         for (hash_map<string,Player>::const_iterator player = _players.begin();
245                                         player != _players.end(); ++player){
246                                 if (player->first == _name || player->second.type == robber)
247                                         continue;
248                                 if (player->second.type == copType && player->second.location == copLocation)
249                                         _copTargets[player->first].first = _robberLocation;
250                         }
251                 }
252         }
253         //cerr << "Sending plan." << endl;
254         cout << "plan\\" << endl;
255         for(hash_map<string, pair<string, PlayerType> >::iterator player = _copTargets.begin();
256                         player != _copTargets.end(); ++player){
257                         Player& pl = _players[player->first];
258                 if (pl.type != player->second.second
259                                 && pl.location != _copHq)
260                         player->second.second = pl.type;
261                 list<string> playerRoute = shortestPath(pl.location, player->second.second, SimpleSPGoal(player->second.first));
262                 if (playerRoute.size() > 1){
263                         list<string>::iterator i = playerRoute.begin();
264                         ++i;
265                         if (*i == _robberLocation || !(playerRoute.size() == 2 && _intersections[*i].type == bank)){
266                                 if (player->first == _name)
267                                         _location = *i;
268                                 //cerr << "Sending " << player->first << " to " << *i << " by " << player->second.second << endl;
269                                 cout << "plan: " << player->first << " " << *i << " " << _playerTypeNames[player->second.second] << " " << _world+1 << endl;
270                         }
271                 }
272         }
273
274         cout << "plan/" << endl;
275 }
276
277 void Cop::move(std::string location){
278         cout << "cmov\\" << endl;
279         cout << "straight-arrow:" << endl;
280         cout << "mov\\" << endl;
281         Bot::move(location);
282         cout << "mov/" << endl;
283         cout << "acc\\" << endl;
284         cout << "acc/" << endl;
285         cout << "cmov/" << endl;
286 }
287
288 int main(){
289         Cop cop("harvCop");
290         cop.play();
291
292         return 0;
293 }