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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const long double pi = 3.141592653589793;

#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, j, n) for (int i = j; i < n; i++)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define F first
#define S second

// #define COMMENT

/*

const int NN = 200090;
ll fact[NN];
ll finv[NN];
ll inv[NN];

void inverse_modulao() {
  fact[0] = fact[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;

  for(int i = 2;i < NN;i++) {
    fact[i] = fact[i-1]*i%mod;
    inv[i] = mod-mod/i*inv[mod%i]%mod;
    finv[i] = finv[i-1]*inv[i]%mod;
  }
}

ll nCr(int n, int r) {
  return fact[n]*finv[r]%mod*finv[n-r]%mod;
}

*/

int n, m, k, xi, yi, xj, yj;
string s[1 << 20];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

#define dist_state pair<int, pair<pii, int>>

int dijkstra() {

  const int INF = 1e8;
  vector<vector<vector<int>>> dist = vector<vector<vector<int>>>(
      n + 3, vector<vector<int>>(m + 3, vector<int>(6, INF)));

  set<pair<int, pair<pii, int>>> pq;
  rep(i, 4) {
    pq.insert({0, {{xi, yi}, i}});
    dist[xi][yi][i] = 0;
  }

  while (!pq.empty()) {
    dist_state ds = *pq.begin();
    pq.erase(pq.begin());

    int dxs = 4;
    for (int i = 0; i < dxs; i++) {
      if (!(dx[i] + dx[ds.S.S] == 0 && dy[i] + dy[ds.S.S] == 0)) {
        int x = ds.S.F.F + dx[i], y = ds.S.F.S + dy[i], cnt = 1;
        while (x >= 0 && y >= 0 && x < n && y < m && cnt <= k &&
               s[x][y] == '.') {
          if ((ds.F + 1) < dist[x][y][i]) {
            pq.erase({dist[x][y][i], {{x, y}, i}});
            pq.insert({1 + ds.F, {{x, y}, i}});
            dist[x][y][i] = 1 + ds.F;
          } else if ((ds.F + 1) > dist[x][y][i])
            break;
          cnt++;
          x += dx[i];
          y += dy[i];
        }
      }
    }
  }

  int ans = INF;
  rep(i, 4) ans = min(ans, dist[xj][yj][i]);
  return (ans == INF ? -1 : ans);
}

int main() {

  // freopen("input.in","r",stdin);
  // freopen("output.out","w",stdout);
  cin >> n >> m >> k;
  cin >> xi >> yi >> xj >> yj;
  xi--;
  yi--;
  xj--;
  yj--;
  rep(i, n) cin >> s[i];

  cout << dijkstra() << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const long double pi = 3.141592653589793;

#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, j, n) for (int i = j; i < n; i++)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define F first
#define S second

// #define COMMENT

/*

const int NN = 200090;
ll fact[NN];
ll finv[NN];
ll inv[NN];

void inverse_modulao() {
  fact[0] = fact[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;

  for(int i = 2;i < NN;i++) {
    fact[i] = fact[i-1]*i%mod;
    inv[i] = mod-mod/i*inv[mod%i]%mod;
    finv[i] = finv[i-1]*inv[i]%mod;
  }
}

ll nCr(int n, int r) {
  return fact[n]*finv[r]%mod*finv[n-r]%mod;
}

*/

int n, m, k, xi, yi, xj, yj;
string s[1 << 20];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

#define dist_state pair<int, pair<pii, int>>

int dijkstra() {

  const int INF = 1e8;
  vector<vector<vector<int>>> dist = vector<vector<vector<int>>>(
      n + 3, vector<vector<int>>(m + 3, vector<int>(6, INF)));

  set<pair<int, pair<pii, int>>> pq;
  rep(i, 4) {
    pq.insert({0, {{xi, yi}, i}});
    dist[xi][yi][i] = 0;
  }

  while (!pq.empty()) {
    dist_state ds = *pq.begin();
    pq.erase(pq.begin());

    int dxs = 4;
    for (int i = 0; i < dxs; i++) {
      if (!(dx[i] + dx[ds.S.S] == 0 && dy[i] + dy[ds.S.S] == 0)) {
        int x = ds.S.F.F + dx[i], y = ds.S.F.S + dy[i], cnt = 1;
        while (x >= 0 && y >= 0 && x < n && y < m && cnt <= k &&
               s[x][y] == '.' && (ds.F + 1) <= dist[x][y][i]) {
          pq.erase({dist[x][y][i], {{x, y}, i}});
          pq.insert({1 + ds.F, {{x, y}, i}});
          dist[x][y][i] = 1 + ds.F;
          cnt++;
          x += dx[i];
          y += dy[i];
        }
      }
    }
  }

  int ans = INF;
  rep(i, 4) ans = min(ans, dist[xj][yj][i]);
  return (ans == INF ? -1 : ans);
}

int main() {

  // freopen("input.in","r",stdin);
  // freopen("output.out","w",stdout);
  cin >> n >> m >> k;
  cin >> xi >> yi >> xj >> yj;
  xi--;
  yi--;
  xj--;
  yj--;
  rep(i, n) cin >> s[i];

  cout << dijkstra() << endl;

  return 0;
}