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
#include <algorithm>
#include <cstdio>
#include <memory>
#include <vector>


const int MAX_N = 29;


int board[MAX_N][MAX_N];
int n;

int round_counter = 0;
int gold_at_home = 200;


void read_board()
{
    scanf("%d", &n);

    for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j)
            scanf("%d", &board[i][j]);
}


void print_board()
{
    for(int i = 0; i < n; ++i)
    {
        for(int j = 0; j < n; ++j)
        {
            printf("%d ", board[i][j]);
        }
        printf("\n");
    }
    printf("rounds passed: %d\n", round_counter);
}


void print_gold_on_board()
{
    int gold_on_board = 0;
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j)
            gold_on_board += board[i][j];
    printf("gold on board: %d\n", gold_on_board);
}


void print_gold_at_home()
{
    printf("gold at home: %d\n", gold_at_home);
}


struct Farmer
{
    Farmer() : x(0), y(0), gold_in_backpack(0) {}

    bool can_move_right()
    {
        return ((y+1) < n);
    }

    bool can_move_left()
    {
        return (y > 0);
    }

    bool can_move_up()
    {
        return (x > 0);
    }

    bool can_move_down()
    {
        return ((x+1) < n);
    }

    void move_right()
    {
        printf("M %d %d %d %d\n", x, y, x, y+1);
        ++y;
    }

    void move_left()
    {
        printf("M %d %d %d %d\n", x, y, x, y-1);
        --y;
    }

    void move_up()
    {
        printf("M %d %d %d %d\n", x, y, x-1, y);
        --x;
    }

    void move_down()
    {
        printf("M %d %d %d %d\n", x, y, x+1, y);
        ++x;
    }

    bool is_at_home()
    {
        return ((x == 0) && (y == 0));
    }

    bool is_on_gold()
    {
        return (board[x][y] > 0);
    }

    void gather_gold()
    {
        const int gathered_gold = std::min(10, board[x][y]);
        board[x][y] -= gathered_gold;
        gold_in_backpack += gathered_gold;

//        printf("farmer gathered %d gold at (%d %d) remaining: %d\n", gathered_gold, x, y, board[x][y]);
    }

    void leave_gold_at_home()
    {
//        printf("adding %d gold\n", gold_in_backpack);

        gold_at_home += gold_in_backpack;
        gold_in_backpack = 0;
    }

    int x, y;
    int gold_in_backpack;
};


std::vector<std::shared_ptr<Farmer>> farmers;


void print_farmers()
{
    printf("farmers count: %lu\n", farmers.size());
    for(const auto& f : farmers)
        printf("%d %d\n", f->x, f->y);
}


std::shared_ptr<Farmer> recruit_farmer()
{
    printf("R FARMER\n");
    gold_at_home -= 100;
    farmers.push_back(std::make_shared<Farmer>());

    return farmers.back();
}


void end_round()
{
    for(int i = 0; i < farmers.size(); ++i)
    {
        if(farmers[i]->is_on_gold())
            farmers[i]->gather_gold();

        if(farmers[i]->is_at_home())
            farmers[i]->leave_gold_at_home();
    }

    ++round_counter;
    printf("=\n");
}


void end_game()
{
    printf("===\n");
}


void move_all_farmers_to_the_right_limit_gathering_all_gold(const int right_limit)
{
    while(true)
    {
        for(const auto& f : farmers)
        {
            if(f->is_on_gold())
                continue;

            if(f->y < right_limit)
                f->move_right();
        }

        end_round();

        bool done = true;
        for(const auto& f : farmers)
        {
            if(f->is_on_gold() || (f->y < right_limit))
            {
                done = false;
                break;
            }
        }

        if(done)
            return;
    }
}


void move_all_farmers_to_the_left_gathering_all_gold()
{
    while(true)
    {
        for(const auto& f : farmers)
        {
            if(f->is_on_gold())
                continue;

            if(f->can_move_left())
                f->move_left();
        }

        end_round();

        bool done = true;
        for(const auto& f : farmers)
        {
            if(f->is_on_gold() || f->can_move_left())
            {
                done = false;
                break;
            }
        }

        if(done)
            return;
    }
}


