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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#include <iostream>
#include <queue>
#include <set>
#include <functional>
#include <algorithm>
#include <sstream>
#include <tuple>
using namespace std;

#define NDEBUG
#include <cassert>

#define MAX_T 10
#define MAX_N 20

struct Unit {
    enum Type { None, Farmer, Tank };

    Type type = None;
    int gold = 0;
    bool can_move = 1;
};

int scores[MAX_T];
string answers[MAX_T];

int n;
int ns[MAX_T];
int map[MAX_N][MAX_N];
int map_orig[MAX_T][MAX_N][MAX_N];
Unit units[MAX_N][MAX_N];
int gold;
int turn;
int number_of_units;
int number_of_tanks;
float stone_prob;
stringstream out;
int try_counter;
bool second_encounter = 0;

int random_start1 = 2347;
int random_start2 = 8046;
int random_value = 3427;
inline float random1() {
    random_start1 = (random_start1 * 1409) % 10007;
    random_start2 = (random_start1 * 1009) % 17327;
    random_value = random_value ^ random_start1 ^ random_start2;
    return float(random_value) / 32768;
}

inline bool in_bound(int i, int j) {
    return 0 <= i && 0 <= j && i < n && j < n;
}

inline void check_square(int i, int j) {
    assert(in_bound(i, j));
    Unit const& unit = units[i][j];
    if (unit.type == Unit::Farmer) {
        assert(map[i][j] >= 0);
    } else {
        assert(unit.gold == 0);
    }
    if (unit.type == Unit::None) {
        assert(unit.can_move);
    }
    if (i == 0 && j == 0) {
        assert(map[i][j] == 0);
    }
}

void move(int i, int j, int di, int dj) {
    assert(-1 <= di && di <= 1 && -1 <= dj && dj <= 1);
    assert(di == 0 || dj == 0);
    check_square(i, j);
    check_square(i + di, j + dj);
    assert(units[i][j].type != Unit::None);
    assert(units[i + di][j + dj].type == Unit::None);
    assert(units[i][j].can_move);
    units[i][j].can_move = 0;
    swap(units[i][j], units[i + di][j + dj]);
    out << "M " << i << " " << j << " " << i + di << " " << j + dj << "\n";
}

template <class T, class S, class C>
void clearpq(priority_queue<T, S, C>& q) {
    struct HackedQueue : private priority_queue<T, S, C> {
        static S& Container(priority_queue<T, S, C>& q) {
            return q.*&HackedQueue::c;
        }
    };
    HackedQueue::Container(q).clear();
}

vector<bool> path_vis(MAX_N * MAX_N);
vector<int> path_to_zero;
priority_queue<tuple<float, pair<int, int>, pair<int, int>>> path_Q;
template <class T> pair<pair<int, int>, pair<int, int>> path(int i, int j, float limit=1e9, bool reverse_path = 0) {
    assert(in_bound(i, j));
    path_to_zero.clear();

    clearpq(path_Q);
    vector<pair<int, int>> Z = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    for (auto z: Z) {
        pair<int, int> pos{i + z.first, j + z.second};
        int posv = pos.first * n + pos.second;
        if (in_bound(pos.first, pos.second) && T::walkable(pos.first, pos.second) && !path_vis[posv]) {
            path_vis[posv] = 1;
            path_to_zero.push_back(posv);
            path_Q.push(make_tuple(-T::dist(i, j, pos.first, pos.second), pos, z));
        }
    }
    int best = 0;
    pair<int, int> best_dir = {0, 0};
    pair<int, int> best_point = {-1, -1};
    while (!path_Q.empty()) {
        auto p = path_Q.top();
        path_Q.pop();

        float d = -get<0>(p);
        if (d > limit) break;

        int a = get<1>(p).first;
        int b = get<1>(p).second;
        pair<int, int> dir = get<2>(p);

        int v = T::score(a, b, d);
        if (v > best) {
            best = v;
            best_dir = dir;
            best_point = {a, b};
        }
        if (T::end(a, b)) break;

        for (auto z: Z) {
            pair<int, int> pos{a + z.first, b + z.second};
            int posv = pos.first * n + pos.second;
            if (in_bound(pos.first, pos.second) && T::walkable(pos.first, pos.second) && !path_vis[posv]) {
                path_vis[posv] = 1;
                path_to_zero.push_back(posv);
                path_Q.push(make_tuple(-d - T::dist(a, b, pos.first, pos.second), pos, reverse_path ? z : dir));
            }
        }
    }

    for (auto& p: path_to_zero) path_vis[p] = 0;

    if (reverse_path) return {best_point, {-best_dir.first, -best_dir.second}};
    return {best_point, best_dir};
}

