Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  char s[h + 2][w + 2];
  int mp[h + 2][w + 2];
  int lf[h + 2][w + 2];
  for (int i = 0; i < h + 2; i++) {
    for (int j = 0; j < w + 2; j++) {
      if (i == 0 || i == h + 1 || j == 0 || j == w + 1) {
        s[i][j] = '#';
      } else {
        cin >> s[i][j];
      }
      mp[i][j] = 999999999;
      lf[i][j] = 0;
    }
  }

  queue<pair<int, int>> q_walk;
  queue<pair<int, int>> q_walked;

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

  int dir_x[] = {1, 0, -1, 0};
  int dir_y[] = {0, 1, 0, -1};
  int ans = -1;
  int wrp_num = 0;
  do {
    while (q_walk.size() != 0) {
      int x, y;
      auto pos = q_walk.front();
      x = pos.first;
      y = pos.second;
      q_walk.pop();

      // cout << "x:" << x << " y:" << y << " wrp:" << wrp_num << endl;
      if (mp[x][y] < wrp_num)
        continue;
      q_walked.push(make_pair(x, y));
      if (x == dh && y == dw) {
        ans = wrp_num;
        break;
      }
      mp[x][y] = wrp_num;
      lf[x][y] = 1;

      for (int i = 0; i < 4; i++) {
        if (s[x + dir_x[i]][y + dir_y[i]] == '.' &&
            lf[x + dir_x[i]][y + dir_y[i]] == 0) {
          q_walk.push(make_pair(x + dir_x[i], y + dir_y[i]));
        }
      }
    }
    if (ans != -1)
      break;

    while (q_walked.size() > 0) {
      auto pos = q_walked.front();
      int x = pos.first;
      int y = pos.second;
      q_walked.pop();
      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          if (x + i < 0 || x + i > h + 1 || y + j < 0 || y + j > w + 1)
            continue;
          if (s[x + i][y + j] == '.' && lf[x + i][y + j] == 0) {
            // cout << "x_i:" << x+i << " y_j:" << y+j << endl;
            q_walk.push(make_pair(x + i, y + j));
            lf[x + i][y + j] = 1;
          }
        }
      }
    }
    wrp_num++;
    if (q_walk.size() == 0)
      break;
  } while (true);

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  char s[h + 2][w + 2];
  int mp[h + 2][w + 2];
  int lf[h + 2][w + 2];
  for (int i = 0; i < h + 2; i++) {
    for (int j = 0; j < w + 2; j++) {
      if (i == 0 || i == h + 1 || j == 0 || j == w + 1) {
        s[i][j] = '#';
      } else {
        cin >> s[i][j];
      }
      mp[i][j] = 999999999;
      lf[i][j] = 0;
    }
  }

  queue<pair<int, int>> q_walk;
  queue<pair<int, int>> q_walked;

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

  int dir_x[] = {1, 0, -1, 0};
  int dir_y[] = {0, 1, 0, -1};
  int ans = -1;
  int wrp_num = 0;
  do {
    while (q_walk.size() != 0) {
      int x, y;
      auto pos = q_walk.front();
      x = pos.first;
      y = pos.second;
      q_walk.pop();

      // cout << "x:" << x << " y:" << y << " wrp:" << wrp_num << endl;
      if (mp[x][y] < wrp_num)
        continue;
      q_walked.push(make_pair(x, y));
      if (x == dh && y == dw) {
        ans = wrp_num;
        break;
      }
      mp[x][y] = wrp_num;
      lf[x][y] = 1;

      for (int i = 0; i < 4; i++) {
        if (s[x + dir_x[i]][y + dir_y[i]] == '.' &&
            lf[x + dir_x[i]][y + dir_y[i]] == 0) {
          q_walk.push(make_pair(x + dir_x[i], y + dir_y[i]));
          lf[x + dir_x[i]][y + dir_y[i]] = 1;
        }
      }
    }
    if (ans != -1)
      break;

    while (q_walked.size() > 0) {
      auto pos = q_walked.front();
      int x = pos.first;
      int y = pos.second;
      q_walked.pop();
      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          if (x + i < 0 || x + i > h + 1 || y + j < 0 || y + j > w + 1)
            continue;
          if (s[x + i][y + j] == '.' && lf[x + i][y + j] == 0) {
            // cout << "x_i:" << x+i << " y_j:" << y+j << endl;
            q_walk.push(make_pair(x + i, y + j));
            lf[x + i][y + j] = 1;
          }
        }
      }
    }
    wrp_num++;
    if (q_walk.size() == 0)
      break;
  } while (true);

  cout << ans << endl;

  return 0;
}