void move_all_farmers_down_gathering_all_gold()
{
    while(true)
    {
        for(const auto& f : farmers)
        {
            if(f->is_on_gold())
                continue;

            if(f->can_move_down())
                f->move_down();
        }

        end_round();

        bool done = true;
        for(const auto& f : farmers)
        {
            if(f->is_on_gold() || f->can_move_down())
            {
                done = false;
                break;
            }
        }

        if(done)
            return;
    }
}


void move_all_farmers_up()
{
    while(true)
    {
        for(const auto& f : farmers)
        {
            if(f->can_move_up())
                f->move_up();
        }

        end_round();

        bool done = true;
        for(const auto& f : farmers)
        {
            if(f->can_move_up())
            {
                done = false;
                break;
            }
        }

        if(done)
            return;
    }
}


void move_all_farmers_left_and_down()
{
    while(farmers.back()->x < (n-1))
    {
        for(int i = farmers.size() - 1; i >= 0; --i)
        {
            if(farmers[i]->y == 0)
            {
                farmers[i]->move_down();
            }
            else
            {
                farmers[i]->move_left();
            }
        }

        end_round();
    }
}


void move_all_farmers_to_the_right_and_recruit_up_to_n_farmers()
{
    while(true)
    {
        for(int i = 0; i < farmers.size(); ++i)
        {
            const int y_destination = n - i - 1;
            if(farmers[i]->y < y_destination)
                farmers[i]->move_right();
        }

        if(farmers.size() < n)
        {
            if(gold_at_home >= 100)
                recruit_farmer();
        }

        end_round();

        const bool can_recruit_more = (gold_at_home >= 100);

        if(can_recruit_more)
        {
            if(farmers.size() == n)
            {
                bool done = true;
                for(int i = 0; i < farmers.size(); ++i)
                {
                    const int y_destination = n - i - 1;
                    if(farmers[i]->y < y_destination)
                    {
                        done = false;
                        break;
                    }
                }

                if(done)
                    return;
            }
        }
        else
        {
            bool done = true;
            for(int i = 0; i < farmers.size(); ++i)
            {
                const int y_destination = n - i - 1;
                if(farmers[i]->y < y_destination)
                {
                    done = false;
                    break;
                }
            }

            if(done)
                return;
        }
    }
}


int find_right_limit()
{
    int s = 0;
    for(int i = 0; i < n; ++i)
    {
        s += board[0][i];
        s += board[1][i];
        s += board[2][i];
        s += board[3][i];

        if(s >= (n-2)*100)
            return i;
    }

    return (n-1);
}


void recruit_two_farmers_and_gather_gold_from_first_four_rows()
{
    // first round
    auto first_farmer = recruit_farmer();
    first_farmer->move_right();
    auto second_farmer = recruit_farmer();
    second_farmer->move_down();
    end_round();

    // second round
    if(!first_farmer->is_on_gold())
        first_farmer->move_right();
    second_farmer->move_down();
    end_round();

    // both go to the right
    const int right_limit = find_right_limit();
    move_all_farmers_to_the_right_limit_gathering_all_gold(right_limit);

    // move both 1 row down
    first_farmer->move_down();
    second_farmer->move_down();
    end_round();

    // both go to the left
    move_all_farmers_to_the_left_gathering_all_gold();

    first_farmer->move_up(); // gets home
    second_farmer->move_up();
    end_round();

    first_farmer->move_right();
    second_farmer->move_up();
    end_round();

    first_farmer->move_right();
    second_farmer->move_up(); // gets home
    end_round();

    move_all_farmers_to_the_right_and_recruit_up_to_n_farmers();

    move_all_farmers_down_gathering_all_gold();
    move_all_farmers_up();

    move_all_farmers_left_and_down();

    end_game();

//    print_board();
//    print_farmers();
//    print_gold_on_board();
//    print_gold_at_home();
}


void play_game()
{
    read_board();

    recruit_two_farmers_and_gather_gold_from_first_four_rows();
}


int main()
{
    int tests, k;
    scanf("%d%d", &tests, &k);

    while(tests--)
    {
        round_counter = 0;
        gold_at_home = 200;

        farmers.clear();

        play_game();
    }
}