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

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int INF = 100000000;
const int MAX_N = 100;
const int MAX_M = 100;
const int dy[4] = {-1, 0, 1, 0};
const int dx[4] = {0, 1, 0, -1};

char maze[MAX_N][MAX_M + 1];
int N, M;
int sy, sx;
int cost[MAX_N][MAX_M];

int bfs(void) {
  rep(i, N) rep(j, M) cost[i][j] = INF;
  queue<P> que;
  que.push(P(sy, sx));
  cost[sy][sx] = 0;
  while (!que.empty()) {
    P cur = que.front();
    que.pop();
    int cy = cur.first;
    int cx = cur.second;
    int cc = cost[cy][cx];
    rep(k, 4) {
      int ny = cy + dy[k];
      int nx = cx + dx[k];
      if (ny < 0 || ny >= N || nx < 0 || nx >= M)
        continue;
      int next_cost =
          cost[cy][cx] + ((maze[ny][nx] == '#' && maze[cy][cx] != '#') ? 1 : 0);
      if (next_cost >= cost[ny][nx])
        continue;
      cost[ny][nx] = next_cost;
      que.push(P(ny, nx));
    } // end rep
  }   // end rep

  int res = INF;
  rep(i, N) res = min(res, cost[i][0]);
  rep(i, N) res = min(res, cost[i][M - 1]);
  rep(j, M) res = min(res, cost[0][j]);
  rep(j, M) res = min(res, cost[N - 1][j]);

  return res;
}

int main() {
  ios_base::sync_with_stdio(0);
  while (cin >> M >> N, M) {
    memset(maze, 0, sizeof(maze));
    memset(cost, 0, sizeof(cost));
    rep(i, N) rep(j, M) cin >> maze[i][j];
    rep(i, N) rep(j, M) if (maze[i][j] == '&') { sy = i, sx = j; }
    int res = bfs();
    cout << res << endl;
  } // end while

  return 0;
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;

char M[105][105];
bool v[105][105];

int bfs(int m, int n, P sp) {
  const int dir[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
  priority_queue<PP, vector<PP>, greater<PP>> que;
  que.push(PP(0, sp));

  while (!que.empty()) {
    PP curr = que.top();
    que.pop();
    int cost = curr.first;
    int cr = curr.second.first, cc = curr.second.second;
    v[cr][cc] = true;
    rep(k, 4) {
      int nr = cr + dir[k][0], nc = cc + dir[k][1];
      if (v[nr][nc])
        continue;
      if (nr < 0 || nr > m + 1 || nc < 0 || nc > n + 1)
        return cost;
      if (M[cr][cc] == '#' && M[nr][nc] == '.') {
        que.push(PP(cost + 1, P(nr, nc)));
      } else {
        que.push(PP(cost, P(nr, nc)));
      } // end if
    }   // end rep
  }     // end while

  return -1;
}
void init_map(int m, int n) { rep(i, m + 2) rep(j, n + 2) M[i][j] = '.'; }

int main() {
  int n, m; // n: ¼Ì m: ìkÌ

  while (cin >> n >> m && n && m) {
    P sp;
    init_map(m, n);
    memset(v, false, sizeof(v));
    for (int i = 1; i <= m; i++) {
      for (int j = 1; j <= n; j++) {
        cin >> M[i][j];
        if (M[i][j] == '&') {
          sp.first = i, sp.second = j;
        } // end if
      }   // end for
    }     // end for
    int res = bfs(m, n, sp);
    cout << res << endl;
  } // end loop

  return 0;
}