Problem p00624 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef pair<string, int> P;

int main() {
  int h, w, d;
  string tmp, g;
  int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};

  while (cin >> h >> w, h || w) {
    g = "";
    for (int i = 0; i < h; i++) {
      cin >> tmp;
      g += tmp;
    }

    map<string, int> m;
    deque<P> q;
    q.push_back(P(g, 1));

    string hoge = "";
    bool f = false;
    while (!q.empty()) {
      P p = q.front();
      q.pop_front();
      g = p.first;
      d = p.second;
      if (m[g])
        continue;
      m[g] = d;

      if (hoge == g)
        break;
      hoge = g;
      int y, x;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          if (g[i * w + j] == '@') {
            y = i;
            x = j;
            break;
          }
        }
      }

      for (int i = 0; i < 4; i++) {
        int sy = y + dy[i], sx = x + dx[i];
        if (g[sy * w + sx] == '#' || g[sy * w + sx] == 'w')
          continue;
        if (g[sy * w + sx] == 'E') {
          cout << d << endl;
          f = true;
          break;
        }
        if (g[sy * w + sx] == '.') {
          tmp = g;
          tmp[y * w + x] = '.';
          tmp[sy * w + sx] = '@';
          q.push_back(P(tmp, d + 1));
        }
        if (g[sy * w + sx] == 'c') {
          tmp = g;
          tmp[sy * w + sx] = '.';
          for (;;) {
            sy += dy[i];
            sx += dx[i];
            if (tmp[sy * w + sx] == '#' || tmp[sy * w + sx] == 'c') {
              tmp[(sy - dy[i]) * w + (sx - dx[i])] = 'c';
              break;
            }
            if (tmp[sy * w + sx] == 'w') {
              tmp[sy * w + sx] = '.';
              break;
            }
          }
          q.push_front(P(tmp, d));
        }
      }
      if (f)
        break;
    }
    if (!f)
      cout << -1 << endl;
  }
}

Error: TLE

Fixed submission:

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

typedef pair<string, int> P;

int main() {
  int h, w, d;
  string tmp, g;
  int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};

  while (cin >> h >> w, h || w) {
    g = "";
    for (int i = 0; i < h; i++) {
      cin >> tmp;
      g += tmp;
    }

    map<string, int> m;
    priority_queue<P, vector<P>, greater<P>> q;
    q.push(P(g, 0));

    bool f = false;
    while (!q.empty()) {
      P p = q.top();
      q.pop();
      g = p.first;
      d = p.second;
      if (m[g])
        continue;
      m[g] = d;

      int y, x;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          if (g[i * w + j] == '@') {
            y = i;
            x = j;
            break;
          }
        }
      }

      for (int i = 0; i < 4; i++) {
        int sy = y + dy[i], sx = x + dx[i];
        if (g[sy * w + sx] == '#' || g[sy * w + sx] == 'w')
          continue;
        if (g[sy * w + sx] == 'E') {
          cout << d + 1 << endl;
          f = true;
          break;
        }
        if (g[sy * w + sx] == '.') {
          tmp = g;
          tmp[y * w + x] = '.';
          tmp[sy * w + sx] = '@';
          q.push(P(tmp, d + 1));
        }
        if (g[sy * w + sx] == 'c') {
          tmp = g;
          tmp[sy * w + sx] = '.';
          for (;;) {
            sy += dy[i];
            sx += dx[i];
            if (tmp[sy * w + sx] == '#' || tmp[sy * w + sx] == 'c') {
              tmp[(sy - dy[i]) * w + (sx - dx[i])] = 'c';
              break;
            }
            if (tmp[sy * w + sx] == 'w') {
              tmp[sy * w + sx] = '.';
              break;
            }
          }
          q.push(P(tmp, d));
        }
      }
      if (f)
        break;
    }
    if (!f)
      cout << -1 << endl;
  }
}