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

#include <cstdio>
#include <fstream>
#include <iostream>

#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iterator>
#include <utility>

#include <cassert>
#include <chrono>
#include <random>

#include <ccomplex>

using namespace std;

#define ll long long
#define prll pair<ll, ll>
#define m_p make_pair
#define INF (ll)1e18

const ll q = 239017;
const ll mod = 1e9 + 7;
const ll mod2 = 1e9 + 13;
const ll N = 2e5 + 100;
const ll M = 100;
const ll L = 26;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define T pair<ll, pair<prll, prll>>

struct cmp {
  bool operator()(T &a, T &b) { return a.first > b.first; }
};

vector<vector<ll>> mt, ds[4];

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

void $main() {
  ll n, m, r;
  cin >> n >> m >> r;
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  --a;
  --b;
  --c;
  --d;

  for (ll i = 0; i < 4; ++i)
    ds[i].resize(n);

  mt.resize(n);
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < 4; ++j)
      ds[j][i].resize(m);

    mt[i].resize(m);
    string s;
    cin >> s;
    for (ll j = 0; j < m; ++j)
      mt[i][j] = s[j] == '.';
  }

  for (ll k = 0; k < 4; ++k)
    for (ll i = 0; i < n; ++i)
      for (ll j = 0; j < m; ++j)
        ds[k][i][j] = INF;

  for (ll k = 0; k < 4; ++k)
    ds[k][a][b] = 0;

  priority_queue<T, vector<T>, cmp> qr;
  qr.emplace(0, m_p(m_p(a, b), m_p(0, -1)));

  while (!qr.empty()) {
    auto u = qr.top();

    ll our = u.first;
    ll i = u.second.first.first;
    ll j = u.second.first.second;
    ll streak = u.second.second.first;
    ll t = u.second.second.second;

    if (streak == r) {
      t = -1;
      streak = 0;
    }

    qr.pop();

    for (ll k = 0; k < 4; ++k) {
      ll x = i + dx[k];
      ll y = j + dy[k];

      ll strk = streak, cost = our + (!strk);

      if (t == -1 || k == t)
        ++strk;
      else {
        ++cost;
        strk = 1;
      }

      if (x < 0 || y < 0 || x >= n || y >= m || !mt[x][y])
        continue;

      for (ll p = 0; p < 4; ++p) {
        if (p != k || ds[p][x][y] <= cost)
          continue;

        ds[p][x][y] = cost;
        qr.emplace(cost, m_p(m_p(x, y), m_p(strk, k)));
      }
    }
  }

  ll ans = INF;
  for (ll p = 0; p < 4; ++p)
    ans = min(ans, ds[p][c][d]);

  cout << (ans == INF ? -1 : ans);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#else
  // freopen("champions", "r", stdin);
  // freopen("club", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  $main();

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <fstream>
#include <iostream>

#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iterator>
#include <utility>

#include <cassert>
#include <chrono>
#include <random>

#include <ccomplex>

using namespace std;

#define ll long long
#define prll pair<ll, ll>
#define m_p make_pair
#define INF (ll)1e18

const ll q = 239017;
const ll mod = 1e9 + 7;
const ll mod2 = 1e9 + 13;
const ll N = 2e5 + 100;
const ll M = 100;
const ll L = 26;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define T pair<ll, pair<prll, prll>>

struct cmp {
  bool operator()(T &a, T &b) { return a.first < b.first; }
};

vector<vector<ll>> mt, ds[4];

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

void $main() {
  ll n, m, r;
  cin >> n >> m >> r;
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  --a;
  --b;
  --c;
  --d;

  for (ll i = 0; i < 4; ++i)
    ds[i].resize(n);

  mt.resize(n);
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < 4; ++j)
      ds[j][i].resize(m);

    mt[i].resize(m);
    string s;
    cin >> s;
    for (ll j = 0; j < m; ++j)
      mt[i][j] = s[j] == '.';
  }

  for (ll k = 0; k < 4; ++k)
    for (ll i = 0; i < n; ++i)
      for (ll j = 0; j < m; ++j)
        ds[k][i][j] = INF;

  for (ll k = 0; k < 4; ++k)
    ds[k][a][b] = 0;

  priority_queue<T, vector<T>, cmp> qr;
  qr.emplace(0, m_p(m_p(a, b), m_p(0, -1)));

  while (!qr.empty()) {
    auto u = qr.top();

    ll i = u.second.first.first;
    ll j = u.second.first.second;
    ll streak = u.second.second.first;
    ll t = u.second.second.second;

    if (streak == r) {
      t = -1;
      streak = 0;
    }

    qr.pop();

    for (ll k = 0; k < 4; ++k) {
      ll x = i + dx[k];
      ll y = j + dy[k];

      ll strk = streak;

      ll cost = INF;
      if (t == -1)
        for (ll p = 0; p < 4; ++p)
          cost = min(cost, ds[p][i][j]);
      else
        cost = ds[t][i][j];

      cost += (!strk);

      if (t == -1 || k == t)
        ++strk;
      else {
        ++cost;
        strk = 1;
      }

      if (x < 0 || y < 0 || x >= n || y >= m || !mt[x][y])
        continue;

      for (ll p = 0; p < 4; ++p) {
        if (p != k || ds[p][x][y] <= cost)
          continue;

        ds[p][x][y] = cost;
        qr.emplace(cost, m_p(m_p(x, y), m_p(strk, k)));
      }
    }
  }

  ll ans = INF;
  for (ll p = 0; p < 4; ++p)
    ans = min(ans, ds[p][c][d]);

  cout << (ans == INF ? -1 : ans);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#else
  // freopen("champions", "r", stdin);
  // freopen("club", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  $main();

  return 0;
}