1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>

#define DEBUG false
#define TEST false
#define PRINT true

using namespace std;

// NIEAKTUALNE:
// kup farmera (albo 2 do jeszcze lepszej optymalizacji)
// wyslij go w dol (potem w razie potrzeby w prawo), az nazbiera na N-2 (albo 3 do jeszcze lepszej) dodatkowych farmerow
// przesuwaj go w prawo i dokupuj kolejnych, zeby stworzyc prawie caly wiersz farmerow (bez ludka w pierwszej kolumnie)
// kaz im zbierac z gory na dol
// na koniec jak beda w ostatnim wierszu to w lewo i do gory do bazy, a jak ktos dotrze do bazy to w prawo, zeby nie blokowal innym

enum Strategy {IDLE, RUN_ON_LOOP, GO_HOME};
enum Directions{LEFT, RIGHT, UP, DOWN};

class Unit {
public:
    int row = 0;
    int column = 0;
    bool movedThatTurn = false; // if already moved that turn (is usefull when it blocks another Unit)
    int turnsStand = 0; // how many turns stand in same place (for deadlock prevention?)
    Strategy strategy = IDLE;
    int onLoopNumber;
    int gold = 0;
};

class Farmer : public Unit {
public:
};


class Game {
    int N;
    int **fields;
    Unit ***fieldsOccupancy; // 2d array of pointers
    int base_gold;
    int non_zero_fields;
    int MAX_FARMERS = 40;
    const int GREEDY_TURNS = 50;
    int MAX_STAY_ON_FIELD = 3;
    int DEADLOCKED_STAY = 30;
    int turn = 0;
    int loopsGold[20];

    vector<Farmer*> farmers;

    bool can_buy_farmer() {
        if(base_gold < 100) return false;
        if(fieldsOccupancy[0][0] != NULL || fieldsOccupancy[0][1] != NULL) return false;
        if(farmers.size() >= MAX_FARMERS) return false;
        return true;
    }

    bool is_ended() {
        // any farmer has gold
        for(vector<Farmer*>::iterator it=farmers.begin(); it!= farmers.end(); ++it) {
            if((*it)->gold > 0) return false;
        }
        // any field has gold
        if(non_zero_fields > 0) return false;
        return true;
    }

    void buy_farmer() {
        Farmer *farmer = new Farmer();
        fieldsOccupancy[0][0] = farmer;
        farmers.push_back(farmer);
        base_gold -= 100;
        if(PRINT)
            cout << "R FARMER\n";
    }

    void pickup_gold() {
        //pickup gold or leave in base if it's in 0,0
        for(vector<Farmer*>::iterator it=farmers.begin(); it!= farmers.end(); ++it) {
            if((*it)->row == 0 && (*it)->column == 0) {
                base_gold += (*it)->gold;
                (*it)->gold = 0;
            } else {
                if (fields[(*it)->row][(*it)->column] > 0) {
                    int pickup = min(fields[(*it)->row][(*it)->column], 10);
                    fields[(*it)->row][(*it)->column] -= pickup;
                    if(fields[(*it)->row][(*it)->column] == 0) {
                        --non_zero_fields;
                    }
                    if ((*it)->row > 0 && (*it)->column > 0)
                        loopsGold[max((*it)->row, (*it)->column)] -= pickup;
                    (*it)->gold += pickup;
                }
            }
            // clear has moved flag
            (*it)->movedThatTurn = false;
        }
    }

    void moveUnitDirection(Unit* unit, Directions dir) {
        int new_col = unit -> column;
        if(dir == RIGHT) new_col += 1;
        else if (dir == LEFT) new_col -= 1;
        int new_row = unit -> row;
        if (dir == DOWN) new_row += 1;
        else if (dir == UP) new_row -= 1;
        if(fieldsOccupancy[new_row][new_col] != NULL) {
            if (fieldsOccupancy[new_row][new_col]->movedThatTurn == false)
                moveUnit(fieldsOccupancy[new_row][new_col]);
        }
        if(fieldsOccupancy[new_row][new_col] != NULL) {
            // can't move
            ++(unit -> turnsStand);
            return;
        }
        // clear, move on!
        if(PRINT)
            cout<<"M "<<unit->row<<" "<<unit->column<<" "<<new_row<<" "<<new_col<<endl;
        fieldsOccupancy[new_row][new_col] = unit;
        fieldsOccupancy[unit->row][unit->column] = NULL;
        unit->row = new_row;
        unit->column = new_col;
        unit->turnsStand = 0;
    }

