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

#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
struct wolf {
  char m[22][22];
  int row;
  int col;
};
int c[22][22];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
  int a, b;
  while (scanf("%d%d", &b, &a), a) {
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        scanf("%d", &c[i][j]);
      }
    }
    int ret = 999999;
    queue<pair<int, wolf>> Q;
    wolf s;
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        if (c[i][j] == 2) {
          s.row = i;
          s.col = j;
          s.m[i][j] = 0;
        } else
          s.m[i][j] = c[i][j];
      }
    }
    Q.push(make_pair(0, s));
    while (Q.size()) {
      if (ret < 99999)
        break;
      int cost = Q.front().first;
      wolf now = Q.front().second;
      Q.pop();
      if (cost == 10)
        continue;
      for (int i = 0; i < 4; i++) {
        int nr = now.row;
        int nc = now.col;
        int t = 0;
        while (1) {
          nr += dx[i];
          nc += dy[i];
          t++;
          if (nr < 0 || nr >= a || nc < 0 || nc >= b) {
            break;
          }
          if (now.m[nr][nc] == 3) {
            ret = min(ret, cost + 1);
            break;
          }
          if (now.m[nr][nc] == 1) {
            if (t > 1) {
              wolf to;
              to.row = nr - dx[i];
              to.col = nc - dy[i];
              for (int j = 0; j < a; j++)
                for (int k = 0; k < b; k++)
                  to.m[j][k] = now.m[j][k];
              to.m[nr][nc] = 0;
              Q.push(make_pair(cost + 1, to));
            }
            break;
          }
        }
      }
    }
    if (ret > 99999)
      printf("-1\\n");
    else
      printf("%d\\n", ret);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
struct wolf {
  int m[22][22];
  int row;
  int col;
};
int c[22][22];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
  int a, b;
  while (scanf("%d%d", &b, &a), a) {
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        scanf("%d", &c[i][j]);
      }
    }
    int ret = 999999;
    queue<pair<int, wolf>> Q;
    wolf s;
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        if (c[i][j] == 2) {
          s.row = i;
          s.col = j;
          s.m[i][j] = 0;
        } else
          s.m[i][j] = c[i][j];
      }
    }
    Q.push(make_pair(0, s));
    while (Q.size()) {
      int cost = Q.front().first;
      wolf now = Q.front().second;
      Q.pop();
      if (cost == 10)
        continue;
      for (int i = 0; i < 4; i++) {
        int nr = now.row;
        int nc = now.col;
        int t = 0;
        while (1) {
          nr += dx[i];
          nc += dy[i];
          t++;
          if (nr < 0 || nr >= a || nc < 0 || nc >= b) {
            break;
          }
          if (now.m[nr][nc] == 3) {
            ret = min(ret, cost + 1);
            break;
          }
          if (now.m[nr][nc] == 1) {
            if (t > 1) {
              wolf to;
              to.row = nr - dx[i];
              to.col = nc - dy[i];
              for (int j = 0; j < a; j++)
                for (int k = 0; k < b; k++)
                  to.m[j][k] = now.m[j][k];
              to.m[nr][nc] = 0;
              Q.push(make_pair(cost + 1, to));
            }
            break;
          }
        }
      }
    }
    if (ret > 99999)
      printf("-1\\n");
    else
      printf("%d\\n", ret);
  }
}