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
#include <cstdio>
#include <vector>
#include <algorithm>

struct Pos{
    int x, y;
    Pos(){
        this->x = 0;
        this->y = 0;
    }
    Pos(int X,int Y):x(X),y(Y){}
    bool operator==(const Pos & pos) const{
        return this->x==pos.x and this->y==pos.y;
    }
};

int tab_t[25][25];

int &tab(int a,int b){
    return tab_t[a+2][b+2];
}
Pos findNearestGoodResource( Pos myPos, int n){
    int bestmoves = (int)1e9;
    Pos best = Pos(0,0);
    for(int x=0; x<n; x++){
        for(int y=0; y<n; y++){
            if(tab(x,y)>0){
                int distance = abs(x-myPos.x)+abs(y-myPos.y);
                if(distance < bestmoves){
                    bestmoves = distance;
                    best = Pos(x,y);
                }
            }
        }
    }
    return best;
}

void przybliz(Pos& myPos, Pos despos){
    if(myPos==despos) return;
    Pos tmp = myPos;
    if(myPos.x!=despos.x){//przybliz x
        if(despos.x>myPos.x){
            tmp.x++;
        }
        else{
            tmp.x--;
        }
    }
    else{//przybliz y
        if(despos.y>myPos.y){
            tmp.y++;
        }
        else{
            tmp.y--;
        }
    }

    printf("M %d %d %d %d\n",myPos.x,myPos.y,tmp.x,tmp.y);
    myPos = tmp;
}

int main() {

    int T,k;
    scanf("%d%d",&T,&k);
    while(T--){

        int n;
        scanf("%d",&n);
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                scanf("%d",&tab(i,j));
            }
        }
        printf("R FARMER\n");
        Pos mypos;
        przybliz(mypos,findNearestGoodResource(mypos, n));
        tab(mypos.x,mypos.y)-=10;
        printf("=\n");

        while(true){
            Pos best = findNearestGoodResource(mypos, n);
            //printf("best.x: %d, y: %d\n",best.x,best.y);
            przybliz(mypos,best);
            tab(mypos.x,mypos.y)-=10;
            printf("=\n");
            if(mypos==Pos(0,0)){
                printf("===\n");
                break;
            }
        }
    }

    return 0;
}