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

#include <bits/stdc++.h>
#define ll long long
#define db double
using namespace std;
const int M = 1010, INF = 0x3f3f3f3f;
int dis[M][M], n, m;
int d[4][4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool g[M][M], vis[M][M];
char s[M];
pair<int, int> C, D;
int main() {
  memset(dis, INF, sizeof(dis));
  scanf("%d%d", &n, &m);
  scanf("%d%d", &C.first, &C.second);
  scanf("%d%d", &D.first, &D.second);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= m; j++)
      g[i][j] = (s[j] == '.');
  }
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      q;
  q.push({0, C});
  while (!q.empty()) {
    pair<int, pair<int, int>> tmp = q.top();
    q.pop();
    pair<int, int> p = tmp.second;
    if (vis[p.first][p.second] || dis[p.first][p.second] <= tmp.first)
      continue;
    // printf("%d %d %d\\n",tmp.first,p.first,p.second);
    dis[p.first][p.second] = tmp.first;
    vis[p.first][p.second] = 1;
    for (int i = 0; i < 4; i++) {
      pair<int, int> pp(p.first + d[i][0], p.second + d[i][1]);
      if (pp.first > 0 && pp.first <= n && pp.second > 0 && pp.second <= m &&
          g[pp.first][pp.second])
        q.push({tmp.first, pp});
    }
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        pair<int, int> pp(p.first + i, p.second + j);
        if (pp.first > 0 && pp.first <= n && pp.second > 0 && pp.second <= m &&
            g[pp.first][pp.second])
          // printf("%d %d\\n",pp.first,pp.second),
          q.push({tmp.first + 1, pp});
      }
    // puts("");
  }
  printf("%d\\n", dis[D.first][D.second] == INF ? -1 : dis[D.first][D.second]);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define db double
using namespace std;
const int M = 1010, INF = 0x3f3f3f3f;
int dis[M][M], n, m;
int d[4][4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool g[M][M];
char s[M];
pair<int, int> C, D;
int main() {
  memset(dis, INF, sizeof(dis));
  scanf("%d%d", &n, &m);
  scanf("%d%d", &C.first, &C.second);
  scanf("%d%d", &D.first, &D.second);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= m; j++)
      g[i][j] = (s[j] == '.');
  }
  priority_queue<pair<int, pair<int, int>>> q;
  q.push({0, C});
  while (!q.empty()) {
    pair<int, pair<int, int>> tmp = q.top();
    q.pop();
    pair<int, int> p = tmp.second;
    // printf("%d %d %d\\n",tmp.first,p.first,p.second);
    if (dis[p.first][p.second] <= tmp.first)
      continue;
    dis[p.first][p.second] = tmp.first;
    for (int i = 0; i < 4; i++) {
      pair<int, int> pp(p.first + d[i][0], p.second + d[i][1]);
      if (pp.first > 0 && pp.first <= n && pp.second > 0 && pp.second <= m &&
          g[pp.first][pp.second])
        q.push({tmp.first, pp});
    }
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        pair<int, int> pp(p.first + i, p.second + j);
        if (pp.first > 0 && pp.first <= n && pp.second > 0 && pp.second <= m &&
            g[pp.first][pp.second])
          q.push({tmp.first + 1, pp});
      }
  }
  printf("%d\\n", dis[D.first][D.second] == INF ? -1 : dis[D.first][D.second]);
  return 0;
}