Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define PI 3.14159265359
typedef long long ll;
const int MOD = 1e9 + 7;
const ll LLINF = 1e18;
using namespace std;

ll llmin(ll x, ll y) {
  if (x < y)
    return x;
  return y;
}

ll llmax(ll x, ll y) {
  if (x > y)
    return x;
  return y;
}

char grid[1001][1001];
bool visited[1001][1001];
queue<pair<int, int>> que;
queue<pair<int, int>> que2;
int h, w, ans;
int ch, cw, gh, gw;
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};

bool is_range(int nowx, int nowy) {
  if (nowx <= h && nowx >= 0 && nowy <= w && nowy >= 0)
    return true;
  return false;
}

int main() {
  cin >> h >> w;
  cin >> ch >> cw >> gh >> gw;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> grid[i][j];
    }
  }

  que.push(make_pair(ch, cw));

  while (1) {
    if (que.empty() && que2.empty()) {
      cout << -1 << endl;
      return 0;
    }
    while (!que.empty()) {
      pair<int, int> p = que.front();
      que.pop();
      int nx = p.first, ny = p.second;
      visited[nx][ny] = true;
      que2.push(make_pair(nx, ny));
      if (nx == gh && ny == gw) {
        cout << ans << endl;
        return 0;
      }
      for (int i = 0; i < 4; i++) {
        if (is_range(nx + dx[i], ny + dy[i])) {
          if (!visited[nx + dx[i]][ny + dy[i]] &&
              grid[nx + dx[i]][ny + dy[i]] == '.') {
            visited[nx + dx[i]][ny + dy[i]] = true;
            que.push(make_pair(nx + dx[i], ny + dy[i]));
            que2.push(make_pair(nx + dx[i], ny + dy[i]));
          }
        }
      }
    }
    while (!que2.empty()) {
      pair<int, int> p = que2.front();
      que2.pop();
      int nx = p.first, ny = p.second;
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          if (is_range(nx + i, ny + j)) {
            if (!visited[nx + i][ny + j] && grid[nx + i][ny + j] == '.') {
              que.push(make_pair(nx + i, ny + j));
            }
          }
        }
      }
    }
    ans++;
    for (int i = 0; i <= 1000; i++) {
      for (int j = 0; j <= 1000; j++) {
        visited[i][j] = false;
      }
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define PI 3.14159265359
typedef long long ll;
const int MOD = 1e9 + 7;
const ll LLINF = 1e18;
using namespace std;

ll llmin(ll x, ll y) {
  if (x < y)
    return x;
  return y;
}

ll llmax(ll x, ll y) {
  if (x > y)
    return x;
  return y;
}

char grid[1001][1001];
bool visited[1001][1001];
queue<pair<int, int>> que;
queue<pair<int, int>> que2;
int h, w, ans;
int ch, cw, gh, gw;
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};

bool is_range(int nowx, int nowy) {
  if (nowx <= h && nowx >= 0 && nowy <= w && nowy >= 0)
    return true;
  return false;
}

int main() {
  cin >> h >> w;
  cin >> ch >> cw >> gh >> gw;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> grid[i][j];
    }
  }

  que.push(make_pair(ch, cw));

  while (1) {
    if (que.empty() && que2.empty()) {
      cout << -1 << endl;
      return 0;
    }
    while (!que.empty()) {
      pair<int, int> p = que.front();
      que.pop();
      int nx = p.first, ny = p.second;
      visited[nx][ny] = true;
      que2.push(make_pair(nx, ny));
      if (nx == gh && ny == gw) {
        cout << ans << endl;
        return 0;
      }
      for (int i = 0; i < 4; i++) {
        if (is_range(nx + dx[i], ny + dy[i])) {
          if (!visited[nx + dx[i]][ny + dy[i]] &&
              grid[nx + dx[i]][ny + dy[i]] == '.') {
            visited[nx + dx[i]][ny + dy[i]] = true;
            que.push(make_pair(nx + dx[i], ny + dy[i]));
            que2.push(make_pair(nx + dx[i], ny + dy[i]));
          }
        }
      }
    }
    while (!que2.empty()) {
      pair<int, int> p = que2.front();
      que2.pop();
      int nx = p.first, ny = p.second;
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          if (is_range(nx + i, ny + j)) {
            if (!visited[nx + i][ny + j] && grid[nx + i][ny + j] == '.') {
              que.push(make_pair(nx + i, ny + j));
            }
          }
        }
      }
    }
    ans++;
  }

  return 0;
}