struct C1 {
    inline static float dist(int, int, int, int) { return 1 + random1() / 100000; }
    inline static int score(int a, int b, float) { return map[a][b] > 0; }
    inline static bool end(int a, int b) { return map[a][b] > 0; }
    inline static bool walkable(int a, int b) { return map[a][b] >= 0 && units[a][b].type == Unit::None; }
};
pair<int, int> dir_to_nearest_gold_pile(int i, int j) {
    return path<C1>(i, j).second;
}

struct C2 {
    inline static float dist(int, int, int a, int b) { return 1 - (map[a][b] > 0) * 0.8 + random1() / 10000; }
    inline static int score(int a, int b, float) { return max(0, map[a][b]); }
    inline static bool end(int a, int b) { return map[a][b] > 0; }
    inline static bool walkable(int a, int b) { return map[a][b] >= 0 && units[a][b].type == Unit::None; }
};
pair<int, int> dir_to_best_gold_pile(int i, int j) {
    return path<C2>(i, j, n / 4).second;
}

struct C3 {
    static int x, y;
    inline static float dist(int, int, int a, int b) { return 1 - (map[a][b] > 0) * 0.1 + random1() / 10000; }
    inline static int score(int a, int b, float) { return a == x && b == y; }
    inline static bool end(int a, int b) { return a == x && b == y; }
    inline static bool walkable(int a, int b) { return map[a][b] >= 0 && units[a][b].type == Unit::None; }
};
int C3::x = -1;
int C3::y = -1;
pair<int, int> dir_to_point(int i, int j, int x, int y) {
    if (units[x][y].type != Unit::None) return  {0, 0};
    if (map[x][y] < 0) return {0, 0};
    C3::x = x;
    C3::y = y;
    return path<C3>(i, j).second;
}

struct C4 {
    static int i, j;
    static constexpr int coeff = 4;
    inline static float dist(int, int, int a, int b) { return 1 - (map[a][b] > 0) * 0.3 + random1() / 10000; }
    inline static bool score(int a, int b, float s) { return (units[a][b].type == Unit::Farmer && units[a][b].can_move && coeff * map[a][b] < map[i][j]); }
    inline static bool end(int a, int b) { return units[a][b].type == Unit::Farmer && units[a][b].can_move && coeff * map[a][b] < map[i][j]; }
    inline static bool walkable(int a, int b) { return map[a][b] >= 0 && (units[a][b].type == Unit::None || (units[a][b].type == Unit::Farmer && units[a][b].can_move && coeff * map[a][b] < map[i][j])); }
};
int C4::i = -1;
int C4::j = -1;
pair<pair<int, int>, pair<int, int>> nearest_free_farmer(int i, int j) {
    C4::i = i;
    C4::j = j;
    return path<C4>(i, j, 1e9, 1);
}

struct C5 {
    inline static float dist(int, int, int a, int b) { return 1 + (map[a][b] < 0) * (-map[a][b]) / 40 - (map[a][b] == 0) * 0.4 + random1() / 10000; }
    inline static int score(int a, int b, float) { return max(0, -map[a][b]); }
    inline static bool end(int a, int b) { return map[a][b] < 0; }
    inline static bool walkable(int a, int b) { return units[a][b].type == Unit::None; }
};
pair<int, int> dir_to_best_stone(int i, int j) {
    return path<C5>(i, j).second;
}

struct C6 {
    inline static float dist(int, int, int a, int b) { return 1 - (map[a][b] < 0) * 0.5 - (map[a][b] == 0) * 0.4 + random1() / 10000; }
    inline static int score(int a, int b, float) { return -map[a][b]+1; }
    inline static bool end(int a, int b) { return map[a][b] <= 0; }
    inline static bool walkable(int a, int b) { return units[a][b].type == Unit::None && (a != 0 || b != 0); }
};
pair<int, int> dir_to_nearest_not_gold(int i, int j) {
    return path<C6>(i, j).second;
}

