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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const long long INF = 1e15;

int main() {
  int h, w;
  cin >> h >> w;

  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--;
  sy--;
  gx--;
  gy--;

  pair<int, int> s = make_pair(sx, sy), g = make_pair(gx, gy);
  vector<vector<int>> ans(h, vector<int>(w, mod));
  string sur[h];

  for (int i = 0; i < h; i++)
    cin >> sur[i];

  auto inout = [&](int x, int y) {
    if (0 <= x && x < w && 0 <= y && y < h)
      return true;
    else
      return false;
  };

  auto add = [&](pair<int, int> now, int dx, int dy) {
    pair<int, int> ret = make_pair(now.first + dx, now.second + dy);
    return ret;
  };

  auto answer = [&](pair<int, int> now) {
    if (now == g) {
      cout << ans[now.second][now.first] << endl;
      return true;
    } else
      return false;
  };

  queue<pair<int, int>> p, q;
  map<pair<int, int>, bool> seen;

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

  p.push(s);
  seen[s] = true;
  ans[s.second][s.first] = 0;

  int can = 0;

  for (int l = 0; l <= h * w; l++) {
    while (!p.empty()) {
      pair<int, int> now = p.front();
      p.pop();
      for (int i = 0; i < 4; i++) {
        pair<int, int> nxt = add(now, dx[i], dy[i]);
        if (inout(nxt.first, nxt.second)) {
          if (sur[nxt.second][nxt.first] == '.' && !seen[nxt]) {
            ans[nxt.second][nxt.first] = can;
            seen[nxt] = true;
            if (answer(nxt))
              return 0;
            p.push(nxt);
          } else
            q.push(now);
        }
      }
    }
    can++;
    while (!q.empty()) {
      pair<int, int> now = q.front();
      q.pop();
      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          pair<int, int> nxt = make_pair(now.first + i, now.second + j);
          if (inout(nxt.first, nxt.second)) {
            if (sur[nxt.second][nxt.first] == '.' && !seen[nxt]) {
              ans[nxt.second][nxt.first] = can;
              seen[nxt] = true;
              p.push(nxt);
              if (answer(nxt))
                return 0;
            }
          }
        }
      }
    }
  }

  cout << -1 << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const long long INF = 1e15;

int main() {
  int h, w;
  cin >> h >> w;

  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--;
  sy--;
  gx--;
  gy--;

  pair<int, int> s = make_pair(sx, sy), g = make_pair(gx, gy);

  string sur[h];

  for (int i = 0; i < h; i++)
    cin >> sur[i];

  auto inout = [&](int x, int y) {
    if (0 <= x && x < w && 0 <= y && y < h) {
      // cout << "true" << " " << x << " " << y << endl;
      return true;
    } else {
      // cout << "false" << " " << x << " " << y << endl;
      return false;
    }
  };

  auto add = [&](pair<int, int> now, int dx, int dy) {
    pair<int, int> ret = make_pair(now.first + dx, now.second + dy);
    return ret;
  };

  deque<pair<pair<int, int>, int>> dq;
  map<pair<int, int>, bool> seen;

  vector<vector<int>> ans(h, vector<int>(w, mod));

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

  dq.push_back(make_pair(s, 0));
  seen[s] = true;
  ans[s.second][s.first] = 0;

  int a = 4, b = 1;

  while (!dq.empty()) {
    pair<pair<int, int>, int> now = dq.front();
    dq.pop_front();
    if (now.second > ans[now.first.second][now.first.first])
      continue;
    for (int i = 0; i < 4; i++) {
      if (inout(now.first.first + dx[i], now.first.second + dy[i])) {
        pair<int, int> nxtz = add(now.first, dx[i], dy[i]);
        if (sur[nxtz.second][nxtz.first] == '.') {
          if (ans[nxtz.second][nxtz.first] <= now.second)
            continue;
          else {
            ans[nxtz.second][nxtz.first] = now.second;
            seen[nxtz] = true;
            dq.push_back(make_pair(nxtz, now.second));
          }
        } else {
          for (int j = -2; j < 3; j++) {
            for (int k = -2; k < 3; k++) {
              if (j == 0 && k == 0)
                continue;
              int x = now.first.first + j, y = now.first.second + k;
              if (inout(x, y)) {
                if (sur[y][x] == '.' && ans[y][x] > now.second + 1) {

                  ans[y][x] = now.second + 1;
                  dq.push_back(make_pair(make_pair(x, y), now.second + 1));
                  seen[make_pair(x, y)] = true;
                }
              }
            }
          }
        }
      }
    }
  }

  int fans = ans[g.second][g.first];
  if (fans == mod)
    cout << -1 << endl;
  else
    cout << fans << endl;

  return 0;
}