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


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

using ll = long long;
using ld = long double;

template <typename T> inline T mini(T &x, T y) { return x = min(x, y); }
template <typename T> inline T maxi(T &x, T y) { return x = max(x, y); }
template <typename T> T rand(T x, T y) { return rand() % (y - x + 1) + x; }
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

class DWizardInMaze {
public:
  void solve(std::istream &cin, std::ostream &cout) {
    int n, m;
    cin >> n >> m;
    pair<int, int> s, e;
    cin >> s.first >> s.second;
    cin >> e.first >> e.second;
    s.first--, s.second--;
    e.first--, e.second--;

    vector<string> a(n);
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
    }
    vector<vector<int>> vis(n, vector<int>(m, 0));
    auto ok = [&](int x, int y) -> bool {
      if (x >= 0 and y >= 0 and x < n and y < m)
        return a[x][y] == '.';
      return false;
    };
    deque<pair<int, int>> q;
    vis[s.first][s.second] = 1;
    q.emplace_back(s.first, s.second);
    while (!q.empty()) {
      auto [x, y] = q.front();
      q.pop_front();
      for (int i = 0; i < 4; ++i) {
        int xx = x + dx[i];
        int yy = y + dy[i];
        if (ok(xx, yy) && (!vis[xx][yy] || vis[x][y] < vis[xx][yy])) {
          vis[xx][yy] = vis[x][y];
          q.emplace_front(xx, yy);
        }
      }
      for (int i = x - 2; i <= x + 2; ++i) {
        for (int j = y - 2; j <= y + 2; ++j) {
          if (ok(i, j) && !vis[i][j]) {
            vis[i][j] = vis[x][y] + 1;
            q.emplace_back(i, j);
          }
        }
      }
    }
    int ans = vis[e.first][e.second];
    cout << (ans ? ans - 1 : -1) << '\\n';
  }
};

int32_t main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  DWizardInMaze solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Error: TLE

Fixed submission:


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

using ll = long long;
using ld = long double;

template <typename T> inline T mini(T &x, T y) { return x = min(x, y); }
template <typename T> inline T maxi(T &x, T y) { return x = max(x, y); }
template <typename T> T rand(T x, T y) { return rand() % (y - x + 1) + x; }
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int dx2[] = {-2, -1, 1, 2, -2, -1, 1, 2, 2, -2, 2, -2};
int dy2[] = {-1, -2, -2, -1, 1, 2, 2, 1, 2, -2, -2, 2};

using pii = pair<int, int>;

class DWizardInMaze {
public:
  void solve(std::istream &cin, std::ostream &cout) {
    int n, m;
    cin >> n >> m;
    pair<int, int> s, e;
    cin >> s.first >> s.second;
    cin >> e.first >> e.second;
    s.first--, s.second--;
    e.first--, e.second--;
    vector<string> a(n);
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
    }
    auto ok = [&](int x, int y) -> bool {
      if (x >= 0 and y >= 0 and x < n and y < m) {
        return a[x][y] == '.';
      }
      return false;
    };
    priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<>> q;
    q.push({0, s});
    int ans = n * m;
    while (!q.empty()) {
      auto tp = q.top();
      q.pop();
      if (tp.second == e) {
        mini(ans, tp.first);
      }
      for (int i = 0; i < 4; ++i) {
        int xx = tp.second.first + dx[i];
        int yy = tp.second.second + dy[i];
        if (ok(xx, yy)) {
          q.push({tp.first, {xx, yy}});
        }
      }
      for (int dx1 = -2; dx1 <= 2; ++dx1) {
        for (int dy1 = -2; dy1 <= 2; ++dy1) {
          if (dx1 == 0 and dy1 == 0)
            continue;
          int xx = tp.second.first + dx1;
          int yy = tp.second.second + dy1;
          if (ok(xx, yy)) {
            q.push({tp.first + 1, {xx, yy}});
          }
        }
      }
      a[tp.second.first][tp.second.second] = 'x';
    }
    cout << (ans == n * m ? -1 : ans) << '\\n';
  }
};

int32_t main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  DWizardInMaze solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}