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

#include <bits/stdc++.h>
using namespace std;

char c[1001][1001];
int d[1001][1001];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

int main() {
  int n, m, s1, s2, e1, e2;
  scanf("%d%d%d%d%d%d", &n, &m, &e1, &e2, &s1, &s2);
  s1--, s2--, e1--, e2--;
  for (int i = 0; i < n; i++)
    scanf("%s", c[i]);

  queue<int> q, que;

  memset(d, -1, sizeof(d));
  que.push(e1);
  que.push(e2);
  q.push(e1);
  q.push(e2);
  d[e1][e2] = 0;

  while (!q.empty() || !que.empty()) {
    if (!que.empty()) {
      while (!que.empty()) {
        int x = que.front();
        que.pop();
        int y = que.front();
        que.pop();
        for (int i = 0; i < 4; i++) {
          int nx2 = dx[i] + x;
          int ny2 = dy[i] + y;
          if (nx2 < 0 || nx2 >= n || ny2 < 0 || ny2 >= m || c[nx2][ny2] == '#')
            continue;
          if (d[nx2][ny2] == -1 || d[nx2][ny2] > d[x][y]) {
            d[nx2][ny2] = d[x][y];
            que.push(nx2);
            que.push(ny2);
            q.push(nx2);
            q.push(ny2);
          }
        }
      }
    } else {
      int x = q.front();
      q.pop();
      int y = q.front();
      q.pop();
      for (int i = x - 2; i < x + 3; i++) {
        for (int j = y - 2; j < y + 3; j++) {
          if (i < 0 || i >= n || j < 0 || j >= m || (i == x && j == y) ||
              c[i][j] == '#')
            continue;
          if (d[i][j] == -1 || d[i][j] > d[x][y] + 1) {
            d[i][j] = d[x][y] + 1;
            que.push(i);
            que.push(j);
            q.push(i);
            q.push(j);
          }
        }
      }
    }
  }
  printf("%d\\n", d[s1][s2]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define endl '\\n'
#define PB push_back
#define F first
#define S second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define sz(x) (int)x.size()

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

#define mem(a, b) memset(a, b, sizeof(a))
#define sqr(a) ((a) * (a))

#define optimize()                                                             \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fraction()                                                             \\
  cout.unsetf(ios::floatfield);                                                \\
  cout.precision(10);                                                          \\
  cout.setf(ios::fixed, ios::floatfield);
#define file()                                                                 \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);

const int mxN = 1010;

int n, m;
char ch[mxN][mxN];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
ll cnt[mxN][mxN];

void bfs(int sx, int sy) {
  queue<pii> q;
  q.push({sx, sy});
  cnt[sx][sy] = 0;
  while (!q.empty()) {
    int ux = q.front().F;
    int uy = q.front().S;
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int nx = ux + dx[i];
      int ny = uy + dy[i];
      if (nx <= 0 || ny <= 0 || nx > m || ny > n)
        continue;
      if (ch[ny][nx] == '.') {
        if (cnt[nx][ny] > cnt[ux][uy]) {
          cnt[nx][ny] = cnt[ux][uy];
          q.push({nx, ny});
        }
      }
    }
    for (int nx = ux - 2; nx <= ux + 2; ++nx) {
      for (int ny = uy - 2; ny <= uy + 2; ++ny) {
        if (nx <= 0 || ny <= 0 || nx > m || ny > n)
          continue;
        if (ch[ny][nx] == '.') {
          if (cnt[nx][ny] > cnt[ux][uy] + 1) {
            cnt[nx][ny] = cnt[ux][uy] + 1;
            q.push({nx, ny});
          }
        }
      }
    }
  }
}

int main() {
  scanf("%d %d", &n, &m);
  int sx, sy, dx, dy;
  scanf("%d %d %d %d", &sy, &sx, &dy, &dx);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      scanf(" %c", &ch[i][j]);
    }
  }
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= m; ++j) {
      cnt[j][i] = inf;
    }
  }
  bfs(sx, sy);
  printf("%lld\\n", (cnt[dx][dy] == inf ? -1 : cnt[dx][dy]));
  return 0;
}