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
#include <iostream>
#include <algorithm>

#if 0
void assert(bool condition)
{
	if(!condition)
	{
		int x=0;
		++x;
	}
}
#else
#define assert(condition)
#endif

#define MAX_N 20
#define MAX_FARMERS MAX_N

struct field_t
{
	int gold;
	int rocks;
	int farmer;
};

struct farmer_t
{
	int x;
	int y;
	int gold;
	int trying_to_move;
	int moved_this_round;
};

int n;
field_t world[MAX_N][MAX_N];

int bank;
int gold_to_collect;
int num_fields_with_gold;

int num_farmers;
farmer_t farmers[MAX_FARMERS];

int round_index;

#if 0
void print_world()
{
	for(int y=0;y<n;++y)
	{
		for(int x=0;x<n;++x)
		{
			const field_t& field=world[y][x];
			if(field.gold)printf("G:%3d  ",field.gold);
			else if(field.rocks)printf("R:%3d  ",field.rocks);
			else printf("E:     ");
		}
		printf("\n");

		for(int x=0;x<n;++x)
		{
			const field_t& field=world[y][x];
			if(field.farmer!=-1)printf("F%d:%3d ",field.farmer,farmers[field.farmer].gold);
			else printf("       ");
		}
		printf("\n");
	}
}
#else
#define print_world()
#endif

void read_world()
{
	round_index=0;

	bank=200;
	gold_to_collect=0;
	num_fields_with_gold=0;

	std::cin>>n;
	for(int y=0;y<n;++y)
		for(int x=0;x<n;++x)
		{
			int v;
			std::cin>>v;
			field_t& field=world[y][x];
			field.farmer=-1;
			field.gold=v>0?v:0;
			field.rocks=v<0?-v:0;

			gold_to_collect+=field.gold;
			if(field.gold)++num_fields_with_gold;
		}

	num_farmers=0;
}

int spawn_farmer()
{
	assert(world[0][0].farmer==-1);
	assert(bank>=100);

	bank-=100;

	int new_farmer_index=num_farmers++;
	farmer_t& farmer=farmers[new_farmer_index];
	farmer.x=farmer.y=0;
	farmer.gold=0;
	farmer.trying_to_move=false;
	farmer.moved_this_round=false;
	
	world[0][0].farmer=new_farmer_index;

	printf("R FARMER\n");

	print_world();

	return new_farmer_index;
}

int try_spawn_farmer()
{
	if(bank<100||world[0][0].farmer!=-1)
		return -1;
	return spawn_farmer();
}

void move_farmer(int farmer_index,int target_x,int target_y)
{
	assert(world[target_y][target_x].farmer==-1);

	farmer_t& farmer=farmers[farmer_index];
	world[farmer.y][farmer.x].farmer=-1;

	printf("M %d %d %d %d\n",farmer.y,farmer.x,target_y,target_x);

	farmer.x=target_x;
	farmer.y=target_y;
	farmer.moved_this_round=true;
	
	world[farmer.y][farmer.x].farmer=farmer_index;

	print_world();
}

enum dir_e
{
	left,
	right,
	up,
	down
};
bool try_move_farmer(int farmer_index,int target_x,int target_y);

bool try_move_out_farmer(int farmer_index,int pusher_x,int pusher_y)
{
	farmer_t& farmer=farmers[farmer_index];
	if(farmer.trying_to_move)return false;
	if(farmer.moved_this_round)return false;

	dir_e dirs[4];
	if(farmer.x==pusher_x)
	{
		dirs[0]=right;
		dirs[1]=left;
		dirs[2]=down;
		dirs[3]=up;
	}
	else
	{
		dirs[0]=down;
		dirs[1]=up;
		dirs[2]=right;
		dirs[3]=left;
	}
	for(int i=0;i<4;++i)
	{
		int target_x=farmer.x;
		int target_y=farmer.y;
		switch((dir_e)i)
		{
		case right: ++target_x;break;
		case left: --target_x;break;
		case down: ++target_y;break;
		case up: --target_y;break;
		}
		if(target_x<0||target_x>=n)continue;
		if(target_y<0||target_y>=n)continue;

		if(try_move_farmer(farmer_index,target_x,target_y))
			return true;
	}

	return false;
}

