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

#define DBG(x)

const int MAXN = 25;

int T,k;
int n;

int field[MAXN][MAXN];
bool map[MAXN][MAXN];
int gold = 200;
int goldStored;
int allGold;
bool end = false;
int K;

int level;
int ll;

struct point
{
    int x,y;

    point operator+(const point& p) const
    {
        return {x+p.x, y+p.y};
    }
};

point dir[] = { {0,1}, {1,0}, {-1,0}, {0, -1} };

point UP = {0, -1};
point DOWN = {0, 1};
point LEFT = {-1, 0};
point RIGHT = {1, 0};

struct Unit
{
    point p;
    int gold;
    int rand;
    int id;
    int id2;
};
std::deque<Unit> F;

int MINERS = 4;
int SUBFIELDS;

void dbg()
{
    for (int i=0;i<n;++i)
    {
        for (int j=0;j<n;++j)
        {
            std::clog << "\t" << field[i][j];
            if (map[i][j]) std::clog << 'F';
        }
        std::clog << std::endl;
    }
}

bool move(Unit& u, point dir)
{
    point np = u.p + dir;
    if (np.x < 0 || np.x >= n || np.y < 0 || np.y >= n || map[np.y][np.x] == true) return false;

    map[u.p.y][u.p.x] = false;
    map[np.y][np.x] = true;

    std::cout << "M " << u.p.y << " " << u.p.x << " " << np.y << " " << np.x << std::endl;
    u.p = np;
    return true;
}

void update(Unit& u)
{
    if (u.rand)
    {
        --u.rand;
        move(u, dir[rand()%4]);
    }
    // else if (u.gold >= 100)
    // {
    //     if (move(u, LEFT)) {}
    //     else if (u.p.y == 0 && move(u, DOWN)) {}
    //     else if (u.p.x == 0 && move(u, UP)) {}
    // }
    else
    {
        // if (field[u.p.y][u.p.x] == 0)
        // {
        // if (u.p.y == n-1 && u.p.x == 1 && move(u, LEFT)) {}
        // else if (u.p.y > 0 && u.p.x == 0 && move(u, UP)) {}
        // else if (u.p.y % 2 == 0 && u.p.x < n-1 && move(u, RIGHT)) {}
        // else if (u.p.y % 2 == 0 && u.p.x == n-1 && move(u, DOWN)) {}
        // else if (u.p.y % 2 == 1 && u.p.x > 1 && move(u, LEFT)) {}
        // else if (u.p.y % 2 == 1 && u.p.x == 1 && move(u, DOWN)) {}
        // }

        if (ll > 1 && u.p.x > 0 && u.p.y / SUBFIELDS == MINERS-u.id2 && field[u.p.y][u.p.x] > 0) {DBG(std::clog << "W";)}
        else if ( (u.p.y == n-1 && u.p.x > 0) && move(u, LEFT)) {DBG(std::clog << "L";)}
        else if (u.p.y > 0 && u.p.x == 0 && move(u, UP)) {DBG(std::clog << "U";)}
        else if (u.p.y == 0 && u.p.x < u.id && move(u, RIGHT)) {DBG(std::clog << "R";)}
        else if (u.p.x == u.id && move(u, DOWN)) {DBG(std::clog << "D";)}
        else {DBG(std::clog << "::" << u.id << ")";) }
    }

    int diff = std::min(field[u.p.y][u.p.x], 10);
    u.gold += diff;
    field[u.p.y][u.p.x] -= diff;

    if (u.p.x == 0 && u.p.y == 0)
    {
        gold += u.gold;
        goldStored += u.gold;
        u.gold = 0;
    }
}

void solve()
{
    std::cin >> n;
    for (int y=0;y<n;++y)
        for (int x=0;x<n;++x)
            std::cin >> field[y][x];

    allGold = 0;
    goldStored = 0;
    gold = 200;
    for (int y=0;y<n;++y)
        for (int x=0;x<n;++x)
            allGold += field[y][x];

    for (int y=0;y<n;++y)
        for (int x=0;x<n;++x)
            map[y][x] = false;
    F.clear();

    SUBFIELDS = (n+MINERS-1)/MINERS;

    level = 1;
    ll = 1;
    K = 0;
    while (allGold > goldStored && K < 3000)
    {
        //if (K > 100) level = n-1;
        ++K;
        if (gold >= 100 && ll <= MINERS && map[0][0] == false)
        {
            gold -= 100;
            F.push_back({ {0,0},0,0,level,ll});
            std::cout << "R FARMER\n";
            map[0][0] = true;

            DBG(std::clog << "level = " << level << "; ll = " << ll << std::endl);
            ++level;
            if (level >= n)
            {
                level = 1;
                ++ll;
            }
        }
        for (int i=0;i<F.size();++i)
        {
            update(F[i]);
        }

        std::cout << "=" << std::endl;
        DBG(std::clog << std::endl << " GOLD = " << gold << " / TURA = " << K << std::endl);
        DBG(dbg());
    }
    std::cout << "===" << std::endl;
}

int main()
{
    std::ios_base::sync_with_stdio(0);
    std::cin >> T >> k;
    while (T--) solve();
    return 0;
}