void recruit(bool farmer) {
    assert(gold >= 100);
    gold -= 100;
    check_square(0, 0);
    assert(units[0][0].type == Unit::None);
    units[0][0] = Unit();
    if (farmer) {
        units[0][0].type = Unit::Farmer;
        out << "R FARMER\n";
    } else {
        units[0][0].type = Unit::Tank;
        out << "R TANK\n";
    }
    ++number_of_units;
    if (!farmer) ++number_of_tanks;
    check_square(0, 0);
}

int MAX_UNITS;

pair<int, int> map_sorted[MAX_N * MAX_N];
bool next_move(bool first) {
    bool end = 1;

    if (first) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                map_sorted[i * n + j] = {i, j};
            }
        }
        sort(map_sorted, map_sorted + n * n, [](pair<int, int> const& a, pair<int, int> const& b) { return map[a.first][a.second] > map[b.first][b.second]; });
    }

    if (gold >= 100 && units[0][0].type == Unit::None && number_of_units < MAX_UNITS) {
        if (stone_prob == 0) {
            recruit(1);
            if (!second_encounter && number_of_units >= 26) MAX_UNITS = number_of_units;
        } else if (number_of_units == 0) {
            if (map[1][0] > 0 || map[0][1] > 0) recruit(1);
            else recruit(0);
        } else if (number_of_units == 1) {
            if (number_of_tanks == 0) recruit(0);
            else recruit(1);
        } else {
            if (stone_prob < 0.65) {
                if (float(number_of_tanks) / number_of_units < stone_prob * stone_prob / 0.7) recruit(0);
                else recruit(1);
                if (number_of_units >= 10 + (try_counter % 3)) MAX_UNITS = number_of_units;
            } else {
                if (float(number_of_tanks) / number_of_units < stone_prob * stone_prob / (0.9 - (try_counter % 10 >= 5) * (10 - try_counter % 10) * 0.07)) recruit(0);
                else recruit(1);
                if (number_of_units >= 10) MAX_UNITS = number_of_units;
            }
        }
        end = 0;
    }

    if (number_of_units >= MAX_UNITS) {
        for (int i = 0; i < n * n; ++i) {
            int a = map_sorted[i].first;
            int b = map_sorted[i].second;
            if (map[a][b] < 64) break;
            if (units[a][b].type == Unit::None) {
                auto point_dir = nearest_free_farmer(a, b);
                auto point = point_dir.first;
                auto dir = point_dir.second;
                if (dir == make_pair(0, 0)) break;
                move(point.first, point.second, dir.first, dir.second);
            } else if (units[a][b].type == Unit::Farmer) {
                units[a][b].can_move = 0;
            }
        }
    }

    int gold_for_base = 0;

    for (int dis = 0; dis < 2 * n; ++dis) {
        for (int i = 0; i < n; ++i) {
            int j = dis - i;
            if (j < 0 || j >= n) continue;
            Unit& unit = units[i][j];
            pair<int, int> dir;
            if (!unit.can_move) continue;
            if (unit.type == Unit::Farmer) {
                if (map[i][j] > 16) continue;
                if (unit.gold < max(50, int(2.3 * (i + j) * number_of_units)) || (number_of_units + (gold_for_base + gold) / 100) >= MAX_UNITS) {
                    if ((number_of_units + (gold_for_base + gold) / 100) < MAX_UNITS) {
                        dir = dir_to_nearest_gold_pile(i, j);
                    } else {
                        dir = dir_to_best_gold_pile(i, j);
                    }
                    if (dir != make_pair(0, 0)) {
                        move(i, j, dir.first, dir.second);
                        end = 0;
                        continue;
                    }
                }

                if (unit.gold > 0) {
                    dir = dir_to_point(i, j, 0, 0);
                    if (dir != make_pair(0, 0)) {
                        move(i, j, dir.first, dir.second);
                        gold_for_base += unit.gold;
                        end = 0;
                        continue;
                    } else {
                        dir = dir_to_nearest_gold_pile(i, j);
                        if (dir != make_pair(0, 0)) {
                            move(i, j, dir.first, dir.second);
                            end = 0;
                            continue;
                        }
                    }
                } else {
                    bool br = 0;
                    for (int a = n - 1; !br && a >= n / 2; a -= 2) {
                        for (int b = n - 1; b >= n / 2; b -= 2) {
                            if (a == i && b == j) {
                                br = 1;
                                break;
                            }
                            dir = dir_to_point(i, j, a, b);
                            if (dir != make_pair(0, 0)) {
                                move(i, j, dir.first, dir.second);
                                end = 0;
                                br = 1;
                                break;
                            }
                        }
                    }
                }
            } else if (unit.type == Unit::Tank) {
                if (map[i][j] < 0) continue;
                dir = dir_to_best_stone(i, j);
                if (dir != make_pair(0, 0)) {
                    move(i, j, dir.first, dir.second);
                    end = 0;
                    continue;
                } else {
                    bool br = 0;
                    for (int a = n - 1; !br && a >= n / 2; a -= 2) {
                        for (int b = n - 1; b >= n / 2; b -= 2) {
                            if (map[a][b] > 0) continue;
                            if (a == i && b == j) {
                                br = 1;
                                break;
                            }
                            dir = dir_to_point(i, j, a, b);
                            if (dir != make_pair(0, 0)) {
                                move(i, j, dir.first, dir.second);
                                end = 0;
                                br = 1;
                                break;
                            }
                        }
                    }
                    if (!br && map[i][j] > 0) {
                        dir = dir_to_nearest_not_gold(i, j);
                        if (dir != make_pair(0, 0)) {
                            move(i, j, dir.first, dir.second);
                            end = 0;
                            continue;
                        }
                    }
                }
            }
        }
    }
    return !end;
}