bool try_move_farmer(int farmer_index,int target_x,int target_y)
{
	farmer_t& farmer=farmers[farmer_index];
	farmer.trying_to_move=true;

	int occupying_farmer=world[target_y][target_x].farmer;
	if(occupying_farmer!=-1&&
		!try_move_out_farmer(occupying_farmer,farmer.x,farmer.y))
	{
		farmer.trying_to_move=false;
		return false;
	}
	move_farmer(farmer_index,target_x,target_y);

	farmer.trying_to_move=false;
	return true;
}

bool need_more_farmers()
{
	if(num_farmers==MAX_FARMERS)return false;
	return
		(num_fields_with_gold>num_farmers)&&
		((gold_to_collect+199)/200>num_farmers);
}

void try_move_farmer_to_base(int farmer_index)
{
	const farmer_t& farmer=farmers[farmer_index];
	if(farmer.x&&(!farmer.y||(rand()&1)))
		if(try_move_farmer(farmer_index,farmer.x-1,farmer.y))
			return;
	if(farmer.y)
		if(try_move_farmer(farmer_index,farmer.x,farmer.y-1))
			return;
	// deep shit
	// maybe next round will bring some change
}

void organize_one_more_farmer()
{
	if(try_spawn_farmer()==-1)
		return;

	int best_farmer=-1;
	int best_farmer_distance;
	for(int i=0;i<num_farmers;++i)
	{
		const farmer_t& farmer=farmers[i];
		if(farmer.gold>=100)
		{
			int distance=farmer.x+farmer.y;
			if(best_farmer==-1||distance<best_farmer_distance)
			{
				best_farmer=i;
				best_farmer_distance=distance;
			}
		}
	}

	if(best_farmer==-1)return;

	try_move_farmer_to_base(best_farmer);
}

void progress_round_index()
{
	++round_index;
	for(int i=0;i<num_farmers;++i)
		farmers[i].moved_this_round=false;
}

void try_move_farmer_furthest_from_base(int farmer_index)
{
	const farmer_t& farmer=farmers[farmer_index];
	if(farmer.x+1<n&&(farmer.y+1>=n||(rand()&1)))
		if(try_move_farmer(farmer_index,farmer.x+1,farmer.y))
			return;
	if(farmer.y+1<n)
		if(try_move_farmer(farmer_index,farmer.x,farmer.y+1))
			return;
}

void try_move_farmer_towards(int farmer_index,int target_x,int target_y)
{
	farmer_t& farmer=farmers[farmer_index];
	if(farmer.y<target_y&&
		try_move_farmer(farmer_index,farmer.x,farmer.y+1))
		return;

	if(farmer.y>target_y&&
		try_move_farmer(farmer_index,farmer.x,farmer.y-1))
		return;

	if(farmer.x>target_x&&
		try_move_farmer(farmer_index,farmer.x-1,farmer.y))
		return;

	if(farmer.x<target_x&&
		try_move_farmer(farmer_index,farmer.x+1,farmer.y))
		return;
}

bool find_free_gold_furthest_from_base(int& free_gold_x,int& free_gold_y)
{
	int best_distance=-1;
	for(int y=0;y<n;++y)
		for(int x=0;x<n;++x)
		{
			const field_t& field=world[y][x];
			if(field.farmer==-1&&field.gold)
			{
				int distance=x+y;
				if(best_distance==-1||distance>best_distance)
				{
					free_gold_x=x;
					free_gold_y=y;
					best_distance=distance;
				}
			}
		}
	return best_distance!=-1;
}

