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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define faster ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define read freopen("in.txt", "r", stdin)
#define write freopen("out.txt", "w", stdout)
#define mem(x, n) memset(x, n, sizeof(x))
#define all(x) x.begin(), x.end()
#define endl "\\n"

int main() {
  //	faster;

  int h, w;
  cin >> h >> w;
  int ch, dh, cw, dw;
  cin >> ch >> dh >> cw >> dw;

  char g[h + 1][w + 1];
  int cost[h + 1][w + 1], idx[] = {-1, 1, 0, 0}, idy[] = {0, 0, -1, 1};
  bool vis[h + 1][w + 1];

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> g[i][j];
    }
  }

  mem(vis, false);
  mem(cost, -1);

  deque<pair<int, int>> q;
  q.push_back({ch, dh});
  cost[ch][dh] = 0;
  vis[ch][dh] = true;

  while (!q.empty()) {
    auto curr = q.front();
    ll curr_cost = cost[curr.first][curr.second];
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int x = curr.first + idx[i], y = curr.second + idy[i];
      if (x > 0 && y > 0 && x <= h && y <= w && g[x][y] == '.') {
        if (cost[x][y] > curr_cost || cost[x][y] == -1) {
          vis[x][y] = true;
          cost[x][y] = curr_cost;
          q.push_front({x, y});
        }
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int x = curr.first + i, y = curr.second + j;
        if (x > 0 && y > 0 && x <= h && y <= w && g[x][y] == '.') {
          if (cost[x][y] > (curr_cost + 1) || cost[x][y] == -1) {
            vis[x][y] = true;
            cost[x][y] = curr_cost + 1;
            q.push_back({x, y});
          }
        }
      }
    }
  }

  cout << cost[cw][dw] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define faster ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define read freopen("in.txt", "r", stdin)
#define write freopen("out.txt", "w", stdout)
#define mem(x, n) memset(x, n, sizeof(x))
#define all(x) x.begin(), x.end()
#define endl "\\n"

int main() {
  //	faster;

  int h, w;
  cin >> h >> w;
  int ch, dh, cw, dw;
  cin >> ch >> dh >> cw >> dw;

  char g[h + 1][w + 1];
  int cost[h + 1][w + 1], idx[] = {-1, 1, 0, 0}, idy[] = {0, 0, -1, 1};
  bool vis[h + 1][w + 1];

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> g[i][j];
    }
  }

  mem(vis, false);
  mem(cost, -1);

  queue<pair<int, int>> q;
  q.push({ch, dh});
  cost[ch][dh] = 0;
  vis[ch][dh] = true;

  while (!q.empty()) {
    auto curr = q.front();
    ll curr_cost = cost[curr.first][curr.second];
    q.pop();
    for (int i = 0; i < 4; i++) {
      int x = curr.first + idx[i], y = curr.second + idy[i];
      if (x > 0 && y > 0 && x <= h && y <= w && g[x][y] == '.') {
        if (cost[x][y] > curr_cost || cost[x][y] == -1) {
          vis[x][y] = true;
          cost[x][y] = curr_cost;
          q.push({x, y});
        }
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int x = curr.first + i, y = curr.second + j;
        if (x > 0 && y > 0 && x <= h && y <= w && g[x][y] == '.') {
          if (cost[x][y] > (curr_cost + 1) || cost[x][y] == -1) {
            vis[x][y] = true;
            cost[x][y] = curr_cost + 1;
            q.push({x, y});
          }
        }
      }
    }
  }

  cout << cost[cw][dw] << endl;

  return 0;
}