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
#include <bits/stdc++.h>
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
#define TRAV(i, a) for(auto &i : a)
#define SZ(i) ((int)(i).size())
#define X first
#define Y second
#define PR std::pair
#define PII std::pair<int, int>
#define MP std::make_pair
#define ll long long
#define VI std::vector<int>

bool debug = false;

const int  DOBAZYMULT = 1;
const int  DOBAZYADD = 200;
const int  MAXFARMERS = 50;
const int  WAIT = 4;
const int WAIT_POWROT = 1;
const int  INCREMENT = 200;

const int INF = 1000000000;

struct Node{
	Node *parent;
	std::vector<Node*> ed;
	int gold, occupied;
	bool plsmove;
	int x, y;
	bool base;
};

struct Farmer{
	int x, y, gold, dobazy, waited;
	Node *node;
};

struct FARMCOMP{
	bool operator()(const Farmer &a, const Farmer &b){
		return (a.x == b.x ? a.y > b.y : a.x > b.x);
	}
};

struct FARMREVCOMP{
	bool operator()(const Farmer &a, const Farmer &b){
		return (a.x == b.x ? a.y < b.y : a.x < b.x);
	}
};

int n, gold, gold_left, gold_not_base;
int tab[25][25];
std::vector<Farmer> farmers;
Node *base;

void move_farmer(Farmer &far, Node *node){
	std::cout << "M " << far.y-1 << " " << far.x-1 << " " << node->y-1 << " " << node->x-1 << "\n";
	far.node->plsmove = false;
	far.node->occupied = false;
	node->occupied = true;
	far.node = node;
	far.x = node->x;
	far.y = node->y;
}

Node *closest_gold(Node *node){
	std::queue<PR<Node*, Node*> > que;
	Node *ans = nullptr;
	std::set<Node*> vis;
	TRAV(nd, node->ed) if(!nd->occupied) que.push(MP(nd, nd)), vis.insert(nd);
	vis.insert(node);
	while(!que.empty()){
		auto par = que.front();
		que.pop();
		Node *cur = par.X;
		if(cur->gold > 0){
			ans = par.Y;
			break;
		}
		TRAV(nd, cur->ed){
			if(nd->occupied || vis.count(nd)) continue;
			vis.insert(nd);
			que.push(MP(nd, par.Y));
		}
	}
	return ans;
}

int main(){
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(0);
	int test, srednia;
	std::cin >> test >> srednia;
	while(test--){
		std::cin >> n;
		farmers.clear();
		gold = 200;
		gold_left = 0;
		gold_not_base = 0;
		base = nullptr;
		FOR(i, 25) FOR(j, 25) tab[i][j] = -INF;
		REP(i, 1, n+1) REP(j, 1, n+1) std::cin >> tab[i][j], gold_left += tab[i][j];
		gold_not_base = gold_left;
		std::map<PII, Node*> mapcord;
		REP(i,1, n+1) REP(j, 1, n+1){
			mapcord[MP(i, j)] = new Node();
		}
		REP(i, 1, n+1) REP(j, 1, n+1){
			Node *node = mapcord[MP(i, j)];
			if(i == 1 && j == 1) node->parent = nullptr;
			else if(i == 1) node->parent = mapcord[MP(i, j-1)];
			else if(j == 1) node->parent = mapcord[MP(i, j+1)];
			else node->parent = mapcord[MP(i-1, j)];
			if(i == 1 && j == 1) base = node;
			node->y = i;
			node->x = j;
			node->gold = tab[i][j];
			node->base = (i == 1 && j == 1);
			node->occupied = false;
			mapcord[MP(i, j)] = node;
		}
		REP(i, 1, n+1) REP(j, 1, n+1){
			if(j < n) mapcord[MP(i, j)]->ed.push_back(mapcord[MP(i, j+1)]);
			if(j > 1) mapcord[MP(i, j)]->ed.push_back(mapcord[MP(i, j-1)]);
			if(i < n) mapcord[MP(i, j)]->ed.push_back(mapcord[MP(i+1, j)]);
			if(i > 1) mapcord[MP(i, j)]->ed.push_back(mapcord[MP(i-1, j)]);
		}
		int runda = 0;
		while(gold_not_base > 0){
			runda++;
			if(gold_left > 0) std::sort(farmers.begin(), farmers.end(), FARMCOMP());
			else std::random_shuffle(farmers.begin(), farmers.end());
			if(gold >= 100 && SZ(farmers) < MAXFARMERS && !base->occupied && gold_left > 0){
				std::cout << "R FARMER\n";
				gold -= 100;
				Farmer farmer = Farmer();
				farmer.x = 1;
				farmer.y = 1;
				farmer.node = base;
				base->occupied = true;
				farmer.gold = 0;
				farmer.dobazy = SZ(farmers)*SZ(farmers)*DOBAZYMULT + DOBAZYADD;
				farmers.push_back(farmer);
			}
			TRAV(far, farmers){
				if(gold_left == 0){
					if(far.gold == 0){
						bool moved = false;
						TRAV(nd, far.node->ed){
							if(!nd->occupied && (nd->x < far.x || nd->y > far.y)){
								move_farmer(far, nd);
								moved = true;
								break;
							}
						}
						if(!moved && (far.node->plsmove || far.x < 7 || far.y < 7)){
							std::random_shuffle(far.node->ed.begin(), far.node->ed.end());
							TRAV(nd, far.node->ed){
								if(!nd->occupied){
									move_farmer(far, nd);
									break;
								}
							}
						}
					}else{
						if(far.node->parent->occupied){
							far.node->parent->plsmove = true;
							far.waited++;
							if(far.waited > WAIT_POWROT){
								far.waited = 0;
								TRAV(nd, far.node->ed){
									if(!nd->occupied && (nd->x > far.x || nd->y > far.y)){
										move_farmer(far, nd);
										break;
									}
								}
							}
						}else{
							move_farmer(far, far.node->parent);
						}
					}
				}
				else if(far.gold >= far.dobazy && SZ(farmers) < MAXFARMERS){
					if(far.node->parent->occupied){
						far.node->parent->plsmove = true;
						far.waited++;
						if(far.waited > WAIT){
							far.waited = 0;
							far.dobazy += INCREMENT;
						}
					}else{
						move_farmer(far, far.node->parent);
					}
				}else if(far.node->gold == 0 || far.node->plsmove){
					Node *node = closest_gold(far.node);
					if(node != nullptr){
						move_farmer(far, node);
					}else if(far.node->plsmove){
						std::random_shuffle(far.node->ed.begin(), far.node->ed.end());
						TRAV(nd, far.node->ed){
							if(!nd->occupied){
								move_farmer(far, nd);
								break;
							}
						}
					}
				}
			}
			TRAV(far, farmers){
				if(far.node->gold > 0){
					far.gold += std::min(10, far.node->gold);
					gold_left -= std::min(10, far.node->gold);
					far.node->gold = far.node->gold - std::min(far.node->gold, 10);
				}
				if(far.node == base){
					gold += far.gold;
					gold_not_base -= far.gold;
					far.gold = 0;
					far.dobazy = SZ(farmers)*SZ(farmers)*DOBAZYMULT + DOBAZYADD;
				}
			}
			std::cout << "=\n";
		}
		std::cout << "===" << "\n";
	}
	return 0;
}