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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 50;
int n, m;
int sx, sy;
int cx, cy;
char mp[maxn][maxn];
int vis[maxn][maxn];
int vis2[maxn][maxn];
struct node {
  int x, y, d;
  bool operator<(const node &p) const { return d > p.d; }
};
int dx[4] = {0, 0, 1, -1};
int dy[4] = {-1, 1, 0, 0};
bool check(int x, int y) {
  if (x >= 1 && x <= n && y >= 1 && y <= m && mp[x][y] == '.')
    return true;
  else
    return false;
}
int bfs() {
  priority_queue<node> q;
  node a, now;
  a.x = sx, a.y = sy, a.d = 0;
  q.push(a);
  vis[sx][sy] = 1;
  while (!q.empty()) {
    now = q.top();
    q.pop();
    // cout << now.x << " " << now.y << " " << now.d << endl;
    if (now.x == cx && now.y == cy) {
      return now.d;
    }
    for (int i = 0; i < 4; i++) {
      int nx = now.x + dx[i];
      int ny = now.y + dy[i];
      if (check(nx, ny) && !vis[nx][ny]) {
        a.x = nx, a.y = ny, a.d = now.d;
        q.push(a);
        vis[nx][ny] = 1;
        vis2[nx][ny] = 1;
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = now.x + i;
        int ny = now.y + j;
        if (check(nx, ny) && !vis2[nx][ny] && !vis[nx][ny]) {
          a.x = nx, a.y = ny, a.d = now.d + 1;
          q.push(a);
          vis2[nx][ny] = 1;
        }
      }
    }
  }
  return -1;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n >> m;
  cin >> sx >> sy;
  cin >> cx >> cy;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> mp[i][j];
    }
  }
  cout << bfs() << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 50;
int n, m;
int sx, sy;
int cx, cy;
char mp[maxn][maxn];
int vis[maxn][maxn];
struct node {
  int x, y, d;
  bool operator<(const node &p) const { return d > p.d; }
};
int dx[4] = {0, 0, 1, -1};
int dy[4] = {-1, 1, 0, 0};
bool check(int x, int y) {
  if (x >= 1 && x <= n && y >= 1 && y <= m && mp[x][y] == '.')
    return true;
  else
    return false;
}
int bfs() {
  priority_queue<node> q;
  node a, now;
  a.x = sx, a.y = sy, a.d = 0;
  q.push(a);
  vis[sx][sy] = 1;
  while (!q.empty()) {
    now = q.top();
    q.pop();
    // cout << now.x << " " << now.y << " " << now.d << endl;
    if (now.x == cx && now.y == cy) {
      return now.d;
    }
    for (int i = 0; i < 4; i++) {
      int nx = now.x + dx[i];
      int ny = now.y + dy[i];
      if (check(nx, ny) && !vis[nx][ny]) {
        a.x = nx, a.y = ny, a.d = now.d;
        q.push(a);
        vis[nx][ny] = 1;
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = now.x + i;
        int ny = now.y + j;
        if (check(nx, ny) && !vis[nx][ny]) {
          a.x = nx, a.y = ny, a.d = now.d + 1;
          q.push(a);
        }
      }
    }
  }
  return -1;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n >> m;
  cin >> sx >> sy;
  cin >> cx >> cy;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> mp[i][j];
    }
  }
  cout << bfs() << endl;
  return 0;
}