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

#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int H, W;
  cin >> H >> W;

  int ch, dh, cw, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  vector<string> s(H);
  for (int i = 0; i < H; ++i) {
    cin >> s[i];
  }

  vector<vector<int>> map(H, vector<int>(W, -1));
  map[ch][cw] = 0;

  queue<pair<int, int>> q;
  queue<pair<int, int>> qq;
  q.push(make_pair(ch, cw));
  qq.push(make_pair(ch, cw));
  while (!q.empty()) {
    set<pair<int, int>> qqvisit;
    while (!q.empty()) {
      pair<int, int> p = q.front();
      q.pop();
      if (qqvisit.find(p) == qqvisit.end()) {
        qq.push(p);
      }
      int warp = map[p.first][p.second];
      if (p.first + 1 < H && s[p.first + 1][p.second] == '.' &&
          map[p.first + 1][p.second] == -1) {
        map[p.first + 1][p.second] = warp;
        q.push(make_pair(p.first + 1, p.second));
        if (qqvisit.find(make_pair(p.first + 1, p.second)) == qqvisit.end()) {
          qq.push(make_pair(p.first + 1, p.second));
          qqvisit.insert(make_pair(p.first + 1, p.second));
        }
      }
      if (p.first - 1 >= 0 && s[p.first - 1][p.second] == '.' &&
          map[p.first - 1][p.second] == -1) {
        map[p.first - 1][p.second] = warp;
        q.push(make_pair(p.first - 1, p.second));
        if (qqvisit.find(make_pair(p.first - 1, p.second)) == qqvisit.end()) {
          qq.push(make_pair(p.first - 1, p.second));
          qqvisit.insert(make_pair(p.first - 1, p.second));
        }
      }
      if (p.second + 1 < W && s[p.first][p.second + 1] == '.' &&
          map[p.first][p.second + 1] == -1) {
        map[p.first][p.second + 1] = warp;
        q.push(make_pair(p.first, p.second + 1));
        if (qqvisit.find(make_pair(p.first, p.second + 1)) == qqvisit.end()) {
          qq.push(make_pair(p.first, p.second + 1));
          qqvisit.insert(make_pair(p.first, p.second + 1));
        }
      }
      if (p.second - 1 >= 0 && s[p.first][p.second - 1] == '.' &&
          map[p.first][p.second - 1] == -1) {
        map[p.first][p.second - 1] = warp;
        q.push(make_pair(p.first, p.second - 1));
        if (qqvisit.find(make_pair(p.first, p.second - 1)) == qqvisit.end()) {
          qq.push(make_pair(p.first, p.second - 1));
          qqvisit.insert(make_pair(p.first, p.second - 1));
        }
      }
    }
    set<pair<int, int>> qvisit;
    while (!qq.empty()) {
      pair<int, int> p = qq.front();
      qq.pop();
      int warp = map[p.first][p.second] + 1;
      for (int h = -2; h <= 2; ++h) {
        for (int w = -2; w <= 2; ++w) {
          if (p.first + h >= 0 && p.first + h < H && p.second + w >= 0 &&
              p.second + w < W && s[p.first + h][p.second + w] == '.' &&
              map[p.first + h][p.second + w] == -1) {
            map[p.first + h][p.second + w] = warp;
            if (qvisit.find(make_pair(p.first + h, p.second + w)) ==
                qvisit.end()) {
              q.push(make_pair(p.first + h, p.second + w));
              qvisit.insert(make_pair(p.first + h, p.second + w));
            }
          }
        }
      }
    }
  }

  cout << map[dh][dw] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int H, W;
  cin >> H >> W;

  int ch, dh, cw, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  vector<string> s(H);
  for (int i = 0; i < H; ++i) {
    cin >> s[i];
  }

  vector<vector<int>> map(H, vector<int>(W, -1));
  map[ch][cw] = 0;

  queue<pair<int, int>> q;
  queue<pair<int, int>> qq;
  q.push(make_pair(ch, cw));
  qq.push(make_pair(ch, cw));
  while (!q.empty()) {
    set<pair<int, int>> qqvisit;
    while (!q.empty()) {
      pair<int, int> p = q.front();
      q.pop();
      if (qqvisit.find(p) == qqvisit.end()) {
        qq.push(p);
      }
      int warp = map[p.first][p.second];
      if (p.first + 1 < H && s[p.first + 1][p.second] == '.' &&
          map[p.first + 1][p.second] == -1) {
        map[p.first + 1][p.second] = warp;
        q.push(make_pair(p.first + 1, p.second));
        if (qqvisit.find(make_pair(p.first + 1, p.second)) == qqvisit.end()) {
          qq.push(make_pair(p.first + 1, p.second));
          qqvisit.insert(make_pair(p.first + 1, p.second));
        }
      }
      if (p.first - 1 >= 0 && s[p.first - 1][p.second] == '.' &&
          map[p.first - 1][p.second] == -1) {
        map[p.first - 1][p.second] = warp;
        q.push(make_pair(p.first - 1, p.second));
        if (qqvisit.find(make_pair(p.first - 1, p.second)) == qqvisit.end()) {
          qq.push(make_pair(p.first - 1, p.second));
          qqvisit.insert(make_pair(p.first - 1, p.second));
        }
      }
      if (p.second + 1 < W && s[p.first][p.second + 1] == '.' &&
          map[p.first][p.second + 1] == -1) {
        map[p.first][p.second + 1] = warp;
        q.push(make_pair(p.first, p.second + 1));
        if (qqvisit.find(make_pair(p.first, p.second + 1)) == qqvisit.end()) {
          qq.push(make_pair(p.first, p.second + 1));
          qqvisit.insert(make_pair(p.first, p.second + 1));
        }
      }
      if (p.second - 1 >= 0 && s[p.first][p.second - 1] == '.' &&
          map[p.first][p.second - 1] == -1) {
        map[p.first][p.second - 1] = warp;
        q.push(make_pair(p.first, p.second - 1));
        if (qqvisit.find(make_pair(p.first, p.second - 1)) == qqvisit.end()) {
          qq.push(make_pair(p.first, p.second - 1));
          qqvisit.insert(make_pair(p.first, p.second - 1));
        }
      }
    }
    set<pair<int, int>> qvisit;
    while (!qq.empty()) {
      pair<int, int> p = qq.front();
      qq.pop();
      int warp = map[p.first][p.second] + 1;
      for (int h = -2; h <= 2; ++h) {
        for (int w = -2; w <= 2; ++w) {
          if (p.first + h >= 0 && p.first + h < H && p.second + w >= 0 &&
              p.second + w < W && s[p.first + h][p.second + w] == '.' &&
              map[p.first + h][p.second + w] == -1) {
            map[p.first + h][p.second + w] = warp;
            if (qvisit.find(make_pair(p.first + h, p.second + w)) ==
                qvisit.end()) {
              q.push(make_pair(p.first + h, p.second + w));
              qvisit.insert(make_pair(p.first + h, p.second + w));
            }
          }
        }
      }
    }
  }

  for (int h = 0; h < H; ++h) {
    for (int w = 0; w < W; ++w) {
      cerr << map[h][w] << " ";
    }
    cerr << endl;
  }

  cout << map[dh][dw] << endl;

  return 0;
}