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

#include <bits/stdc++.h>
#define ll long long
#define pp pair<int, int>
#define ppn pair<int, pp>
#define F first
#define S second
#define N 300005
#define mod 1000000007
#define pi 3.14159265358979323846

using namespace std;

int n, m, a, b, xxx, yyy, ans;

char s[1005][1005];
int dp[1005][1005];
int xx[] = {-1, 0, 0, 1}, yy[] = {0, -1, 1, 0};
priority_queue<ppn, vector<ppn>, greater<ppn>> q;

bool check() {

  q.push({0, {a - 1, b - 1}});
  while (!q.empty()) {
    ppn t = q.top();
    q.pop();

    int u = t.S.F;
    int v = t.S.S;
    int c = t.F;
    if (dp[u][v])
      continue;
    dp[u][v] = 1;

    // cout<<t.F<<" "<<t.S.F<<" "<<t.S.S<<endl;
    if (u == xxx - 1 && v == yyy - 1) {
      ans = c;
      return true;
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        // if(!i&&!j)continue;
        int x = u + i;
        int y = v + j;
        if (x < 0 || x >= n || y < 0 || y >= m)
          continue;
        if (dp[x][y] || s[x][y] == '#')
          continue;
        q.push({t.F + 1, {x, y}});
      }
    }

    for (int i = 0; i < 4; i++) {
      int x = u + xx[i];
      int y = v + yy[i];
      if (x < 0 || x >= n || y < 0 || y >= m)
        continue;
      if (dp[x][y] || s[x][y] == '#')
        continue;
      q.push({t.F, {x, y}});
    }
  }
  return false;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  // memset(dp,-1,sizeof dp);
  cin >> n >> m;
  cin >> a >> b >> xxx >> yyy;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> s[i][j];

  if (check())
    cout << ans << endl;
  else
    cout << -1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pp pair<int, int>
#define ppn pair<int, pp>
#define F first
#define S second
#define N 300005
#define mod 1000000007
#define pi 3.14159265358979323846

using namespace std;

int n, m, a, b, xxx, yyy, ans;

char s[1005][1005];
int dp[1005][1005];
int xx[] = {-1, 0, 0, 1}, yy[] = {0, -1, 1, 0};
priority_queue<ppn, vector<ppn>, greater<ppn>> q;

bool check() {

  q.push({0, {a - 1, b - 1}});
  while (!q.empty()) {
    ppn t = q.top();
    q.pop();

    int u = t.S.F;
    int v = t.S.S;
    int c = t.F;
    dp[u][v] = 1;

    // cout<<t.F<<" "<<t.S.F<<" "<<t.S.S<<endl;
    if (u == xxx - 1 && v == yyy - 1) {
      ans = c;
      return true;
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        // if(!i&&!j)continue;
        int x = u + i;
        int y = v + j;
        if (x < 0 || x >= n || y < 0 || y >= m)
          continue;
        if (dp[x][y] || s[x][y] == '#')
          continue;
        q.push({t.F + 1, {x, y}});
      }
    }

    for (int i = 0; i < 4; i++) {
      int x = u + xx[i];
      int y = v + yy[i];
      if (x < 0 || x >= n || y < 0 || y >= m)
        continue;
      if (dp[x][y] || s[x][y] == '#')
        continue;
      q.push({t.F, {x, y}});
    }
  }
  return false;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  // memset(dp,-1,sizeof dp);
  cin >> n >> m;
  cin >> a >> b >> xxx >> yyy;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> s[i][j];

  if (check())
    cout << ans << endl;
  else
    cout << -1 << endl;
}