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
// bade chociaz 3 punkciki ???????? :(
// no chyba beda

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// gp_hash_table<int, int> mapka;

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define PII pair<int,int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream& os, vector<T> V) {
  os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

int dx[] = {0,1,0,-1,0};
int dy[] = {1,0,-1,0,0};
int TURY = 0;
const int inf = 1e9;
vector<vector<int>> RESY;
struct Sol{
  vector<int> KIEDY;
  int zloto = 200;
  int n;
  vector<PII> farmer,tank;
  vector<int> kieszen;
  vector<vector<int>> board;
  vector<vector<int>> zaj;
  
  void rekrutuj_farmera(){
    cout << "R FARMER\n";
    assert(zaj[0][0] == -1);
    zaj[0][0] = SZ(farmer);
    farmer.PB({0ll,0ll});
    kieszen.PB(0);
    assert(zloto >= 100);
    zloto -= 100;
  }
  void rekrutuj_tank(){
    cout << "R TANK\n";
    assert(zaj[0][0] == -1);
    zaj[0][0] = SZ(tank);
    tank.PB({0ll,0ll});
    assert(zloto >= 100);
    zloto -= 100;
  }
  void move(PII& x, int k, bool czolg){
    int nx = x.FI + dx[k];
    int ny = x.SE + dy[k];
    assert(nx >= 0 && nx < n && ny >= 0 && ny < n);
    if(zaj[nx][ny] != -1){
      debug(KIEDY);
      debug(zaj,plan[time],dx[k],dy[k]);
      debug(plan[time][x.FI][x.SE]);
      debug(plan[time][nx][ny]);
      debug(zaj[x.FI][x.SE]);
      debug(zaj[nx][ny]);
      
     // debug(RESY[0],RESY[1]);
      assert(0);
    }
    if(!czolg)
      assert(board[nx][ny] >= 0);
    zaj[nx][ny] = zaj[x.FI][x.SE];
    zaj[x.FI][x.SE] = -1;
    cout << "M " << x.FI << " " << x.SE << " " << nx << " " << ny << "\n";
    x = {nx,ny};
  }
  void koniec_tury(){
    TURY++;
    R(i,SZ(farmer)){
      int pom = min(10ll, board[farmer[i].FI][farmer[i].SE]);
      board[farmer[i].FI][farmer[i].SE] -= pom;
      kieszen[i] += pom;
      if(farmer[i] == MP(0ll,0ll)){
        zloto += kieszen[i];
        kieszen[i] = 0;
      }
    }
    R(i,SZ(tank)){
      int pom = min(10ll, -board[tank[i].FI][tank[i].SE]);
      board[tank[i].FI][tank[i].SE] += pom;
    }
    cout << "=\n";
  }
  bool koniec_gry(){
    for(int el:kieszen){
      if(el > 0)return 0;
    }
    R(i,n)R(j,n)if(board[i][j] > 0){
      return 0;
    }
    return 1;
  }
  
  struct ruch{
    int typ;
    PII pole;
    int kier;
    void wykonaj(Sol &x){
      debug("wykonuje ruch !!!!!!!!!!", typ, pole, kier);
      if(typ == 0){
        if(x.zaj[pole.FI][pole.SE] == -1){
          if(pole != MP(0ll,0ll)){
            debug(x.KIEDY, x.ile_farmerow);
            assert(0);
          }
          x.rekrutuj_farmera();
        }
        int id =  x.zaj[pole.FI][pole.SE];
        x.move(x.farmer[id], kier, 0);
      }else{
        if(x.zaj[pole.FI][pole.SE] == -1){
          assert(pole == MP(0ll,0ll));
          x.rekrutuj_tank();
        }
        int id =  x.zaj[pole.FI][pole.SE];
        x.move(x.tank[id], kier, 1);
      }
    }
  };
  
  vector<vector<ruch>> tura;
  vector<vector<vector<int>>> plan;
  vector<vector<int>> zostalo;
  vector<vector<int>> kiedy_wolne;
  int time;
  int nie_zbieram,lim;
  
  struct wpis{
    int t,x,y;
    friend ostream& operator<<(ostream& out, wpis p){
      out << "(t=" << p.t << ", " << p.x << ", " << p.y << ")"; return out;
    }
  };
  vector<vector<vector<PII>>> par;
  int ktore_idz = 0;
  vector<vector<vector<int>>> kiedy;
  
  void idz(int px,int py,int pt,int celx,int cely,vector<PII>& res){
    ktore_idz++;
    vector<wpis> t1,t2;
    //zawsze ide najbardziej w te sama strone, moze cos z tym zrobic ?
    auto add = [&](int t,int x,int y, PII pop){
      if(t >= lim || x < 0 || x >= n || y < 0 || y >= n || kiedy_wolne[x][y] > t || 
        plan[t][x][y] == 1 || (kiedy[t][x][y] == ktore_idz) || (celx == -1 && board[x][y] > 0))return;
     // debug(t,x,y,pop);
      kiedy[t][x][y] = ktore_idz;
      par[t][x][y] = pop;
      t2.PB({t,x,y});
    };
    add(pt,px,py,{-1,-1});
    while(!t2.empty()){
     // debug("!!!!!",t2);
      t1.clear();
      swap(t1,t2);
      for(auto el:t1){
        if(el.x == celx && el.y == cely && el.t != pt){
          vector<PII> res2;
          while(el.t != pt){
            res2.PB(MP(el.x,el.y));
            R(k,5){
              int nx = el.x + dx[k];
              int ny = el.y + dy[k];
              if(nx < 0 || nx >= n || ny < 0 || ny >= n)continue;
              if(kiedy[el.t-1][nx][ny] == ktore_idz){
                el.x = nx;
                el.y = ny;
                break;
              }
            }
       //     debug(el.x, el.y, el.t);
        //    tie(el.x, el.y) = par[el.t][el.x][el.y];
            el.t--;
          }
       //   debug(px, py, pt,  el.x, el.y, el.t);
          assert(px == el.x && py == el.y);
          reverse(ALL(res2));
          for(auto el2:res2){
            res.PB(el2);
          }
          return;
        }
        R(i,5){
          add(el.t + 1, el.x + dx[i], el.y + dy[i], {el.x,el.y});
        }
      }
    }
    if(celx == -1){
      int akx = -1, aky = -1;
      R(i,n)R(j,n){
        if(kiedy[lim-1][i][j] == ktore_idz){
          if(i + j > akx + aky){
            akx = i;
            aky = j;
          }
        }
      }
      if(akx != -1){
        int tt = lim-1;
        vector<PII> res2;
        while(tt != pt){
          res2.PB(MP(akx,aky));
          if(kiedy[tt-1][akx][aky] == ktore_idz){
            tt--;
          }else{
            tie(akx,aky) = par[tt][akx][aky];
            tt--;
          }
        }
        reverse(ALL(res2));
        for(auto el2:res2){
          res.PB(el2);
        }
        return;
      }
    }
    res.clear();
  }
  
  int ile_farmerow = 0;
  int ile_tankow = 0;
  
  bool buduj_tank(){
    if(ile_farmerow == 0 && ile_tankow == 1)return 0;
    //TODO zbuduj tanka 
    return 0;
  }
  bool rekrutuj(){
    if(zloto < 100 * (ile_farmerow + ile_tankow + 1) && plan[time][0][0] == 0)return 0;
    bool kupuje = 0;
    int x = 0, y = 0;
    vector<PII> res;
    int zl = 0;
    vector<vector<int>> zostalopam = zostalo;
    while(1){
      if(x == 0 && y == 0)zl = 0;
      int akx = inf;
      int aky = inf;
      R(i,n)R(j,n){
        if(zostalo[i][j]){
          if(abs(x - i) + abs(j - y) < abs(x - akx) + abs(y - aky)){
            akx = i;
            aky = j;
          }
        }
      }
      if(akx == inf && kupuje == 0){
        return 0;
      }
      kupuje = 1;
      if(akx == inf  || time + SZ(res) > lim - 80 - ile_farmerow * 5 ||
        (zl / (akx + aky + 1) >= 50 && time + SZ(res) < 300 )){//TODO szczegolnie to 100
        if(x == 0 && y == 0){
          break;
        }
        debug(akx,aky);
        //TODO kiedy wracac zeby bylo z czego budowac ?
        akx = 0;
        aky = 0;
      }
      debug(x,y,akx, aky,SZ(res),zl);
      idz(x, y, time + SZ(res), akx, aky, res);
      if(res.empty()){
        zostalo = zostalopam;
        return buduj_tank();
      }
      x = akx;
      y = aky;
      if(zostalo[x][y]){
        int pom = min(10ll, zostalo[x][y]);
        zl += pom;
        zostalo[x][y] -= pom;
      }
      /*while(zostalo[x][y] && plan[time + SZ(res) + 1][x][y] == 0){ // +1 ??
        debug(x,y,plan[time + SZ(res) + 1][x][y]);
        debug(plan[time + SZ(res) + 2][x][y]);
        debug(plan[time + SZ(res) + 3][x][y]);
        int pom = min(10ll, zostalo[x][y]);
        zl += pom;
        zostalo[x][y] -= pom;
        res.PB({x,y});
      }*/
    }
    if(res.empty()){
      zostalo = zostalopam;
      return buduj_tank();
    }
    
    ucieczki[time + SZ(res)] = 1;
    KIEDY.PB(time + SZ(res));
    debug(res,SZ(res));
    PII ost = {0,0};
    R(i,SZ(res)){
      R(j,4){
        if(res[i] == MP(ost.FI + dx[j],ost.SE + dy[j])){
          tura[i + time].PB({0,ost,j});
        }
      }
//       if(plan[i + time + 1][res[i].FI][res[i].SE] != 0 && 
//         res[i - 1] != res[i]){
//         debug(i + time + 1, res[i].FI, res[i].SE);
//         assert(0);
//       }
      //plan[i + time][res[i].FI][res[i].SE] = 1;
      plan[i + time ][res[i].FI][res[i].SE] = 1;
      plan[i + time + 1][res[i].FI][res[i].SE] = 1;
      if(i + time + 2 < SZ(plan)){
        plan[i + time + 2][res[i].FI][res[i].SE] = 1;
      }
      if(i == SZ(res) - 1){
        if(i + time + 2 < SZ(plan)){
          plan[i + time + 3][res[i].FI][res[i].SE] = 1;
        }
      }
//       debug(plan[0],plan[1],plan[2]);
//       assert(0);
      ost = res[i];
      //czy na pewno tak ?
    }
    
    ile_farmerow++;
 //   rekrutuj_farmera(); // czemu chciałem aby to tu było ?
    return 1;
  }
  void uciekaj(){
    debug("uciekaj !!!",zaj,board);
  // exit(0);
    vector<PII> res;
    plan[time][0][0] = 0;
    debug(time);
    idz(0,0,time, -1, -1, res);
    PII ost = {0,0};
    assert(SZ(res));
    R(i,SZ(res)){
      R(j,4){
        if(res[i] == MP(ost.FI + dx[j],ost.SE + dy[j])){
          tura[i + time].PB({0,ost,j});
        }
      }
      plan[i + time + 1][res[i].FI][res[i].SE] = 1;
      if(i + time + 2 < SZ(plan)){
        plan[i + time + 2][res[i].FI][res[i].SE] = 1;
      }
      ost = res[i];
    }
  }
  vector<int> ucieczki;
  void run(int _lim){
    lim = _lim;
    //nie_zbieram = lim - 100; // TODO
    cin >> n;
    time = 0;
    par.resize(lim, vector<vector<PII>>(n, vector<PII>(n, {inf,inf})));
    kiedy.resize(lim, vector<vector<int>>(n, vector<int>(n, 0)));
    ucieczki.resize(lim,0);
    kiedy_wolne.resize(n,vector<int>(n));
    board.resize(n,vector<int>(n));
    zostalo.resize(n,vector<int>(n));
    zaj.resize(n,vector<int>(n, -1));
    R(i,n)R(j,n){
      cin >> board[i][j];
      if(board[i][j] >= 0){
        zostalo[i][j] = board[i][j];
        kiedy_wolne[i][j] = 0;
      }else{
        kiedy_wolne[i][j] = inf;
      }
    }
    tura.resize(lim);
    plan.resize(lim,vector<vector<int>>(n, vector<int>(n)));
    while(!koniec_gry()){
      if(ucieczki[time])
        uciekaj();
      while(rekrutuj());
      reverse(ALL(tura[time]));
      for(auto el:tura[time]){
        el.wykonaj(*this);
      }
      time++;
      cerr << "-------------------" << time << " " << SZ(farmer) << " " << zloto << "\n";
      debug(kieszen);
      debug(board);
      koniec_tury();
    }
    cout << "===\n";
  }
};

int k;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  int T;
  cin >> T >> k;
  k = 600;
  TURY = T;
  int TT = T;
  while(TT--){
   // cout << TT << "\n";
    Sol sol;
    sol.run(k - 1);
  }
  //cout << (LD)TURY / T << " " << T * k << "\n";
}