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

#include <bits/stdc++.h>
using namespace std;
#define FO(x)                                                                  \\
  {                                                                            \\
    freopen(#x ".in", "r", stdin);                                             \\
    freopen(#x ".out", "w", stdout);                                           \\
  }
#define pa pair<int, int>
#define mod 998244353
#define ll long long
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define cl(x) memset(x, 0, sizeof x)
#ifdef Devil_Gary
#define bug(x) cout << (#x) << " " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define bug(x)
#define debug(...)
#endif

const int INF = 0x7fffffff;
const int N = 2e3 + 5;
/*
char *TT,*mo,but[(1<<15)+2];
#define getchar()
((TT==mo&&(mo=(TT=but)+fread(but,1,1<<15,stdin),TT==mo))?-1:*TT++)//*/
inline int read() {
  int x = 0, rev = 0, ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      rev = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return rev ? -x : x;
} /*
 struct Edge{
         int nxt,v;
 //	int w;
 //	int id;
 }e[N<<1];
 void add(int u,int v){
         e[++tot].v=v,e[tot].nxt=head[u],head[u]=tot;
         e[++tot].v=u,e[tot].nxt=head[v],head[v]=tot;
 }*/
priority_queue<pair<int, pa>> heap;
char s[N][N];
int d[N][N];
int n, m, sx, sy, tx, ty;
int x[4] = {1, -1, 0, 0}, y[4] = {0, 0, 1, -1};
int main() {
#ifdef Devil_Gary
  freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
  n = read(), m = read();
  memset(d, 127 / 3, sizeof d);
  sx = read(), sy = read(), tx = read(), ty = read();
  d[sx][sy] = 0;
  for (int i = 1; i <= n; i++)
    cin >> s[i] + 1;
  heap.push(mk(0, mk(sx, sy)));
  while (1) {
    for (; !heap.empty() &&
           -d[heap.top().se.fi][heap.top().se.se] != heap.top().fi;
         heap.pop())
      ;
    if (heap.empty())
      break;
    pa now = heap.top().se;
    heap.pop();
    //		cout<<now.fi<<" "<<now.se<<" "<<d[now.fi][now.se]<<endl;
    for (int i = 0; i < 4; i++) {
      int nx = now.fi + x[i], ny = now.se + y[i];
      if (nx < 1 || nx > n || ny < 1 || ny > m)
        continue;
      //			if(s[now.fi][now.se]=='#'&&s[nx][ny]=='#')
      //continue;
      if (s[nx][ny] == '#')
        continue;
      //			cout<<nx<<" "<<ny<<endl;
      //			int flg=(s[nx][ny]=='#');
      if (d[nx][ny] > d[now.fi][now.se]) {
        d[nx][ny] = d[now.fi][now.se];
        heap.push(mk(-d[nx][ny], mk(nx, ny)));
      }
    }
    for (int i = now.fi - 2; i <= now.fi + 2; i++)
      for (int j = now.se - 2; j <= now.se + 2; j++) {
        if (i < 1 || i > n || j < 1 || j > m)
          continue;

        if (s[i][j] == '#')
          continue;
        if (d[i][j] > d[now.fi][now.se] + 1) {
          d[i][j] = d[now.fi][now.se] + 1;
          heap.push(mk(-d[i][j], mk(i, j)));
        }
      }
  }
  if (d[tx][ty] == d[0][0])
    puts("-1");
  else
    cout << d[tx][ty] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FO(x)                                                                  \\
  {                                                                            \\
    freopen(#x ".in", "r", stdin);                                             \\
    freopen(#x ".out", "w", stdout);                                           \\
  }
#define pa pair<int, int>
#define mod 998244353
#define ll long long
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define cl(x) memset(x, 0, sizeof x)
#ifdef Devil_Gary
#define bug(x) cout << (#x) << " " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define bug(x)
#define debug(...)
#endif

const int INF = 0x7fffffff;
const int N = 1e3 + 5;
/*
char *TT,*mo,but[(1<<15)+2];
#define getchar()
((TT==mo&&(mo=(TT=but)+fread(but,1,1<<15,stdin),TT==mo))?-1:*TT++)//*/
inline int read() {
  int x = 0, rev = 0, ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      rev = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return rev ? -x : x;
} /*
 struct Edge{
         int nxt,v;
 //	int w;
 //	int id;
 }e[N<<1];
 void add(int u,int v){
         e[++tot].v=v,e[tot].nxt=head[u],head[u]=tot;
         e[++tot].v=u,e[tot].nxt=head[v],head[v]=tot;
 }*/
priority_queue<pair<int, pa>> heap;
char s[N][N];
int d[N][N];
int n, m, sx, sy, tx, ty;
int x[4] = {1, -1, 0, 0}, y[4] = {0, 0, 1, -1};
int main() {
#ifdef Devil_Gary
  freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
  n = read(), m = read();
  memset(d, 127 / 3, sizeof d);
  sx = read(), sy = read(), tx = read(), ty = read();
  d[sx][sy] = 0;
  for (int i = 1; i <= n; i++)
    cin >> s[i] + 1;
  heap.push(mk(0, mk(sx, sy)));
  while (1) {
    for (; !heap.empty() &&
           d[heap.top().se.fi][heap.top().se.se] != heap.top().fi;
         heap.pop())
      ;
    if (heap.empty())
      break;
    pa now = heap.top().se;
    heap.pop();
    //		cout<<now.fi<<" "<<now.se<<" "<<d[now.fi][now.se]<<endl;
    /*	for(int i=0;i<4;i++){
                    int nx=now.fi+x[i],ny=now.se+y[i];
                    if(nx<1||nx>n||ny<1||ny>m) continue;
//			if(s[now.fi][now.se]=='#'&&s[nx][ny]=='#') continue;
                    int flg=(s[nx][ny]=='#');
                    if(d[nx][ny]>d[now.fi][now.se]+flg){
                            d[nx][ny]=d[now.fi][now.se]+flg;
                            heap.push(mk(d[nx][ny],mk(nx,ny)));
                    }
            }*/
    for (int i = now.fi - 2; i <= now.fi + 2; i++)
      for (int j = now.se - 2; j <= now.se + 2; j++) {
        if (i < 1 || i > n || j < 1 || j > m)
          continue;
        int tmp = abs(i - now.fi) + abs(j - now.se);
        if (tmp == 0)
          continue;
        if (s[i][j] == '#')
          continue;
        int flg = (tmp != 1);
        if (d[i][j] > d[now.fi][now.se] + flg) {
          d[i][j] = d[now.fi][now.se] + flg;
          heap.push(mk(d[i][j], mk(i, j)));
        }
      }
  }
  if (d[tx][ty] == d[0][0])
    puts("-1");
  else
    cout << d[tx][ty] << endl;
  return 0;
}