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

#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const ll N = 1e3 + 5;

int n, m, sx, sy, ex, ey;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int vis[N][N];
char a[N][N];

struct node {
  int x, y, z;
  bool operator<(const node &T) const { return z > T.z; }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m >> sx >> sy >> ex >> ey;
  for (int i = 1; i <= n; i++)
    cin >> a[i] + 1;
  priority_queue<node> q;
  memset(vis, -1, sizeof(vis));
  vis[sx][sy] = 0;
  q.push({sx, sy, 0});
  while (q.size()) {
    node t = q.top();
    q.pop();
    // cout<<"t.x="<<t.x<<" t.y="<<t.y<<" t.z="<<t.z<<'\\n';
    if (t.z >= vis[ex][ey] and vis[ex][ey] != -1)
      break;
    // if(t.x==ex and t.y==ey){cout<<t.z<<'\\n';return 0;}
    int x = t.x, y = t.y, z = t.z;
    for (int i = 0; i < 4; i++) {
      int tx = x + dx[i];
      int ty = y + dy[i];
      // cout<<"tx="<<tx<<" ty="<<ty<<"
      // vis["<<tx<<"]["<<ty<<"]="<<vis[tx][ty]<<"
      // a["<<tx<<"]["<<ty<<"]="<<a[tx][ty]<<'\\n';
      if (tx >= 1 and tx <= n and ty >= 1 and ty <= m and a[tx][ty] == '.' and
          (vis[tx][ty] == -1 || z < vis[tx][ty])) {
        vis[tx][ty] = z;
        q.push({tx, ty, z});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int tx = x + i;
        int ty = y + j;
        if (tx >= 1 and tx <= n and ty >= 1 and ty <= m and a[tx][ty] == '.' and
            (vis[tx][ty] == -1 || z + 1 < vis[tx][ty])) {
          vis[tx][ty] = z + 1;
          q.push({tx, ty, z + 1});
        }
      }
    }
  }
  cout << vis[ex][ey] << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma optimize(2)
#define fi first
#define se second
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const ll N = 1e3 + 5;

int n, m, sx, sy, ex, ey;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
char a[N][N];
int vis[N][N];

void dfs(int x, int y, int z, int p) {
  if (z >= vis[ex][ey] && vis[ex][ey] != -1)
    return;
  if (z > p)
    return;
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      int tx = x + i;
      int ty = y + j;
      int tz = z;
      if (abs(i) + abs(j) > 1)
        tz++;
      if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && a[tx][ty] == '.' &&
          (vis[tx][ty] > tz || vis[tx][ty] == -1) && tz <= p) {
        vis[tx][ty] = tz;
        dfs(tx, ty, tz, p);
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m;
  cin >> sx >> sy >> ex >> ey;
  for (int i = 1; i <= n; i++)
    cin >> a[i] + 1;
  // memset(vis,-1,sizeof(vis));
  // bool ok=true;
  // for(int i=-2;i<=2;i++){
  // 	for(int j=-2;j<=2;j++){
  // 		int tx=ex+i;
  // 		int ty=ey+j;
  // 		if(a[tx][ty]=='#' || tx<1 || tx>n || ty<1 || ty>m) continue;
  // 		else{ok=false;break;}
  // 	}
  // }
  // for(int i=-2;i<=2;i++){
  // 	for(int j=-2;j<=2;j++){
  // 		int tx=sx+i;
  // 		int ty=sy+j;
  // 		if(a[tx][ty]=='#' || tx<1 || tx>n || ty<1 || ty>m) continue;
  // 		else{ok=false;break;}
  // 	}
  // }
  // if(ok) return cout<<"-1\\n",0;
  // cout<<"debug\\n";
  for (int i = 0; i <= 10000; i++) {
    memset(vis, -1, sizeof(vis));
    vis[sx][sy] = 0;
    dfs(sx, sy, 0, i);
    if (vis[ex][ey] != -1) {
      cout << i << '\\n';
      return 0;
    }
  }
  cout << -1 << '\\n';
  return 0;
}