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

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define llu unsigned long long int
#define sa(a, n, t)                                                            \\
  for (int ii = 0; ii < n; ii++)                                               \\
  scanf("%" #t "", &(a)[ii])
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, a, n) for (int i = a; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rreps(i, a, n) for (int i = n - 1; i >= a; i--)
#define validl(i, a, b) ((i >= a) && (i <= b)) ? 1 : 0
#define validmat(x, y, a, b, c, d) (validl(x, a, b) && validl(y, a, b)) ? 1 : 0
#define validmn(x, y, m, n) validmat(x, y, 0, m, 0, n)
#define valid(i, n) validl(i, 0, n)
#define v(t) vector<t>
#define vv(t) vector<vector<t>>
#define mk make_pair
#define pb push_back
#define pa(a, n, t)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  printf("%" #t "", a[i])
#define p2 pair<int, int>
#define p3 pair<p2, int>
#define fi first
#define se second
#define sd(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define pl(a) printf("%lld", a)
#define pd(a) printf("%d", a)
#define sf(a) scanf("%lf", &a)
#define pf(a) printf("%lf", a)
#define nline printf("\\n")
#define ss(a) scanf("%s", a)
#define ps(a) printf("%s", a)
#define sc(a) scanf("%c", &a)
#define pc(a) printf("%c", &a)
#define mf(m, f) m.find(f) != m.end()
#define pp3(m) cout << m.fi.fi << " " << m.fi.se << " " << m.se << " "
#define pp2(m) cout << m.fi << " " << m.se << " "
#define debug 0
/*v(int) adj[200001];
void dfs(int i,int p){
    rep(j,adj[i].size()){
        if(p!=adj[i][j])
            dfs(adj[i][j],i);
    }
}*/
int n, m, p, q;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw;
  cin >> dh >> dw;
  string s[1001];
  int val[1001][1001];
  int added[1001][1001];
  rep(i, h) rep(j, w) val[i][j] = INT_MAX, added[i][j] = false;
  rep(i, h) cin >> s[i];
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      qq;
  ch--, cw--, dh--, dw--;
  int dx[] = {1, -1, 0, 0};
  int dy[] = {0, 0, 1, -1};
  qq.push(mk(0, mk(ch, cw)));
  val[ch][cw] = 0;
  while (!qq.empty()) {
    auto x = qq.top();
    qq.pop();
    if (val[x.se.fi][x.se.se] < x.fi)
      continue;
    // val[x.se.fi][x.se.se]=x.fi;
    if (x.se.fi == dh && x.se.se == dw)
      break;
    rep(i, 4) {
      p = x.se.fi + dx[i];
      q = x.se.se + dy[i];
      if (p >= 0 && q >= 0 && p < h && q < w && s[p][q] == '.' &&
          val[p][q] > x.fi) {
        qq.push(mk(x.fi, mk(p, q)));
        val[p][q] = x.fi;
        // added[p][q]=true;
      }
    }
    reps(i, -2, 3) reps(j, -2, 3) {
      p = x.se.fi + i;
      q = x.se.se + j;
      if (p >= 0 && q >= 0 && p < h && q < w && s[p][q] == '.' &&
          val[p][q] > x.fi + 1) {
        qq.push(mk(x.fi + 1, mk(p, q)));
        val[p][q] = x.fi + 1;
      }
    }
  }
  if (val[dh][dw] != INT_MAX)
    cout << val[dh][dw] << endl;
  else
    cout << -1 << endl;
  // cout<<ans<<endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define llu unsigned long long int
#define sa(a, n, t)                                                            \\
  for (int ii = 0; ii < n; ii++)                                               \\
  scanf("%" #t "", &(a)[ii])
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, a, n) for (int i = a; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rreps(i, a, n) for (int i = n - 1; i >= a; i--)
#define validl(i, a, b) ((i >= a) && (i <= b)) ? 1 : 0
#define validmat(x, y, a, b, c, d) (validl(x, a, b) && validl(y, a, b)) ? 1 : 0
#define validmn(x, y, m, n) validmat(x, y, 0, m, 0, n)
#define valid(i, n) validl(i, 0, n)
#define v(t) vector<t>
#define vv(t) vector<vector<t>>
#define mk make_pair
#define pb push_back
#define pa(a, n, t)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  printf("%" #t "", a[i])
#define p2 pair<int, int>
#define p3 pair<p2, int>
#define fi first
#define se second
#define sd(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define pl(a) printf("%lld", a)
#define pd(a) printf("%d", a)
#define sf(a) scanf("%lf", &a)
#define pf(a) printf("%lf", a)
#define nline printf("\\n")
#define ss(a) scanf("%s", a)
#define ps(a) printf("%s", a)
#define sc(a) scanf("%c", &a)
#define pc(a) printf("%c", &a)
#define mf(m, f) m.find(f) != m.end()
#define pp3(m) cout << m.fi.fi << " " << m.fi.se << " " << m.se << " "
#define pp2(m) cout << m.fi << " " << m.se << " "
#define debug 0
/*v(int) adj[200001];
void dfs(int i,int p){
    rep(j,adj[i].size()){
        if(p!=adj[i][j])
            dfs(adj[i][j],i);
    }
}*/
int n, m, p, q;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw;
  cin >> dh >> dw;
  string s[1001];
  int val[1001][1001];
  rep(i, h) rep(j, w) val[i][j] = INT_MAX;
  rep(i, h) cin >> s[i];
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      qq;
  ch--, cw--, dh--, dw--;
  int dx[] = {1, -1, 0, 0};
  int dy[] = {0, 0, 1, -1};
  qq.push(mk(0, mk(ch, cw)));
  while (!qq.empty()) {
    auto x = qq.top();
    qq.pop();
    if (val[x.se.fi][x.se.se] < x.fi)
      continue;
    val[x.se.fi][x.se.se] = x.fi;
    if (x.se.fi == dh && x.se.se == dw)
      break;
    rep(i, 4) {
      p = x.se.fi + dx[i];
      q = x.se.se + dy[i];
      if (p >= 0 && q >= 0 && p < h && q < w && s[p][q] == '.' &&
          val[p][q] > x.fi) {
        qq.push(mk(x.fi, mk(p, q)));
      }
    }
    reps(i, -2, 3) reps(j, -2, 3) {
      p = x.se.fi + i;
      q = x.se.se + j;
      if (p >= 0 && q >= 0 && p < h && q < w && s[p][q] == '.' &&
          val[p][q] > x.fi + 1) {
        qq.push(mk(x.fi + 1, mk(p, q)));
      }
    }
  }
  if (val[dh][dw] != INT_MAX)
    cout << val[dh][dw] << endl;
  else
    cout << -1 << endl;
  // cout<<ans<<endl;
  return 0;
}