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

#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

bool InBounds(const vector<string> &mat, int x, int y) {
  return 0 <= x && x < (int)mat.size() && 0 <= y && y < (int)mat[x].size();
}

void Fill(const vector<string> &mat, vector<vector<int>> &dp, int x, int y,
          int cost, queue<pair<int, int>> &qq) {
  queue<pair<int, int>> q;
  q.push({x, y});
  dp[x][y] = cost;

  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();

    const vector<pair<int, int>> kMods = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
    for (const auto &mod : kMods) {
      auto nx = x + mod.first;
      auto ny = y + mod.second;

      if (InBounds(mat, nx, ny) && mat[nx][ny] == '.' && dp[nx][ny] > cost) {
        dp[nx][ny] = cost;
        q.push({nx, ny});
        qq.push({nx, ny});
      }
    }
  }
}

int Solve(const vector<string> &mat, int sx, int sy, int dx, int dy) {
  int rows = mat.size();
  int cols = mat[0].size();

  const auto kInf = (1 << 30);
  vector<vector<int>> dp(rows, vector<int>(cols, kInf));
  dp[sx][sy] = 0;

  queue<pair<int, int>> q;
  q.push({sx, sy});

  vector<vector<bool>> in_q(rows, vector<bool>(cols, false));
  in_q[sx][sy] = true;

  while (!q.empty()) {
    auto x = q.front().first;
    auto y = q.front().second;
    q.pop();

    for (int mx = -2; mx <= 2; mx += 1) {
      for (int my = -2; my <= 2; my += 1) {
        auto nx = x + mx;
        auto ny = y + my;
        if (nx < 0 || ny < 0 || rows <= nx || cols <= ny) {
          continue;
        }
        if (mat[nx][ny] != '.') {
          continue;
        }

        auto cost = dp[x][y] + (abs(mx) + abs(my) > 1 ? 1 : 0);
        if (cost < dp[nx][ny]) {
          dp[nx][ny] = cost;
          Fill(mat, dp, nx, ny, cost, q);
          if (!in_q[nx][ny]) {
            q.push({nx, ny});
          }
        }
      }
    }
  }
  return dp[dx][dy] < kInf ? dp[dx][dy] : -1;
}

int main() {
  int rows, cols;
  cin >> rows >> cols;

  int sx, sy;
  cin >> sx >> sy;

  int dx, dy;
  cin >> dx >> dy;
  cin.get();

  vector<string> mat(rows);
  for (auto &row : mat) {
    getline(cin, row);
  }

  auto res = Solve(mat, sx - 1, sy - 1, dx - 1, dy - 1);
  cout << res << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int Solve(const vector<string> &mat, int sx, int sy, int dx, int dy) {
  int rows = mat.size();
  int cols = mat[0].size();

  const auto kInf = (1 << 30);
  vector<vector<int>> dp(rows, vector<int>(cols, kInf));
  dp[sx][sy] = 0;

  queue<pair<int, int>> q;
  q.push({sx, sy});

  vector<vector<bool>> in_q(rows, vector<bool>(cols, false));
  in_q[sx][sy] = true;

  while (!q.empty()) {
    auto x = q.front().first;
    auto y = q.front().second;
    q.pop();

    in_q[x][y] = false;
    if (x == dx && y == dy) {
      continue;
    }

    for (int mx = -2; mx <= 2; mx += 1) {
      for (int my = -2; my <= 2; my += 1) {
        auto nx = x + mx;
        auto ny = y + my;
        if (nx < 0 || ny < 0 || rows <= nx || cols <= ny) {
          continue;
        }
        if (mat[nx][ny] != '.') {
          continue;
        }

        auto cost = dp[x][y] + (abs(mx) + abs(my) > 1 ? 1 : 0);
        if (cost < dp[nx][ny]) {
          dp[nx][ny] = cost;
          if (!in_q[nx][ny]) {
            q.push({nx, ny});
            in_q[nx][ny] = true;
          }
        }
      }
    }
  }
  return dp[dx][dy] < kInf ? dp[dx][dy] : -1;
}

int main() {
  int rows, cols;
  cin >> rows >> cols;

  int sx, sy;
  cin >> sx >> sy;

  int dx, dy;
  cin >> dx >> dy;
  cin.get();

  vector<string> mat(rows);
  for (auto &row : mat) {
    getline(cin, row);
  }

  auto res = Solve(mat, sx - 1, sy - 1, dx - 1, dy - 1);
  cout << res << "\\n";

  return 0;
}