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

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

char field[101][101];
bool passed[101][101];

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

int h, w;
// ×ÚÇÀWÌXg
set<pair<int, int>> walls;
set<pair<int, int>> spList;

bool bfs(int sx, int sy, bool w) {
  // Ü¸ÊÉbfs
  queue<pair<int, int>> *prv = new queue<pair<int, int>>();
  queue<pair<int, int>> *nxt = new queue<pair<int, int>>();

  prv->push(make_pair(sy, sx));
  bool fin = false;
  passed[sy][sx] = true;
  while (prv->size()) {
    while (prv->size()) {
      pair<int, int> p = prv->front();
      prv->pop();
      // ÇÉBµ½çAI¹
      if (p.first == 0 || p.first == h - 1 || p.second == 0 ||
          p.second == w - 1) {
        return true;
      }
      // lûüÉÚ®
      for (int i = 0; i < 4; i++) {
        int nx = p.second + dx[i];
        int ny = p.first + dy[i];
        if (!passed[ny][nx]) {
          if (w) {
            if (field[ny][nx] == '.') {
              nxt->push(make_pair(ny, nx));
              passed[ny][nx] = true;
            } else if (field[ny][nx] == '#') {
              walls.insert(make_pair(ny, nx));
            }
          } else {
            if (field[ny][nx] == '#') {
              nxt->push(make_pair(ny, nx));
              passed[ny][nx] = true;
            } else if (field[ny][nx] = '.') {
              spList.insert(make_pair(ny, nx));
            }
          }
        }
      }
    }
    swap(prv, nxt);
  }
  return false;
}

int main() {

  // ÇÉÕË·éÜÅbfsðJè©¦·
  while (cin >> w >> h && !(w == 0 && h == 0)) {
    walls.clear();
    spList.clear();
    int sx, sy;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> field[i][j];
        if (field[i][j] == '&')
          sx = j, sy = i;
      }
      fill(passed[i], passed[i] + w, false);
    }
    int cnt = 0;
    bool fin = false;
    spList.insert(make_pair(sy, sx));
    while (1) {
      // ÇÀWÌXgªèÉüéÌÅA»ÌÇÀW©çBÅ«éêðTõ
      for (set<pair<int, int>>::iterator it = spList.begin();
           it != spList.end(); it++) {
        int cx = it->second;
        int cy = it->first;
        if (bfs(cx, cy, true)) {
          fin = true;
          break;
        }
      }
      if (fin)
        break;
      spList.clear();
      cnt++;
      // ÇðÂ½ÁÄ¢¯éªðTõµA»ÌãÇÉ×Ú·éÀWðÛµÄ¨«A[v
      for (set<pair<int, int>>::iterator it = walls.begin(); it != walls.end();
           it++) {
        int cx = it->second;
        int cy = it->first;
        if (bfs(cx, cy, false)) {
          fin = true;
          break;
        }
      }
      if (fin)
        break;
      walls.clear();
    }
    cout << cnt << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

char field[101][101];
bool passed[101][101];

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

int h, w;
// ×ÚÇÀWÌXg
vector<pair<int, int>> walls;
vector<pair<int, int>> spList;

bool bfs(int sx, int sy, bool w) {
  // Ü¸ÊÉbfs
  queue<pair<int, int>> *prv = new queue<pair<int, int>>();
  queue<pair<int, int>> *nxt = new queue<pair<int, int>>();

  prv->push(make_pair(sy, sx));
  bool fin = false;
  passed[sy][sx] = true;
  while (prv->size()) {
    while (prv->size()) {
      pair<int, int> p = prv->front();
      prv->pop();
      // ÇÉBµ½çAI¹
      if (p.first == 0 || p.first == h - 1 || p.second == 0 ||
          p.second == w - 1) {
        return true;
      }
      // lûüÉÚ®
      for (int i = 0; i < 4; i++) {
        int nx = p.second + dx[i];
        int ny = p.first + dy[i];
        if (!passed[ny][nx]) {
          if (w) {
            if (field[ny][nx] == '.') {
              nxt->push(make_pair(ny, nx));
              passed[ny][nx] = true;
            } else if (field[ny][nx] == '#') {
              walls.push_back(make_pair(ny, nx));
            }
          } else {
            if (field[ny][nx] == '#') {
              nxt->push(make_pair(ny, nx));
              passed[ny][nx] = true;
            } else if (field[ny][nx] = '.') {
              spList.push_back(make_pair(ny, nx));
            }
          }
        }
      }
    }
    swap(prv, nxt);
  }
  return false;
}

int main() {

  // ÇÉÕË·éÜÅbfsðJè©¦·
  while (cin >> w >> h && !(w == 0 && h == 0)) {
    walls.clear();
    spList.clear();
    int sx, sy;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> field[i][j];
        if (field[i][j] == '&')
          sx = j, sy = i;
      }
      fill(passed[i], passed[i] + w, false);
    }
    int cnt = 0;
    bool fin = false;
    spList.push_back(make_pair(sy, sx));
    while (1) {
      // ÇÀWÌXgªèÉüéÌÅA»ÌÇÀW©çBÅ«éêðTõ
      for (int i = 0; i < spList.size(); i++) {
        int cx = spList[i].second;
        int cy = spList[i].first;
        if (bfs(cx, cy, true)) {
          fin = true;
          break;
        }
      }
      if (fin)
        break;
      spList.clear();
      cnt++;
      // ÇðÂ½ÁÄ¢¯éªðTõµA»ÌãÇÉ×Ú·éÀWðÛµÄ¨«A[v
      for (int i = 0; i < walls.size(); i++) {
        int cx = walls[i].second;
        int cy = walls[i].first;
        if (bfs(cx, cy, false)) {
          fin = true;
          break;
        }
      }
      if (fin)
        break;
      walls.clear();
    }
    cout << cnt << endl;
  }

  return 0;
}