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
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

struct pkt
{
    int x, y;
};

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int PZZ(vector< vector<int> >& a)
{
    int il = 0;
    for (int i = 0; i < a.size(); ++i)
        for (int j = 0; j < a[i].size(); ++j)
            if (a[i][j] > 0)
                ++il;
    return il;
}

int NZ(vector< vector<int> >& a, pkt w)
{
    int odl = 1000;
    for (int i = 0; i < a.size(); ++i)
        for (int j = 0; j < a[i].size(); ++j)
            if (a[i][j] > 0)
                odl = min(odl, (int) abs(w.x - i) + (int) abs(w.y - j));
    return odl;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int T, K;
    cin >> T >> K;
    for (int i = 0; i < T; ++i)
    {
        int n;
        cin >> n;
        vector< vector<int> > a(n, vector<int>(n));
        for (int j = 0; j < n; ++j)
            for (int k = 0; k < n; ++k)
                cin >> a[j][k];
        cout << "R FARMER" << endl;
        pkt akt = {0, 0};
        while (PZZ(a) >= 1)
        {
            int odl = 1000, kier;
            for (int j = 0; j < 4; ++j)
            {
                pkt nowy = {akt.x + dx[j], akt.y + dy[j]};
                if (nowy.x >= 0 && nowy.x < n && nowy.y >= 0 & nowy.y < n)
                {
                    if (NZ(a, nowy) < odl)
                    {
                        odl = NZ(a, nowy);
                        kier = j;
                    }
                }
            }
            pkt nowy = {akt.x + dx[kier], akt.y + dy[kier]};
            cout << "M " << akt.x << " " << akt.y << " " << nowy.x << " " << nowy.y << endl;
            cout << "=" << endl;
            akt = nowy;
            a[akt.x][akt.y] = max(a[akt.x][akt.y] - 10, 0);
        }
        while (akt.x > 0)
        {
            cout << "M " << akt.x << " " << akt.y << " " << akt.x - 1 << " " << akt.y << endl;
            cout << "=" << endl;
            --akt.x;
        }
        while (akt.y > 0)
        {
            cout << "M " << akt.x << " " << akt.y << " " << akt.x << " " << akt.y - 1 << endl;
            cout << "=" << endl;
            --akt.y;
        }
        cout << "===" << endl;
    }
    return 0;
}