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

#include <bits/stdc++.h>
#define ll long long int
#define forr(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007
#define oo 100000000000000000
using namespace std;

void _sol() {
  int h, w, a, b, c, d;
  cin >> h >> w >> a >> b >> c >> d;
  vector<string> v(h + 1);
  forr(i, h) cin >> v[i + 1], v[i + 1] = '#' + v[i + 1];
  int dp[h + 2][w + 2];
  forr(i, h + 2) forr(j, w + 2) dp[i][j] = INT_MAX / 2;
  queue<pair<int, int>> q, border;
  q.push({a, b});
  dp[a][b] = 0;
  int tot = 1;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    // adj
    if (x - 1 >= 1 && dp[x - 1][y] > dp[x][y] && v[x - 1][y] == '.')
      dp[x - 1][y] = dp[x][y], q.push({x - 1, y});
    if (x + 1 <= h && dp[x + 1][y] > dp[x][y] && v[x + 1][y] == '.')
      dp[x + 1][y] = dp[x][y], q.push({x + 1, y});
    if (y - 1 >= 1 && dp[x][y - 1] > dp[x][y] && v[x][y - 1] == '.')
      dp[x][y - 1] = dp[x][y], q.push({x, y - 1});
    if (y + 1 <= w && dp[x][y + 1] > dp[x][y] && v[x][y + 1] == '.')
      dp[x][y + 1] = dp[x][y], q.push({x, y + 1});

    // border
    if (x - 1 >= 1 && v[x - 1][y] == '#')
      border.push({x, y});
    else if (x + 1 <= h && v[x + 1][y] == '#')
      border.push({x, y});
    else if (y - 1 >= 1 && v[x][y - 1] == '#')
      border.push({x, y});
    else if (y + 1 <= w && v[x][y + 1] == '#')
      border.push({x, y});

    if (q.empty()) {
      while (!border.empty()) {
        p = border.front();
        border.pop();
        x = p.first, y = p.second;
        for (int i = max(x - 2, 1); i <= min(x + 2, h); i++) {
          for (int j = max(y - 2, 1); j <= min(y + 2, w); j++) {
            if (i >= 1 && i <= h && j >= 1 && j <= w && v[i][j] == '.' &&
                dp[i][j] > dp[x][y] + 1) {
              dp[i][j] = dp[x][y] + 1;
              q.push({i, j});
            }
          }
        }
      }
    }
  }
  if (dp[c][d] == INT_MAX / 2)
    cout << -1;
  else
    cout << dp[c][d];
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  _sol();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define forr(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007
#define oo 100000000000000000
using namespace std;

void _sol() {
  int h, w, a, b, c, d;
  cin >> h >> w >> a >> b >> c >> d;
  vector<string> v(h + 1);
  forr(i, h) cin >> v[i + 1], v[i + 1] = '#' + v[i + 1];
  int dp[h + 2][w + 2];
  forr(i, h + 2) forr(j, w + 2) dp[i][j] = INT_MAX / 2;
  queue<pair<int, int>> q;
  q.push({a, b});
  dp[a][b] = 0;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    if (x - 1 >= 1 && dp[x - 1][y] > dp[x][y] && v[x - 1][y] == '.')
      dp[x - 1][y] = dp[x][y], q.push({x - 1, y});
    if (x + 1 <= h && dp[x + 1][y] > dp[x][y] && v[x + 1][y] == '.')
      dp[x + 1][y] = dp[x][y], q.push({x + 1, y});
    if (y - 1 >= 1 && dp[x][y - 1] > dp[x][y] && v[x][y - 1] == '.')
      dp[x][y - 1] = dp[x][y], q.push({x, y - 1});
    if (y + 1 <= w && dp[x][y + 1] > dp[x][y] && v[x][y + 1] == '.')
      dp[x][y + 1] = dp[x][y], q.push({x, y + 1});
    int xx = x, yy = y;
    for (int i = max(xx - 2, 1); i <= min(xx + 2, h); i++) {
      for (int j = max(yy - 2, 1); j <= min(yy + 2, w); j++) {
        if (i >= 1 && i <= h && j >= 1 && j <= w && v[i][j] == '.' &&
            dp[i][j] > dp[xx][yy] + 1) {
          dp[i][j] = dp[xx][yy] + 1;
          q.push({i, j});
          x = i, y = j;
          if (x - 1 >= 1 && dp[x - 1][y] > dp[x][y] && v[x - 1][y] == '.')
            dp[x - 1][y] = dp[x][y], q.push({x - 1, y});
          if (x + 1 <= h && dp[x + 1][y] > dp[x][y] && v[x + 1][y] == '.')
            dp[x + 1][y] = dp[x][y], q.push({x + 1, y});
          if (y - 1 >= 1 && dp[x][y - 1] > dp[x][y] && v[x][y - 1] == '.')
            dp[x][y - 1] = dp[x][y], q.push({x, y - 1});
          if (y + 1 <= w && dp[x][y + 1] > dp[x][y] && v[x][y + 1] == '.')
            dp[x][y + 1] = dp[x][y], q.push({x, y + 1});
        }
      }
    }
  }
  if (dp[c][d] == INT_MAX / 2)
    cout << -1;
  else
    cout << dp[c][d];
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  _sol();
}