bool find_free_gold_nearest_from_base(int& free_gold_x,int& free_gold_y)
{
	int best_distance=-1;
	for(int y=0;y<n;++y)
		for(int x=0;x<n;++x)
		{
			const field_t& field=world[y][x];
			if(field.farmer==-1&&field.gold)
			{
				int distance=x+y;
				if(best_distance==-1||distance<best_distance)
				{
					free_gold_x=x;
					free_gold_y=y;
					best_distance=distance;
				}
			}
		}
	return best_distance!=-1;
}

void try_move_farmer_to_gold_nearest_from_base(int farmer_index)
{
	int free_gold_x,free_gold_y;
	if(!find_free_gold_nearest_from_base(free_gold_x,free_gold_y))
		return;

	const farmer_t& farmer=farmers[farmer_index];
	if(farmer.x+farmer.y<=free_gold_x+free_gold_y&&
		world[farmer.y][farmer.x].gold)return;

	try_move_farmer_towards(farmer_index,free_gold_x,free_gold_y);
}

bool try_move_farmer_to_gold_furthest_from_base(int farmer_index)
{
	const farmer_t& farmer=farmers[farmer_index];

	int free_gold_x,free_gold_y;
	if(!find_free_gold_furthest_from_base(free_gold_x,free_gold_y))
		return world[farmer.y][farmer.x].gold>0;

	if(farmer.x+farmer.y>=free_gold_x+free_gold_y&&
		world[farmer.y][farmer.x].gold)return true;

	try_move_farmer_towards(farmer_index,free_gold_x,free_gold_y);
	return true;
}

void update_farmer(int farmer_index)
{
	farmer_t& farmer=farmers[farmer_index];
	if(farmer.moved_this_round)return;

	if(!gold_to_collect)
	{
		if(farmer.gold)
			try_move_farmer_to_base(farmer_index);
		else
			try_move_farmer_furthest_from_base(farmer_index);
		return;
	}

	if(need_more_farmers())
		try_move_farmer_to_gold_nearest_from_base(farmer_index);
	else if(!try_move_farmer_to_gold_furthest_from_base(farmer_index))
		try_move_farmer_furthest_from_base(farmer_index);;
}

void update_farmers()
{
	for(int i=0;i<num_farmers;++i)
		update_farmer(i);
}

void do_round()
{
	progress_round_index();	

	int num_farmers_before=num_farmers;
	if(need_more_farmers())
		organize_one_more_farmer();
	
	update_farmers();

	if(num_farmers!=num_farmers_before&&need_more_farmers())
	{
		int new_farmer_index=try_spawn_farmer();
		update_farmer(new_farmer_index);
	}
}

void end_round()
{
	for(int y=0;y<n;++y)
		for(int x=0;x<n;++x)
		{
			field_t& field=world[y][x];
			if(field.gold&&field.farmer!=-1)
			{
				int collected_gold=std::min(10,field.gold);
				farmers[field.farmer].gold+=collected_gold;
				field.gold-=collected_gold;
				gold_to_collect-=collected_gold;
				if(!field.gold)--num_fields_with_gold;
			}
		}
	if(world[0][0].farmer!=-1)
	{
		farmer_t& farmer=farmers[world[0][0].farmer];
		bank+=farmer.gold;
		farmer.gold=0;
	}

	printf("=\n");

	print_world();
}

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

bool is_done()
{
	if(gold_to_collect)return false;
	for(int i=0;i<num_farmers;++i)
		if(farmers[i].gold)
			return false;
	return true;
}

void solve()
{
	read_world();

	while(!is_done())
	{
		do_round();
		end_round();
	}
	end_game();
}

int main()
{
	int t,k;
	std::cin>>t>>k;
	for(int i=0;i<t;++i)
		solve();
	return 0;
}

/*

2 100
2
0 1
1 1
2
0 100
100 100


2 12
3
0 -8 -512
-16 -1 -128
8 -2 -512
3
0 64 -1
64 -1 -1
1 -1 -1


*/