Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e3 + 7;
int h, w, ch, cw, dh, dw;
char grid[N][N];
int visited[N][N];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> h >> w >> ch >> cw >> dh >> dw;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      cin >> grid[i][j];

  queue<tuple<int, int, int>> bfs_curr;
  queue<tuple<int, int, int>> bfs_next;

  bfs_curr.push({ch, cw, 0});
  visited[ch][cw] = 1;

  while (!bfs_curr.empty()) {
    int x, y, dist;
    tie(x, y, dist) = bfs_curr.front();
    if (x == dh && y == dw)
      return cout << dist << "\n", 0;
    bfs_curr.pop();
    if (x > 1 && grid[x - 1][y] == '.' && !visited[x - 1][y])
      bfs_curr.push({x - 1, y, dist}), visited[x - 1][y] = 1;
    if (x < h && grid[x + 1][y] == '.' && !visited[x + 1][y])
      bfs_curr.push({x + 1, y, dist}), visited[x + 1][y] = 1;
    if (y > 1 && grid[x][y - 1] == '.' && !visited[x][y - 1])
      bfs_curr.push({x, y - 1, dist}), visited[x][y - 1] = 1;
    if (y < w && grid[x][y + 1] == '.' && !visited[x][y + 1])
      bfs_curr.push({x, y + 1, dist}), visited[x][y + 1] = 1;

    for (int i = x - 2; i <= x + 2; i++)
      for (int j = y - 2; j <= y + 2; j++)
        if (i >= 1 && i <= h && j >= 1 && j <= w && !visited[i][j] &&
            grid[i][j] == '.')
          bfs_next.push({i, j, dist + 1});

    if (bfs_curr.empty()) {
      bfs_curr = bfs_next;
      bfs_next = queue<tuple<int, int, int>>();
    }
  }

  cout << -1;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e3 + 7;
int h, w, ch, cw, dh, dw;
char grid[N][N];
int visited[N][N];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> h >> w >> ch >> cw >> dh >> dw;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      cin >> grid[i][j];

  queue<tuple<int, int, int>> bfs_curr;
  queue<tuple<int, int, int>> bfs_next;

  bfs_curr.push({ch, cw, 0});
  visited[ch][cw] = 1;

  while (!bfs_curr.empty()) {
    int x, y, dist;
    tie(x, y, dist) = bfs_curr.front();
    if (x == dh && y == dw)
      return cout << dist << "\n", 0;
    bfs_curr.pop();
    if (x > 1 && grid[x - 1][y] == '.' && !visited[x - 1][y])
      bfs_curr.push({x - 1, y, dist}), visited[x - 1][y] = 1;
    if (x < h && grid[x + 1][y] == '.' && !visited[x + 1][y])
      bfs_curr.push({x + 1, y, dist}), visited[x + 1][y] = 1;
    if (y > 1 && grid[x][y - 1] == '.' && !visited[x][y - 1])
      bfs_curr.push({x, y - 1, dist}), visited[x][y - 1] = 1;
    if (y < w && grid[x][y + 1] == '.' && !visited[x][y + 1])
      bfs_curr.push({x, y + 1, dist}), visited[x][y + 1] = 1;

    for (int i = x - 2; i <= x + 2; i++)
      for (int j = y - 2; j <= y + 2; j++)
        if (i >= 1 && i <= h && j >= 1 && j <= w && !visited[i][j] &&
            grid[i][j] == '.')
          bfs_next.push({i, j, dist + 1});

    if (bfs_curr.empty()) {
      while (!bfs_next.empty()) {
        tie(x, y, dist) = bfs_next.front();
        visited[x][y] = 1;
        bfs_curr.push({x, y, dist});
        bfs_next.pop();
      }
    }
  }

  cout << -1;

  return 0;
}