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

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define endl "\\n"
#define mod 1000000007
#define inf 1e18
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)

int32_t main() {

  IOS;

  int h, w;
  pair<int, int> start, target;

  cin >> h >> w;
  cin >> start.first >> start.second >> target.first >> target.second;

  start.first--, start.second--, target.first--, target.second--;

  vector<vector<char>> v(h, vector<char>(w));
  vector<vector<int>> dis(h, vector<int>(w, inf));

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> v[i][j];

  dis[start.first][start.second] = 0;

  deque<pair<int, int>> qq;

  qq.push_front(start);
  pair<int, int> cur;

  vector<pair<int, int>> dir{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

  while (!qq.empty()) {
    auto [x, y] = qq.front();
    qq.pop_front();

    for (auto [dx, dy] : dir) {
      dx += x, dy += y;
      if (dx < 0 || dx == h || dy < 0 || dy == w || v[dx][dy] == '#')
        continue;

      if (dis[x][y] < dis[dx][dy]) {
        dis[dx][dy] = dis[x][y];
        qq.push_front({dx, dy});

        // if (make_pair(dx, dy) == target)
        // 	break;
      }
    }

    int left = max(0ll, y - 2);
    int right = min(w - 1, y + 2);
    int up = max(0ll, x - 2);
    int down = min(h - 1, x + 2);

    for (int i = up; i <= down; i++) {
      for (int j = left; j <= right; j++) {
        if (x == i && y == j || v[i][j] == '#')
          continue;

        if (dis[x][y] + 1 < dis[i][j]) {
          dis[i][j] = dis[x][y] + 1;
          qq.push_back({i, j});
        }
      }
    }
  }

  if (dis[target.first][target.second] == inf)
    cout << "-1";
  else
    cout << dis[target.first][target.second];

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define endl "\\n"
#define mod 1000000007
#define inf 1e18
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)

int32_t main() {

  IOS;

  int h, w;
  pair<int, int> start, target;

  cin >> h >> w;
  cin >> start.first >> start.second >> target.first >> target.second;

  start.first--, start.second--, target.first--, target.second--;

  vector<vector<char>> v(h, vector<char>(w));
  vector<vector<int>> dis(h, vector<int>(w, inf));

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> v[i][j];

  dis[start.first][start.second] = 0;

  deque<pair<int, int>> qq;

  qq.push_front(start);
  pair<int, int> cur;

  vector<pair<int, int>> dir{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

  while (!qq.empty()) {
    auto [x, y] = qq.front();
    qq.pop_front();

    for (auto [dx, dy] : dir) {
      dx += x, dy += y;
      if (dx < 0 || dx == h || dy < 0 || dy == w || v[dx][dy] == '#')
        continue;

      if (dis[x][y] < dis[dx][dy]) {
        dis[dx][dy] = dis[x][y];
        qq.push_front({dx, dy});
      }
    }

    int left = max(0ll, y - 2);
    int right = min(w - 1, y + 2);
    int up = max(0ll, x - 2);
    int down = min(h - 1, x + 2);

    for (int i = up; i <= down; i++) {
      for (int j = left; j <= right; j++) {
        if (x == i && y == j || v[i][j] == '#')
          continue;

        if (dis[x][y] + 1 < dis[i][j]) {
          dis[i][j] = dis[x][y] + 1;
          qq.push_front({i, j});
        }
      }
    }
  }

  if (dis[target.first][target.second] == inf)
    cout << "-1";
  else
    cout << dis[target.first][target.second];

  return 0;
}