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

#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
// #define N 998244353
#define INF 1e16
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> Q;

const int inf = (int)1e9;

char board[1010][1010];
int h, w;

bool check(int i, int j) { return (i >= 0) && (i < h) && (j >= 0) && (j < w); }

int dist[1010][1010];
bool used[1010][1010];

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

int main(void) {
  cin >> h >> w;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> board[i][j];
      dist[i][j] = inf;
    }
  }
  priority_queue<Q, vector<Q>, function<bool(Q, Q)>> que(
      [](Q a, Q b) { return a.first >= b.first; });
  dist[sx][sy] = 0;
  que.push(Q(0, P(sx, sy)));
  while (!que.empty()) {
    Q q = que.top();
    que.pop();
    int cost = q.first;
    int nowx = q.second.first;
    int nowy = q.second.second;
    for (int i = 0; i < 4; i++) {
      int nx = nowx + dx[i];
      int ny = nowy + dy[i];
      if (!check(nx, ny))
        continue;
      if (board[nx][ny] == '.') {
        if (dist[nx][ny] > cost) {
          dist[nx][ny] = cost;
          que.push(Q(dist[nx][ny], P(nx, ny)));
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        int nx = nowx + i;
        int ny = nowy + j;
        if (!check(nx, ny))
          continue;
        if (board[nx][ny] == '.') {
          if (dist[nx][ny] > cost + 1) {
            dist[nx][ny] = cost + 1;
            que.push(Q(dist[nx][ny], P(nx, ny)));
          }
        }
      }
    }
  }
  if (dist[gx][gy] == inf)
    cout << -1 << endl;
  else
    cout << dist[gx][gy] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
// #define N 998244353
#define INF 1e16
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> Q;

const int inf = (int)1e9;

char board[1010][1010];
int h, w;

bool check(int i, int j) { return (i >= 0) && (i < h) && (j >= 0) && (j < w); }

int dist[1010][1010];
bool used[1010][1010];

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

int main(void) {
  cin >> h >> w;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> board[i][j];
      dist[i][j] = -1;
    }
  }
  queue<Q> que;
  dist[sx][sy] = 0;
  que.push(Q(0, P(sx, sy)));
  while (!que.empty()) {
    Q q = que.front();
    que.pop();
    int cost2 = q.first;
    int nowx = q.second.first;
    int nowy = q.second.second;
    for (int i = 0; i < 4; i++) {
      int nx = nowx + dx[i];
      int ny = nowy + dy[i];
      if (check(nx, ny) && board[nx][ny] == '.') {
        if (dist[nx][ny] == -1) {
          dist[nx][ny] = cost2;
          que.push(Q(dist[nx][ny], P(nx, ny)));
          continue;
        } else {
          if (dist[nx][ny] > cost2) {
            dist[nx][ny] = cost2;
            que.push(Q(dist[nx][ny], P(nx, ny)));
            continue;
          }
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        int nx = nowx + i;
        int ny = nowy + j;
        if (!check(nx, ny))
          continue;
        if (board[nx][ny] == '.') {
          if (dist[nx][ny] == -1) {
            dist[nx][ny] = cost2 + 1;
            que.push(Q(dist[nx][ny], P(nx, ny)));
            continue;
          }
          if (dist[nx][ny] > cost2 + 1) {
            dist[nx][ny] = cost2 + 1;
            que.push(Q(dist[nx][ny], P(nx, ny)));
          }
        }
      }
    }
  }
  if (dist[gx][gy] == -1)
    cout << -1 << endl;
  else
    cout << dist[gx][gy] << endl;
  return 0;
}