    void moveUnit(Unit* unit) {
        // move unit (possibly cause other units to move if they didn't move and block position)
        if (unit->strategy == IDLE) {
            // TODO: chose better strategy, then move
            unit -> strategy = RUN_ON_LOOP;
            // FIXME temporary random
            if (turn < 30) {
                unit -> onLoopNumber = min(rand() % 3 + 1, N-1);
            }
            else if (non_zero_fields == 0) {
                unit -> onLoopNumber = min(rand() % 3 + (N-3), N-1);
            } else {
                int tryes = 0;
                do {
                    if (tryes > 1000) {
                        unit -> onLoopNumber = min(rand() % 3 + (N-3), N-1);
                        break;
                    }
                    unit -> onLoopNumber = rand() % (N-1) + 1;
                    ++tryes;
                } while (loopsGold[unit -> onLoopNumber] == 0);
            }
        }
        if (unit -> gold >= 100 && turn < GREEDY_TURNS) {
            unit -> strategy = GO_HOME;
        }
        if (non_zero_fields == 0) {
            if (unit->gold > 0)
                unit->strategy = GO_HOME;
            else {
                unit -> strategy = RUN_ON_LOOP;
                unit -> onLoopNumber = N-1;
            }
        }
        if(DEBUG)
            cout<<"Moving farmer with strategy: "<<unit->strategy<<" "<<unit->onLoopNumber<<" from " <<unit->row<<" "<<unit->column<<endl;
        // TODO it's naive a little bit
        unit -> movedThatTurn = true;
        if (unit -> turnsStand >= DEADLOCKED_STAY) {
            // naive deadlock detector
            if (unit -> column < N - 1 && unit -> column == unit -> onLoopNumber && fieldsOccupancy[unit->row][unit->column + 1] == NULL) {
                moveUnitDirection(unit, RIGHT);
            } else if (unit -> row < N - 1) {
                moveUnitDirection(unit, DOWN);
            }
            unit -> onLoopNumber = max(unit -> onLoopNumber, max(unit -> row, unit -> column));
        } else if (unit -> strategy == GO_HOME) {
            if (unit -> row == 0)
                moveUnitDirection(unit, DOWN);
            else if (unit -> column == 0)
                moveUnitDirection(unit, UP);
            else 
                moveUnitDirection(unit, LEFT);
        } else if (unit -> strategy == RUN_ON_LOOP) {
            if (fields[unit -> row][unit -> column] > 0 && unit -> turnsStand < MAX_STAY_ON_FIELD)
                //stay
                ++(unit -> turnsStand);
            else if (unit -> row == 0) {
                if (unit -> column < unit -> onLoopNumber)
                    moveUnitDirection(unit, RIGHT);
                else
                    moveUnitDirection(unit, DOWN);
            } else if (unit -> column == 0) {
                moveUnitDirection(unit, UP);
            } else {
                // unit on loop
                if (unit -> row == unit -> onLoopNumber)
                    moveUnitDirection(unit, LEFT);
                else moveUnitDirection(unit, DOWN);
            }
        }
        if (unit -> row == 0 && unit -> column == 0) {
            unit -> strategy = IDLE;
        }
    }

public:
    Game(int n, int** fields) {
        N = n;
        MAX_FARMERS = 6*N;
        base_gold = 200;
        // read fields and init others
        this->fields = fields;
        fieldsOccupancy = new Unit**[n];
        non_zero_fields = n * n - 1;
        for(int i = 0; i <= N; i++)
            loopsGold[i] = 0;

        for(int i = 0; i < n; ++i) {
            fieldsOccupancy[i] = new Unit*[n];
            for(int j = 0; j < n; j++) {
                if (fields[i][j] > 0) {
                    if (i > 0 && j > 0)
                        loopsGold[max(i, j)] += fields[i][j];
                } 
                fieldsOccupancy[i][j] = NULL;
            }
        }
    }

    int play() {
        for(turn=1; turn < 100000; turn++) {
            if(can_buy_farmer()) {
                buy_farmer();
            }
            MAX_STAY_ON_FIELD = min(15, max(2, (int)((turn+20)/30)));

            for(vector<Farmer*>::iterator it=farmers.begin(); it!= farmers.end(); ++it) {
                if(!(*it)->movedThatTurn) {
                    moveUnit(*it);
                }
            }

            // get gold from farmers and leave gold in base
            pickup_gold();
            // end game or turn
            if(PRINT)
                cout<<"=\n";
            if (DEBUG) {
                cout<<"Board\n";
                for(int i = 0; i < N; ++i){
                    for (int j = 0; j < N; ++j)
                        cout<<fields[i][j]<<" ";
                    cout<<endl;
                }
                cout<<"loops gold: ";
                for(int i =1 ; i <= N; i++) {
                    cout<<loopsGold[i]<<" ";
                }
                cout<<endl;
            }
            if(is_ended()) {
                if(PRINT)
                    cout<<"===\n";
                if (DEBUG) cout<<"turns: "<<turn<<endl;
                return turn;
            }
        }
        return 0;
    }

};


int main() {
    srand(137);
    ios_base::sync_with_stdio(0);
    if(TEST) {
        int T = 100;
        int total_turns = 0;
        int turns = 0;
        int n = 20;
        int pow2[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
        for(int t = 0; t < T; t++) {
            int **fields = new int*[n];
            for(int i = 0; i < n; ++i) {
                fields[i] = new int[n];
                for(int j = 0; j < n; j++) {
                    fields[i][j] = pow2[rand()%10];
                }
            }
            fields[0][0] = 0;
            Game *game = new Game(n, fields);
            turns = game -> play();
            total_turns += turns;
        }
        cout<<"avg turns: "<<turns<<endl;
    } else {
        int T, k;
        cin >> T >> k;
        while (T--) {
            int n;
            cin>>n;
            int **fields = new int*[n];
            for(int i = 0; i < n; ++i) {
                fields[i] = new int[n];
                for(int j = 0; j < n; j++) {
                    cin >> fields[i][j];
                }
            }
            Game *game = new Game(n, fields);
            game -> play();
        }
    }
    return 0;
}