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
#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
typedef long long LL;
typedef double ld;

int board[20][20];

struct Farmer {
    int r1, c1;
    int pts;
};

int updateFarmerAndBoard(Farmer& f, int& i, int& j, int n, int dir) {
    if(board[i][j] == 0) { 
        if(i+dir == n || i+dir == -1) {
            dir *= (-1);
            if(j+1 == n)
                return -2;
            j++;
            printf("M %d %d %d %d\n", i, j-1, i, j);
        } else {
            i += dir;
            printf("M %d %d %d %d\n", i-dir, j, i, j);
        }
    } 
    f.pts += ((board[i][j] < 10) ? board[i][j] : 10);
    board[i][j] = (board[i][j] < 10) ? 0 : board[i][j] - 10;  
    return dir;
}

void moveAndUpdate(Farmer& f, int i, int j) {
    f.r1 = i;
    f.c1 = j;
    f.pts += ((board[i][j] < 10) ? board[i][j] : 10);
    printf("M %d %d %d %d\n", i, j-1, i, j);
    board[i][j] = (board[i][j] < 10) ? 0 : board[i][j] - 10;  
}

int moveIfPossibleAndUpdate(Farmer& f, int n) {
    if(board[f.r1][f.c1] == 0) {
        if(f.r1 + 1 == n) return 1;
        printf("M %d %d %d %d\n", f.r1, f.c1, f.r1 + 1, f.c1);
        f.r1 += 1;    
    }
    f.pts += ((board[f.r1][f.c1] < 10) ? board[f.r1][f.c1] : 10);
    board[f.r1][f.c1]= (board[f.r1][f.c1] < 10) ? 0 : board[f.r1][f.c1] - 10;  
    return 0;
}

int main() {
    int n, m, T, k, p = 0;
    cin >> T >> k;
    for(int z=0; z < T; z++) {
        int rounds = 0, baza = 200;
        cin >> n;
        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                cin >> board[i][j];
            }
        }
        vector<Farmer> whr; 
        printf("R FARMER\n");
        baza -= 100;
        printf("M %d %d %d %d\n", 0, 0, 1, 0);
        printf("=\n");

        int a1 = 1, b1 = 0, dir = 1;
        Farmer f1 = {1, 0, (board[1][0] < 10) ? board[1][0] : 10};
        board[1][0] = (board[1][0] <= 10) ? 0 : board[1][0] - 10;    
        rounds += 1;
        int required = (n-2)*100; // jeden juz jest, na drugiego jest hajs
        while(f1.pts < required) {
            dir = updateFarmerAndBoard(f1, a1, b1, n, dir);
            printf("=\n");
            if(dir == -2) break;
            rounds++;
        }
        whr.push_back(f1);
        if(dir == -2) {
            printf("===\n");
            continue;
        }
        while(a1 > 0) { // wycofanie
            printf("M %d %d %d %d\n=\n", a1, b1, a1-1, b1);
            a1--;
            if(!(a1==0 && b1==0)) {
                whr[0].pts += ((board[a1][b1] < 10) ? board[a1][b1] : 10);
                board[a1][b1] = (board[a1][b1] <= 10) ? 0 : board[a1][b1] - 10;   
            }
            printf("=\n");
            rounds++;
        }   
        while(b1 > 0) {
            printf("M %d %d %d %d\n=\n", a1, b1, a1, b1-1);
            b1--;
            if(!(a1==0 && b1==0)) {
                whr[0].pts += ((board[a1][b1] < 10) ? board[a1][b1] : 10);
                board[a1][b1] = (board[a1][b1] <= 10) ? 0 : board[a1][b1] - 10;   
            }
            printf("=\n");
            rounds++;
        }
        whr[0].r1 = 0;
        whr[0].c1 = 0;
        
        baza += whr[0].pts;
        whr[0].pts = 0;
        printf("=\n");
        printf("M %d %d %d %d\n=\n", 0, 0, 1, 0);       
        
        // przyporzadkowanie kazdemu kolumny
        for(int i=0; i<n-1; i++) {
            baza -= 100;
            Farmer f2 = {0, 0, 0};
            whr.push_back(f2);
            printf("R FARMER\n");
            for(int k=1; k<whr.size(); k++) {
                moveAndUpdate(whr[k], whr[k].r1, whr[k].c1 + 1);
            }
            printf("=\n");
        }
        // w doool
        
        while(true) {
            int ends = 0;
            for(int i=0; i<whr.size(); i++) {
                ends += moveIfPossibleAndUpdate(whr[i], n);
            }
            printf("=\n");
            rounds += 1;
            if(ends == n) break;
        }
        // do tyluuu        
        while(true) {
            int ends = 0;
            for(int i=0; i<whr.size(); i++) {
                if(whr[i].r1 > 0) {
                    printf("M %d %d %d %d\n", whr[i].r1, whr[i].c1, whr[i].r1 - 1, whr[i].c1);
                    whr[i].r1 -= 1;
                } else {
                    ends += 1;
                }
            }
            printf("=\n");
            rounds += 1;
            if(ends == n) break;
        }
        
        printf("M %d %d %d %d\n", 0, 0, 1, 0);
        printf("=\n");

        // i wszystkie przesuwamy do (0, 0)
        for(int j=whr.size() - 1; j >= 1; j--) {
            while(whr[j].c1 > 0) {
                printf("M %d %d %d %d\n", whr[j].r1, whr[j].c1, whr[j].r1, whr[j].c1 - 1);
                whr[j].c1 -= 1;                    
                printf("=\n");
            }
            while(whr[j].c1 < j) {
                printf("M %d %d %d %d\n", whr[j].r1, whr[j].c1, whr[j].r1, whr[j].c1 + 1);
                whr[j].c1 += 1;                    
                printf("=\n");
            }
            printf("M %d %d %d %d\n", 0, j, 1, j);
            printf("=\n");
        }
        printf("===\n");
    }
    return 0;
}