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

#include <iostream>
#include <stack>
using namespace std;

int w, h;
int t[20][20];
int sx, sy;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

bool goalFlg;

class Info {
public:
  int map[20][20];
  int cost, x, y;
};

bool move(int d, Info *info) {
  int nx = info->x + dx[d];
  int ny = info->y + dy[d];
  if (nx >= 0 && nx < w && ny >= 0 && ny < h && info->map[ny][nx] == 1)
    return false;

  while (true) {
    nx = info->x + dx[d];
    ny = info->y + dy[d];

    if (nx < 0 || nx >= w || ny < 0 || ny >= h)
      return false;
    else if (info->map[ny][nx] == 1) {
      info->map[ny][nx] = 0;
      return true;
    } else if (info->map[ny][nx] == 3) {
      goalFlg = true;
      info->x = nx;
      info->y = ny;
      return true;
    }
    info->x = nx;
    info->y = ny;
  }
}

int bfs(void) {
  stack<Info> s;
  Info info, ninfo;
  int ans = 999999999;

  goalFlg = false;

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      info.map[i][j] = t[i][j];
  info.cost = 0, info.x = sx, info.y = sy;
  s.push(info);

  while (!s.empty()) {
    info = s.top();
    s.pop();
    int ncost = info.cost + 1;
    if (ncost > 10)
      continue;

    for (int i = 0; i < 4; i++) {
      ninfo = info;
      if (move(i, &ninfo) && !goalFlg) {
        ninfo.cost = ncost;
        s.push(ninfo);
      }
      if (goalFlg) {
        goalFlg = false;
        if (ans > ncost)
          ans = ncost;
        continue;
      }
    }
  }
  if (ans == 999999999)
    return -1;
  else
    return ans;
}

int main(void) {
  while (cin >> w >> h && (w || h)) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> t[i][j];
        if (t[i][j] == 2)
          sx = j, sy = i;
      }
    }
    cout << bfs() << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;

int w, h;
int t[20][20];
int sx, sy;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

bool goalFlg;

class Info {
public:
  int map[20][20];
  int cost, x, y;
};

bool move(int d, Info *info) {
  int nx = info->x + dx[d];
  int ny = info->y + dy[d];
  if (nx >= 0 && nx < w && ny >= 0 && ny < h && info->map[ny][nx] == 1)
    return false;

  while (true) {
    nx = info->x + dx[d];
    ny = info->y + dy[d];

    if (nx < 0 || nx >= w || ny < 0 || ny >= h)
      return false;
    else if (info->map[ny][nx] == 1) {
      info->map[ny][nx] = 0;
      return true;
    } else if (info->map[ny][nx] == 3) {
      goalFlg = true;
      info->x = nx;
      info->y = ny;
      return true;
    }
    info->x = nx;
    info->y = ny;
  }
}

int bfs(void) {
  queue<Info> q;
  Info info, ninfo;

  goalFlg = false;

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      info.map[i][j] = t[i][j];
  info.cost = 0, info.x = sx, info.y = sy;
  q.push(info);

  while (!q.empty()) {
    info = q.front();
    q.pop();
    int ncost = info.cost + 1;
    if (ncost > 10)
      continue;

    for (int i = 0; i < 4; i++) {
      ninfo = info;
      if (move(i, &ninfo) && !goalFlg) {
        ninfo.cost = ncost;
        q.push(ninfo);
      }
      if (goalFlg)
        return ncost;
    }
  }

  return -1;
}

int main(void) {
  while (cin >> w >> h && (w || h)) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> t[i][j];
        if (t[i][j] == 2)
          sx = j, sy = i;
      }
    }
    cout << bfs() << endl;
  }

  return 0;
}