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

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

vector<int> dx = {-1, 0, 1, 0};
vector<int> dy = {0, 1, 0, -1};

int main() {
  int H, W;
  cin >> H >> W;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<char>> field(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> field.at(i).at(j);
    }
  }
  vector<vector<int>> cost(H, vector<int>(W, 1000000000));
  cost.at(ch).at(cw) = 0;
  deque<pair<int, int>> que;
  que.push_front(make_pair(ch, cw));
  while (!que.empty()) {
    int x = que.front().first;
    int y = que.front().second;
    que.pop_front();
    for (int i = 0; i < 4; i++) {
      int nx = x + dx.at(i);
      int ny = y + dy.at(i);
      if (nx < 0 || nx >= H || ny < 0 || ny >= W) {
        continue;
      } else if (field.at(nx).at(ny) == '#') {
        continue;
      } else if (cost.at(nx).at(ny) > cost.at(x).at(y)) {
        cost.at(nx).at(ny) = cost.at(x).at(y);
        que.push_front(make_pair(nx, ny));
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0) {
          continue;
        }
        int nx = x + i;
        int ny = y + j;
        if (nx < 0 || nx >= H || ny < 0 || ny >= W) {
          continue;
        } else if (field.at(nx).at(ny) == '#') {
          continue;
        } else if (cost.at(nx).at(ny) > cost.at(x).at(y) + 1) {
          cost.at(nx).at(ny) = cost.at(x).at(y) + 1;
          que.push_back(make_pair(nx, ny));
        }
      }
    }
  }
  if (cost.at(dh).at(dw) == 1000000000) {
    cout << -1 << endl;
  } else {
    cout << cost.at(dh).at(dw) << endl;
  }
}

Error: TLE

Fixed submission:

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

vector<vector<int>> Count;
vector<vector<bool>> seen;
int H, W, a, b, c, d;
vector<vector<char>> Data;
queue<pair<int, int>> que;

void bfs(int x, int y) {
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      if (x + i < 0 || x + i >= H || y + j < 0 || y + j >= W) {
        continue;
      } else if (Data.at(x + i).at(y + j) == '#') {
        continue;
      } else {
        if (!seen.at(x + i).at(y + j)) {
          que.push(make_pair(x + i, y + j));
        }
        if ((i == 0 && j == 1) || (i == 0 && j == -1) || (i == -1 && j == 0) ||
            (i == 1 && j == 0)) {
          if (Count.at(x + i).at(y + j) == -1) {
            Count.at(x + i).at(y + j) = Count.at(x).at(y);
          } else {
            Count.at(x + i).at(y + j) =
                min(Count.at(x).at(y), Count.at(x + i).at(y + j));
          }
        } else {
          if (Count.at(x + i).at(y + j) == -1) {
            Count.at(x + i).at(y + j) = Count.at(x).at(y) + 1;
          } else {
            Count.at(x + i).at(y + j) =
                min(Count.at(x).at(y) + 1, Count.at(x + i).at(y + j));
          }
        }
      }
    }
  }
}

int main() {
  cin >> H >> W >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  Data.assign(H, vector<char>(W, '0'));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> Data.at(i).at(j);
    }
  }
  Count.assign(H, vector<int>(W, -1));
  seen.assign(H, vector<bool>(W, false));
  Count.at(a).at(b) = 0;
  que.push(make_pair(a, b));
  while (!que.empty()) {
    int x = que.front().first;
    int y = que.front().second;
    seen.at(x).at(y) = true;
    que.pop();
    bfs(x, y);
  }
  cout << Count.at(c).at(d) << endl;
}