Language: C++

#include <bits/stdc++.h>

using namespace std;

#define SZ(x) (int)(x.size())

using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const ll MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;

template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}

template<typename T1, typename T2>
ostream& operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}

struct XorShift
{
   uint32_t w, x, y, z;
   XorShift(uint32_t seed = time(nullptr))
   {
       w = seed;
       x = w << 13;
       y = (w >> 9) ^ (x << 6);
       z = y >> 7;
   }
 
   uint32_t rand()
   {
       uint32_t t = x ^ (x << 11);
       x = y;
       y = z;
       z = w;
       return w = (w ^ (w >> 19) ^ (t ^ (t >> 8)));
   }
 
   // [min,max] の整数値乱数
   int randInt(int min = 0, int max = 0x7FFFFFFF)
   {
       return rand() % (max - min + 1) + min;
   }
 
   // [min,max] の浮動小数点乱数
   double randDouble(double min = 0, double max = 1)
   {
       return (double)(rand() % 0xFFFF) / 0xFFFF * (max - min) + min;
   }
 
   // 変数をデフォルト値に設定する
   void SetDefault()
   {
       w = 123456789;
       x = 362436069;
       y = 521288629;
       z = 88675123;
   }
};
 
XorShift randgen;

int n, m;
vvi a;
string str = "URDL";
vi dr = {-1, 0, 1, 0};
vi dc = {0, 1, 0, -1};
const int SIZE = 1;


//状態
struct STATE {
  vector<vector<pair<P, int>>> man;
  vvi col;
  int score;
  map<pair<P, int>, bool> sel;

  STATE() {
    man.resize(4);
    col.resize(n, vi(n, -1));
    col[0][0] = 0;
    col[0][n-1] = 1;
    col[n-1][0] = 2;
    col[n-1][n-1] = 3;
    score = 4;
  }
};

bool operator< (const STATE &s1, const STATE &s2) {
  return s1.score > s2.score;
}

STATE now;



// 状態遷移
void update (int cnt) {
  cout << cnt << endl;
  STATE nxt = now;


  for(int i=0;i<n;++i) {
    for(int j=0;j<n;++j) {
      if(now.col[i][j] != cnt%4) continue;
      if(SZ(now.man[cnt%4]) == m/4) continue;
      for(int k=0;k<4;++k) {
        if(sel[{{i, j}, k}]) continue;
        int pscore = now.score;
        vector<int> prev(6);
        for(int l=1;l<=5;++l) {
          int nr = i+l*dr[k], nc = j+l*dc[k];
          //cout << i << ":" << j << " " << nr << ":" << nc << endl;
          if(!(0 <= nr && nr < n && 0 <= nc && nc < n)) break;
          if(now.col[nr][nc] == a[nr][nc] && a[nr][nc] != cnt%4) now.score--;
          if(now.col[nr][nc] != a[nr][nc] && a[nr][nc] == cnt%4) now.score++;
          prev[l] = now.col[nr][nc];
          now.col[nr][nc] = cnt%4;
        }
        now.man[cnt%4].push_back({{i, j}, k});
        if(prev.top().score < now.score) {
          sel[{{i, j}, k}] = true;
          
          sel[{{i, j}, k}] = false;
        } else if(newpq.top().score == now.score) {
          double prob = randgen.randDouble();
          if(prob > 0.5) {
            newpq.pop();
            sel[{{i, j}, k}] = true;
            newpq.push(now);
            sel[{{i, j}, k}] = false;
          }
        }

        for(int l=1;l<=5;++l) {
          int nr = i+l*dr[k], nc = j+l*dc[k];
          //cout << i << ":" << j << " " << nr << ":" << nc << endl;
          if(!(0 <= nr && nr < n && 0 <= nc && nc < n)) break;
          if(prev[l] == a[nr][nc] && a[nr][nc] != cnt%4) now.score++;
          if(prev[l] != a[nr][nc] && a[nr][nc] == cnt%4) now.score--;
          now.col[nr][nc] = prev[l];
        }
        now.man[cnt%4].pop_back();
      }
    }
  }
}

// 状態のスコア計算
int score (STATE& state) {
  int res = 0;
  for(int i=0;i<n;++i) {
    for(int j=0;j<n;++j) {
      res += state.col[i][j] == a[i][j];
    }
  }
  return res;
}

int nowscore = 0;


int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  cin >> n >> m;
  a.resize(n, vi(n));
  for(int i=0;i<n;++i) {
    for(int j=0;j<n;++j) {
      cin >> a[i][j];
    }
  }

  now = STATE();

  for(int i=0;i<m;++i) {
    update(i);
  }
  //cout << "A" << endl;

  //cout << now.score << endl;
  for(int i=0;i<m;++i) {
    int col = i%4, idx = i/4;
    if(idx < SZ(now.man[col])) {
      cout << now.man[col][idx].first.first << " "
          << now.man[col][idx].first.second << " "
          << str[now.man[col][idx].second] << endl;
    } else {
      cout << -1 << endl;
    }
  }

}


Compiler output:
a.cc: In function 'void update(int)':
a.cc:123:12: error: 'sel' was not declared in this scope
  123 |         if(sel[{{i, j}, k}]) continue;
      |            ^~~
a.cc:136:17: error: 'class std::vector<int>' has no member named 'top'
  136 |         if(prev.top().score < now.score) {
      |                 ^~~
a.cc:137:11: error: 'sel' was not declared in this scope
  137 |           sel[{{i, j}, k}] = true;
      |           ^~~
a.cc:140:19: error: 'newpq' was not declared in this scope
  140 |         } else if(newpq.top().score == now.score) {
      |                   ^~~~~
a.cc:144:13: error: 'sel' was not declared in this scope
  144 |             sel[{{i, j}, k}] = true;
      |             ^~~