Problem p01340 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
#include <string>
using namespace std;
struct Kaeru {
  char b[10][10];
  int px, py, dir;
  string F;
};
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
queue<Kaeru> Q;
int H, W;
char x[10][10];
int cx, cy, cd = 0;
char CC[5] = "URDL";
int main() {
  cin >> H >> W;
  while (!Q.empty())
    Q.pop();
  int CNT = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> x[i][j];
      if (x[i][j] == 'o')
        CNT++;
      if (x[i][j] == 'U') {
        cx = i;
        cy = j;
        cd = 0;
        x[i][j] = '.';
      }
      if (x[i][j] == 'R') {
        cx = i;
        cy = j;
        cd = 1;
        x[i][j] = '.';
      }
      if (x[i][j] == 'D') {
        cx = i;
        cy = j;
        cd = 2;
        x[i][j] = '.';
      }
      if (x[i][j] == 'L') {
        cx = i;
        cy = j;
        cd = 3;
        x[i][j] = '.';
      }
    }
  }
  Kaeru D;
  for (int i = 0; i < 100; i++)
    D.b[i / 10][i % 10] = x[i / 10][i % 10];
  D.px = cx;
  D.py = cy;
  D.dir = cd;
  D.F = "";
  Q.push(D);
  while (!Q.empty()) {
    Kaeru a1 = Q.front();
    Q.pop();
    Kaeru a2 = a1;
    if (a2.F.size() == CNT) {
      cout << a2.F << endl;
      break;
    }

    for (int i = 0; i < 4; i++) {
      a2 = a1;
      char y[10][10];
      for (int j = 0; j < 100; j++)
        y[j / 10][j % 10] = a2.b[j / 10][j % 10];
      if (i == (a1.dir + 2) % 4)
        continue;
      int ex = a2.px, ey = a2.py;
      while (true) {
        ex += dx[i];
        ey += dy[i];
        if (ex < 0 || ey < 0 || ex >= H || ey >= W)
          break;
        if (y[ex][ey] == 'o') {
          y[ex][ey] = '.';
          for (int j = 0; j < 100; j++)
            a2.b[j / 10][j % 10] = y[j / 10][j % 10];
          a2.px = ex;
          a2.py = ey;
          a2.dir = i;
          a2.F += CC[i];
          Q.push(a2);
          break;
        }
      }
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
using namespace std;
struct Kaeru {
  char b[10][10];
  int px, py, dir;
  string F;
};
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
queue<Kaeru> Q;
int H, W;
char x[10][10];
int cx, cy, cd = 0;
char CC[5] = "URDL";
int main() {
  cin >> H >> W;
  while (!Q.empty())
    Q.pop();
  int CNT = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> x[i][j];
      if (x[i][j] == 'o')
        CNT++;
      if (x[i][j] == 'U') {
        cx = i;
        cy = j;
        cd = 0;
        x[i][j] = '.';
      }
      if (x[i][j] == 'R') {
        cx = i;
        cy = j;
        cd = 1;
        x[i][j] = '.';
      }
      if (x[i][j] == 'D') {
        cx = i;
        cy = j;
        cd = 2;
        x[i][j] = '.';
      }
      if (x[i][j] == 'L') {
        cx = i;
        cy = j;
        cd = 3;
        x[i][j] = '.';
      }
    }
  }
  Kaeru D;
  for (int i = 0; i < 100; i++)
    D.b[i / 10][i % 10] = x[i / 10][i % 10];
  D.px = cx;
  D.py = cy;
  D.dir = cd;
  D.F = "";
  Q.push(D);
  while (!Q.empty()) {
    Kaeru a1 = Q.front();
    Q.pop();
    Kaeru a2 = a1;
    if (a2.F.size() == CNT) {
      cout << a2.F << endl;
      break;
    }

    for (int i = 0; i < 4; i++) {
      a2 = a1;
      char y[10][10];
      for (int j = 0; j < 100; j++)
        y[j / 10][j % 10] = a1.b[j / 10][j % 10];
      if (i == (a1.dir + 2) % 4)
        continue;
      int ex = a1.px, ey = a1.py;
      while (true) {
        ex += dx[i];
        ey += dy[i];
        if (ex < 0 || ey < 0 || ex >= H || ey >= W)
          break;
        if (y[ex][ey] == 'o') {
          y[ex][ey] = '.';
          for (int j = 0; j < 100; j++)
            a2.b[j / 10][j % 10] = y[j / 10][j % 10];
          a2.px = ex;
          a2.py = ey;
          a2.dir = i;
          a2.F += CC[i];
          Q.push(a2);
        }
      }
    }
  }
  return 0;
}