void end_turn() {
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            Unit& unit = units[i][j];
            if (unit.type == Unit::Farmer) {
                assert(map[i][j] >= 0);
                if (i == 0 && j == 0) {
                    gold += unit.gold;
                    unit.gold = 0;
                } else {
                    int gold_to_take = min(map[i][j], 10);
                    unit.gold += gold_to_take;
                    map[i][j] -= gold_to_take;
                }
            } else if (unit.type == Unit::Tank) {
                if (map[i][j] < 0) {
                    int stones_to_destroy = min(-map[i][j], 10);
                    map[i][j] += stones_to_destroy;
                }
            }

            unit.can_move = 1;
        }
    }
    out << "=\n";
    ++turn;
}

bool is_end() {
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            check_square(i, j);
            if (map[i][j] > 0) return 0;
            if (units[i][j].gold > 0) return 0;
        }
    }
    return 1;
}

bool next_turn() {
    bool first = 1;
    while (next_move(first)) {
        first = 0;
    }
    end_turn();
    return !is_end();
}

void input(int a) {
    cin >> ns[a];
    for (int i = 0; i < ns[a]; ++i) {
        for (int j = 0; j < ns[a]; ++j) {
            cin >> map_orig[a][i][j];
        }
    }
}

void game(int number, int limit) {
    try_counter = -1;
    do {
        ++try_counter;
        n = ns[number];
        int stones_count = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                map[i][j] = map_orig[number][i][j];
                units[i][j] = Unit();
                if (map[i][j] < 0) ++stones_count;
            }
        }
        stone_prob = float(stones_count) / (n * n - 1);
        MAX_UNITS = 28;
        if (stone_prob == 0 && limit >= 600) MAX_UNITS = 11;
        gold = 200;
        turn = 0;
        number_of_units = 0;
        number_of_tanks = 0;
        out.str("");

        int new_limit = limit + (stone_prob != 0) * limit * 0.4;
        while (next_turn() && turn < new_limit) {}
        out << "===\n";

        if (turn >= new_limit) {
            continue;
        }
        int score = turn + 1;

        if (score < scores[number] || scores[number] == 0) {
            scores[number] = score;
            answers[number] = out.str();
        }
    } while (scores[number] == 0);
}

int overall_score(int T) {
    int ret = 0;
    for (int i = 0; i < T; ++i) {
        ret += scores[i];
    }
    return ret;
}

int main() {
    ios::sync_with_stdio(0);

    int T, k;
    cin >> T >> k;
    for (int i = 0; i < T; ++i) input(i);

    int limit = min(k, 800) * 1.1;
    for (int i = 0; i < T; ++i) {
        game(i, limit);
    }

    second_encounter = 1;
    while (overall_score(T) > T * k) {
        int i = int(random1() * 32768) % T;
        game(i, limit);
    }

    for (int i = 0; i < T; ++i) {
        cout << answers[i];
    }

    return 0;
}