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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> State;
const int INF = 1e+8;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};

// デバッグ出力
void debug(const vector<string> &m) {
  cout << "[debug]" << endl;
  for (int y = 0; y < m.size(); y++)
    cout << m[y] << endl;
}

// 幅と高さ
int w, h;

// ゴールの位置を調べる
P search_goal(vector<string> &m) {
  int gx, gy;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (m[y][x] == '&') {
        m[y][x] = '.';
        gx = x;
        gy = y;
        break;
      }
    }
  }
  return P(gx, gy);
}

int solve(vector<string> &m) {
  // n := ノード数
  int n = w * h;
  // d[y][x] := (x,y) への最小コスト, まだ確定していないときはINF
  int d[110][110];
  // used[y][x] := (x,y) が探索済みかどうか
  bool used[110][110];
  // 初期化
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      d[y][x] = INF;
      used[y][x] = false;
    }
  }

  // ゴールの位置を調べる
  P gp = search_goal(m);
  int gx = gp.first;
  int gy = gp.second;

  // (x,y)=(0,0) を始点として探索する
  priority_queue<State, vector<State>, greater<State>> q;
  q.push(State(0, P(0, 0)));
  d[0][0] = 0;

  int result = INF;
  while (!q.empty()) {
    // コストの小さい座標から順に出てくる
    int cost = q.top().first;
    int x = q.top().second.first;
    int y = q.top().second.second;
    q.pop();
    // 座標 (x,y) を確定ノードにする
    used[y][x] = true;

    // ゴールのとき
    if (x == gx && y == gy) {
      result = min(result, cost);
    }

    for (int i = 0; i < 4; i++) {
      int mx = x + dx[i];
      int my = y + dy[i];

      if (mx < 0 || my < 0 || mx >= w || my >= h || used[my][mx])
        continue;

      int next_cost = cost;
      if (m[y][x] == '.' && m[my][mx] == '#') {
        next_cost++;
      }
      if (next_cost < d[my][mx]) {
        d[my][mx] = next_cost;
        State next(next_cost, P(mx, my));
        q.push(next);
      }
    }
  }
  // debug
  // debug( m );
  /* for(int y=0 ; y < h ; y++ ){
          for(int x=0 ; x < w ; x++ ){
                  if( d[y][x] == INF ){
                          cout << "?";
                  }else{
                          cout << d[y][x];
                  }
          }
          cout << endl;
  }*/
  return result;
}

int main() {
  while (cin >> w >> h, w || h) {
    vector<string> m(h + 2);
    string t;
    for (int i = 0; i < w + 2; i++) {
      t.push_back('.');
    }
    m[0] = m[h + 1] = t;

    for (int y = 1; y <= h; y++) {
      string s, s_;
      cin >> s;
      s_ = "." + s + ".";
      m[y] = s_;
    }
    w += 2;
    h += 2;
    cout << solve(m) << endl;
    // debug2();
    // int ans = d[gy][gx];
    // cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> State;
const int INF = 1e+9;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};

void debug(const vector<string> &m) {
  for (int y = 0; y < m.size(); y++)
    cout << m[y] << endl;
}

// Æ³
int w, h;
// d[y][x] Í(y,x)ÌÅ¬RXg,
// Ü¾mèµÄ¢È¢Æ«ÍINF
int d[110][110];
// used[p] ÍP p=(x,y)ªTõÏÝ©Ç¤©
map<P, bool> used;
// n ÍÀWÌ(=w*h)
int n;
// S[ÌÊu
int gx, gy;

void debug2() {
  cout << "d:" << endl;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (d[y][x] == INF) {
        cout << '?';
      } else if (d[y][x] >= 0 && d[y][x] <= 9) {
        cout << d[y][x];
      } else {
        cout << 9;
      }
    }
    cout << endl;
  }
}

// S[ÌÊuð²×é
void search_goal(vector<string> &m) {
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (m[y][x] == '&') {
        m[y][x] = '.';
        gx = x;
        gy = y;
      }
    }
  }
}

void solve(vector<string> &m) {
  // ú»
  w += 2;
  h += 2;
  n = w * h;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      d[y][x] = INF;
      used[P(x, y)] = false;
    }
  }
  // S[ÌÊuð²×é
  search_goal(m);

  // (x,y)=(0,0) ðn_ÆµÄTõ·é
  P sp(0, 0);
  d[0][0] = 0;
  used[sp] = true;
  // State s; ÉÂ¢Ä s.first ÍRXg, s.second
  // ÍÀW(pair<int,int>)
  State ini(0, sp);
  priority_queue<State, vector<State>, greater<State>> q;
  q.push(ini);

  while (!q.empty()) {
    // RXgÌ¬³¢ÀW©çÉoÄ­é
    int cost = q.top().first;
    P p = q.top().second;
    q.pop();
    // ÀW p ðmèm[hÉ·é
    used[p] = true;

    for (int i = 0; i < 4; i++) {
      int mx = p.first + dx[i];
      int my = p.second + dy[i];
      P next_p(mx, my);

      if (mx < 0 || my < 0 || mx >= w || my >= h || used[next_p] == true)
        continue;

      int next_cost;
      if (m[my][mx] == '#') {
        d[my][mx] = min(d[my][mx], cost + 1);
        next_cost = cost + 1;
      } else if (m[my][mx] == '.') {
        d[my][mx] = min(d[my][mx], cost);
        next_cost = cost;
      }
      State next(next_cost, next_p);
      q.push(next);
    }
  }
}

int main() {
  while (cin >> w >> h, w || h) {
    vector<string> m(h + 2);
    string t;
    for (int i = 0; i < w + 2; i++)
      t.push_back('.');
    m[0] = m[h + 1] = t;

    for (int y = 1; y <= h; y++) {
      string s, s_;
      cin >> s;
      s_ = "." + s + ".";
      m[y] = s_;
    }
    // debug( m );
    solve(m);
    // debug2();
    int ans = d[gy][gx];
    cout << ans << endl;
  }
}