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

#include <bits/stdc++.h>

using namespace std;
#define int long long
const int N = 2666, inf = 0x3f3f3f3f;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int n, m, Sx, Sy, Tx, Ty;
char mp[N][N];
int vis[N][N];
struct Node {
  int x, y, z;
};
deque<Node> q;
int step[N][N];
signed main() {

  cin >> n >> m >> Sx >> Sy >> Tx >> Ty;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> mp[i][j], step[i][j] = inf;

  q.push_back((Node){Sx, Sy, 0});
  while (!q.empty()) {
    Node t = q.front();
    q.pop_front();
    if (vis[t.x][t.y])
      continue;
    vis[t.x][t.y] = 1;
    step[t.x][t.y] = min(step[t.x][t.y], t.z);
    for (int i = 0; i < 4; i++) {
      int tx = t.x + dx[i], ty = t.y + dy[i];
      if (tx >= 1 && ty >= 1 && tx <= n && ty <= m && mp[tx][ty] != '#') {
        q.push_front({tx, ty, t.z});
      }
    }
    for (int i = (t.x - 2); i <= (t.x + 2); i++)
      for (int j = (t.y - 2); j <= (t.y + 2); j++)
        if (i >= 1 && j >= 1 && i <= n && j <= m && mp[i][j] != '#') {
          q.push_back({i, j, t.z + 1});
        }
  }
  if (step[Tx][Ty] == inf)
    cout << "-1";
  else
    cout << step[Tx][Ty];
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define int long long
const int N = 1009, inf = 0x3f3f3f3f;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
struct Node {
  int x, y, z;

  bool operator<(const Node &t) const { return z > t.z; }
};
priority_queue<Node> Q;
int n, m, Sx, Sy, Tx, Ty;
char mp[N][N];
int mark[N][N], vis[N][N]; // S 1 , T -1
void bfs(int x, int y, int markk) {
  memset(vis, 0, sizeof(vis));
  queue<pair<int, int>> q;
  q.push({x, y});
  while (!q.empty()) {
    pair<int, int> t = q.front();
    q.pop();
    if (vis[t.first][t.second])
      continue;
    vis[t.first][t.second] = 1;
    mark[t.first][t.second] = markk;
    for (int i = 0; i < 4; i++) {
      int tx = t.first + dx[i];
      int ty = t.second + dy[i];
      if (tx >= 1 && ty >= 1 && tx <= n && ty <= m && mp[tx][ty] != '#') {
        q.push({tx, ty});
      }
    }
  }
}

int BFS() {
  memset(vis, 0, sizeof(vis));
  while (!Q.empty()) {
    struct Node t = Q.top();
    Q.pop();
    /*
    if(vis[t.x][t.y]) continue;
    vis[t.x][t.y]=1;
    */
    for (int i = 0; i < 4; i++) {
      int tx = t.x + dx[i];
      int ty = t.y + dy[i];
      if (tx >= 1 && ty >= 1 && tx <= n && ty <= m && mp[tx][ty] != '#') {
        Q.push({tx, ty, t.z});
        if (mark[tx][ty] == 1) {
          //	cout<<tx<<" "<<ty<<'\\n';
          return t.z;
        }
      }
    }

    for (int i = (t.x - 2); i <= (t.x + 2); i++) {
      for (int j = (t.y - 2); j <= (t.y + 2); j++) {
        if (i >= 1 && j >= 1 && i <= n && j <= n && mp[i][j] != '#') {
          if (mark[i][j] != -1) {
            Q.push((Node){i, j, t.z + 1});
            if (mark[i][j] == 1) {
              //	cout<<i<<" "<<j<<'\\n';
              return t.z + 1;
            }
          }
        }
      }
    }
  }
  return -1;
}
signed main() {
  cin >> n >> m >> Sx >> Sy >> Tx >> Ty;
  getchar();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%c", &mp[i][j]);
    }
    getchar();
  }
  bfs(Sx, Sy, 1);
  if (mark[Tx][Ty] == 1) {
    cout << "0";
    return 0;
  }
  bfs(Tx, Ty, -1);
  /*
  for(int i=1;i<=n;i++){
          for(int j=1;j<=m;j++){
                  cout<<mark[i][j]<<" ";
          }
          cout<<'\\n';
  }
  */
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (mark[i][j] == -1) {
        Q.push((Node){i, j, 0});
      }
    }
  }
  int ans = BFS();
  cout << ans;
  return 0;
}
/**
.....
...#.
...#.
..#..
..#..
*/