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
#include<bits/stdc++.h>
//#include<ncurses.h>
//#include<unistd.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

#define PB push_back
#define ST first
#define ND second

const int maxn = 30;
int t[maxn][maxn];
int n;
int rno;

pii dp[] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

pii operator+(pii a, pii b) { return pii(a.ST+b.ST, a.ND+b.ND); }
ostream &operator<<(ostream &stream, pii a)
{
	stream << a.ST << " " << a.ND;
	return stream;
}

enum Type { NONE = 0, FARMER = 1, TANK = 1};
enum Direction { RIGHT = 0, UP = 1, LEFT = 2, DOWN = 3};
enum Action {returning, findingRow, inRow};

struct Robot {
	pii p;
	Type type;
	int gold;
	Action act;
	Robot(pii _p, Type _type) : p(_p), type(_type) {gold = 0; act=findingRow; }
};

Type u[maxn][maxn];
int suf[maxn][maxn];
vector<Robot> v;
int totalGold;
int ret;

/*int disp=0;

void display()
{
	if(disp == 0)
	{
		initscr();
		cbreak();
		keypad(stdscr, TRUE);
		noecho();
		disp = 1;
	}
	clear();
	for(int i=0; i < n; i++)
	{
		for(int j=0; j < n; j++)
		{
			string s = to_string(t[i][j]);
			mvprintw(2*i+2, 5*j, s.c_str());
		}
	}
	for(Robot &r : v)
	{
		string s = "F";
		mvprintw(2*r.p.ST+1, 5*r.p.ND, s.c_str());
	}
	string s = to_string(rno);
	mvprintw(0, 0, s.c_str());
	s = "gold: " + to_string(totalGold);
	mvprintw(0, 20, s.c_str());
	refresh();
	usleep(50000);
}*/

bool move(Robot &r, Direction d)
{
	pii q = r.p + dp[d];
	if(q.ST < 0 || q.ST >= n || q.ND < 0 || q.ND >= n)
		return false;
	if(u[q.ST][q.ND] != NONE)
		return false;
	if(r.type == FARMER && t[q.ST][q.ND] < 0)
		return false;
	u[q.ST][q.ND] = r.type;
	u[r.p.ST][r.p.ND] = NONE;
	cout << "M " << r.p << " " << q << "\n";
	r.p = q;
	return true;
}

bool buy(Type type)
{
	if(totalGold < 100 || type == NONE)
		return false;
	totalGold-=100;
	u[0][0] = type;
	v.PB(Robot({0, 0}, type));
	cout << "R ";
	if(type == FARMER)
		cout << "FARMER\n";
	else
		cout << "TANK\n";
	return true;
}

void endTurn()
{
	for(Robot &r : v)
	{
		int &a = t[r.p.ST][r.p.ND];
		if(r.type == TANK && a < 0)
			a = min(0, a+10);
		else if(r.type == FARMER && a > 0)
		{
			if(a >= 10)
			{
				a-=10;
				r.gold+=10;
			}
			else
			{
				r.gold+=a;
				a = 0;
			}
		}
		else if(r.p.ST == 0 && r.p.ND == 0)
		{
			totalGold+=r.gold;
			r.gold=0;
		}
	}
	for(int i=0; i < n; i++)
	{
		suf[i][n-1] = t[i][n-1];
		for(int j=n-2; j >= 0; j--)
			suf[i][j] = t[i][j] + suf[i][j+1];
	}
	cout << "=\n";
	//display();
	rno++;
}

bool isRowFree(int a)
{
	for(int i=1; i < n; i++)
		if(u[a][i] == FARMER)
			return false;
	return true;
}

bool isColumnFree(int a)
{
	for(int i=0; i < n; i++)
		if(u[i][a] == FARMER || u[i][a] == TANK)
			return false;
	return true;
}

void playWithRocks()
{
	cout << "===\n";
}

bool cmp(const Robot &a, const Robot &b)
{
	if(a.p.ST < b.p.ST)
		return true;
	return false;
}

void playWithoutRocks()
{
	buy(FARMER);
	move(v.back(), RIGHT);
	buy(FARMER);
	move(v.back(), DOWN);
	endTurn();
	while(true)
	{
		for(int i=0; i < v.size(); i++)
		{
			Robot &r = v[i];
			if(r.act == inRow)
			{
				if(r.p.ND < n-1 && suf[r.p.ST][r.p.ND+1] > 0) {
					move(r, RIGHT);
				}
				else if(t[r.p.ST][r.p.ND]) {
					//stay here
				}
				else if(r.p.ND > 1)
					move(r, LEFT);
			}
			else if(r.act == findingRow)
			{
				if(isRowFree(r.p.ST))
				{
					move(r, RIGHT);
					r.act = inRow;
				}
				else
					move(r, DOWN);
			}
			else
			{
				if(r.p.ND > 1)
					move(r, LEFT);
				else if(r.p.ND == 1)
				{
					if(isColumnFree(0))
						move(r, LEFT);
				}
				else
				{
					move(r, UP);
					if(r.p.ST == 0)
					{
						r.act = findingRow;
						ret = -2;
					}
				}
			}
		}
		if(ret < 0)
			ret++;
		if(v.size() < n && ret<=0 && totalGold >= 100 && u[0][0] == NONE)
		{
			buy(FARMER);
		}
		endTurn();
		if(ret <= 0 && v.size() == n)
		{
			bool ch = true;
			for(Robot &r : v)
			{
				if(r.p.ND != 1)
					ch = false;
				if(suf[r.p.ST][r.p.ND] > 0)
					ch = false;
				if(ch == false)
					break;
			}
			if(ch)
				break;
		}
		if(ret == 0 && v.size() < n && totalGold < 100)
		{
			int maxi=0;
			for(int i=0; i < v.size(); i++)//choose one to return
			{
				if(v[i].gold > v[maxi].gold)
					maxi = i;
			}
			if(v[maxi].gold > 0) {
				v[maxi].act = returning;
				ret = 1;
			}
		}
	}
	for(Robot &r : v)
	{
		move(r, LEFT);
	}
	endTurn();
	int maxv=0;
	for(int i=0; i < n; i++)
		maxv = max(maxv, t[i][0]);
	maxv = maxv/10+1;
	for(int i=0; i < maxv; i++)
		endTurn();
	sort(v.begin(), v.end(), cmp);
	
	int cc=n-1;
	while(cc--)
	{
		for(Robot &r : v)
		{
			if(r.p.ST == 0)
				move(r, RIGHT);
			else
				move(r, UP);
		}
		endTurn();
	}
}

void play()
{
	cin >> n;
	bool ch = false;
	for(int i=0; i < n; i++)
	{
		for(int j=0; j < n; j++)
		{
			u[i][j] = NONE;
			cin >> t[i][j];
			if(t[i][j] < 0)
				ch = true;
		}
	}
	v.clear();
	totalGold=200;
	rno=1;
	ret=0;
	if(ch)
		playWithRocks();
	else
		playWithoutRocks();
	cout << "===\n";
}

int main()
{
	ios_base::sync_with_stdio(0);
	int T, k;
	cin >> T >> k;
	while(T--)
	{
		play();
	}
	/*sleep(2);
	endwin();*/
	
	return 0;
}