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

// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define io ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e3 + 10;
char s[N][N];
int d[N][N];
struct node {
  int x, y, d;
};
int n, m, sx, sy, ex, ey;
bool check(int i, int j) { return i >= 1 && i <= n && j >= 1 && j <= m; }
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
void solve() {
  // cin>>n>>m>>sx>>sy>>ex>>ey;
  scanf("%d%d%d%d%d%d", &n, &m, &sx, &sy, &ex, &ey);
  for (int i = 1; i <= n; i++)
    scanf("%s", s[i] + 1);
  deque<node> q;
  memset(d, inf, sizeof(d));
  q.push_front(node{sx, sy, 0});
  d[sx][sy] = 0;
  while (q.size()) {
    node p = q.front();
    q.pop_front();
    if (p.x == ex && p.y == ey) {
      break;
    }
    for (int k = 0; k < 4; k++) {
      int x = p.x + dx[k], y = p.y + dy[k];
      if (!check(x, y))
        continue;
      if (s[x][y] == '#')
        continue;
      if (d[x][y] > d[p.x][p.y]) {
        q.push_front(node{x, y, p.d});
        d[x][y] = d[p.x][p.y];
      }
    }
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        int ny = p.y - 2 + j, nx = p.x - 2 + i;
        if (!check(nx, ny))
          continue;
        if (s[nx][ny] == '#')
          continue;
        if (d[nx][ny] > d[p.x][p.y] + 1) {
          d[nx][ny] = d[p.x][p.y] + 1;
          q.push_back(node{nx, ny, 0});
        }
      }
    }
  }
  if (d[ex][ey] == inf)
    cout << -1;
  else
    cout << d[ex][ey];
}
int main() {
  // io;
  // int T;cin>>T;
  // while(T--)
  solve();
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <utility>
#define io ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e3 + 10;
char s[N][N];
int vis[N][N], d[N][N];
struct node {
  int x, y, d;
};
int n, m, sx, sy, ex, ey;
bool check(int i, int j) { return i >= 1 && i <= n && j >= 1 && j <= m; }
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
void solve() {
  // cin>>n>>m>>sx>>sy>>ex>>ey;
  scanf("%d%d%d%d%d%d", &n, &m, &sx, &sy, &ex, &ey);
  for (int i = 1; i <= n; i++)
    scanf("%s", s[i] + 1);
  queue<node> q;
  memset(d, inf, sizeof(d));
  q.push(node{sx, sy, 0});
  d[sx][sy] = 0;
  vis[sx][sy] = 1;
  while (q.size()) {
    node p = q.front();
    q.pop();
    vis[p.x][p.y] = 1;
    if (p.x == ex && p.y == ey) {
      printf("%d", d[ex][ey]);
      return;
    }
    int f = 0;
    for (int k = 0; k < 4; k++) {
      int x = p.x + dx[k], y = p.y + dy[k];
      if (check(x, y) && s[x][y] != '#' && !vis[x][y]) {
        q.push(node{x, y, p.d});
        f = 1;
        d[x][y] = min(d[x][y], d[p.x][p.y]);
      }
    }
    int ok = 0;
    for (int i = p.x - 2; i <= p.x + 2; i++) {
      for (int j = p.y - 2; j <= p.y + 2; j++) {
        if (!check(i, j))
          continue;
        if (s[i][j] == '#' || vis[i][j])
          continue;
        d[i][j] = min(d[i][j], d[p.x][p.y] + 1);
        ok = 1;
        q.push(node{i, j, p.d + 1});
      }
    }
  }
  cout << -1;
}
int main() {
  // io;
  // int T;cin>>T;
  // while(T--)
  solve();
}