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

#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1000 + 5;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
char mp[maxn][maxn];
int n, m;
int sx, sy, tx, ty;
pii s, t;
map<pii, bool> vis;
int dis[maxn][maxn];
struct node {
  int dis;
  pii id;
  bool operator<(const node &x) const { return dis > x.dis; }
};
priority_queue<node> q;
void bfs() {
  q.push({0, s});
  memset(dis, 0x3f3f3f3f, sizeof(dis));
  dis[s.first][s.second] = 0;
  while (!q.empty()) {
    node cur = q.top();
    q.pop();
    int x = cur.id.first;
    int y = cur.id.second;
    if (vis[cur.id])
      continue;
    vis[cur.id] = 1;
    for (int i = 0; i < 4; ++i) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (x < 1 || y < 1 || x > n || y > m)
        continue;
      if (mp[xx][yy] == '.' && dis[xx][yy] > dis[x][y]) {
        dis[xx][yy] = dis[x][y];
        if (!vis[make_pair(xx, yy)])
          q.push({dis[xx][yy], make_pair(xx, yy)});
      }
    }
    for (int i = max(0, x - 2); i <= min(n, x + 2); ++i)
      for (int j = max(0, y - 2); j <= min(m, y + 2); ++j) {
        if (mp[i][j] == '.' && dis[i][j] > dis[x][y] + 1) {
          dis[i][j] = dis[x][y] + 1;
          if (!vis[make_pair(i, j)])
            q.push({dis[i][j], make_pair(i, j)});
        }
      }
  }
  int ans = dis[tx][ty] == 0x3f3f3f3f ? -1 : dis[tx][ty];
  cout << ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cin >> n >> m;
  cin >> sx >> sy;
  cin >> tx >> ty;
  s = make_pair(sx, sy);
  t = make_pair(tx, ty);
  for (int i = 1; i <= n; ++i)
    cin >> mp[i] + 1;
  bfs();
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1000 + 5;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
char mp[maxn][maxn];
int n, m;
int sx, sy, tx, ty;
pii node[maxn];
pii s, t;
map<pii, bool> vis;
int dis[maxn][maxn];
void bfs() {
  queue<pii> q;
  q.push(s);
  memset(dis, 0x3f3f3f3f, sizeof(dis));
  dis[s.first][s.second] = 0;
  vis[s] = 1;
  while (!q.empty()) {
    pii cur = q.front();
    q.pop();
    int x = cur.first;
    int y = cur.second;
    vis[cur] = 0;
    for (int i = 0; i < 4; ++i) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (x < 1 || y < 1 || x > n || y > m)
        continue;
      if (mp[xx][yy] == '.' && dis[xx][yy] > dis[x][y]) {
        dis[xx][yy] = dis[x][y];
        if (!vis[make_pair(xx, yy)])
          q.push(make_pair(xx, yy));
        vis[make_pair(xx, yy)] = 1;
      }
    }
    for (int i = max(0, x - 2); i <= min(n, x + 2); ++i)
      for (int j = max(0, y - 2); j <= min(m, y + 2); ++j) {
        if (mp[i][j] == '.' && dis[i][j] > dis[x][y] + 1) {
          dis[i][j] = dis[x][y] + 1;
          if (!vis[make_pair(i, j)])
            q.push(make_pair(i, j));
          vis[make_pair(i, j)] = 1;
        }
      }
  }
  int ans = dis[tx][ty] == 0x3f3f3f3f ? -1 : dis[tx][ty];
  cout << ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cin >> n >> m;
  cin >> sx >> sy;
  cin >> tx >> ty;
  s = make_pair(sx, sy);
  t = make_pair(tx, ty);
  for (int i = 1; i <= n; ++i)
    cin >> mp[i] + 1;
  bfs();
  return 0;
}