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

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI 3.14159265359
using namespace std;
using ll = long long;
using P = pair<int, int>;

int dp[4][1000000];
bool used[4][1000000];
bool field[1000000];

struct cell {
  int dir, loc, d1, d2;
  cell(int d, int l, int dd1, int dd2) {
    dir = d;
    loc = l;
    d1 = dd1;
    d2 = dd2;
  }
};

bool cmp(cell &c1, cell &c2) {
  if (c1.d1 == c2.d1)
    return c1.d2 > c2.d2;
  else
    return c1.d1 > c2.d1;
}

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) {
      if (s[j] == '.')
        field[i * w + j] = false;
      else
        field[i * w + j] = true;
    }
  }
  rep(k, 4) rep(i, h) rep(j, w) {
    used[k][i * w + j] = false;
    dp[k][i * w + j] = -1;
  }

  priority_queue<cell, vector<cell>, decltype(&cmp)> pque(cmp);
  rep(i, 4) { pque.push(cell(i, x1 * w + y1, 0, 0)); }

  while (!pque.empty()) {
    int dir = pque.top().dir;
    int d1 = pque.top().d1, d2 = pque.top().d2;
    int loc = pque.top().loc;
    pque.pop();

    if (used[dir][loc])
      continue;
    dp[dir][loc] = d1 + (d2 > 0 ? 1 : 0);
    used[dir][loc] = true;

    int xx = loc / w, yy = loc % w;

    rep(i, 4) {
      if (i != dir) {
        if (!used[i][loc])
          pque.push(cell(i, loc, d1 + (d2 > 0 ? 1 : 0), 0));
      }
    }

    if (dir == 0 && xx > 0) { // up
      if (!used[0][xx * w + yy - w] && !field[xx * w + yy - w])
        pque.push(
            cell(0, xx * w + yy - w, d1 + (d2 == k - 1 ? 1 : 0), (d2 + 1) % k));
    } else if (dir == 1 && yy < w - 1) { // right
      if (!used[1][xx * w + yy + 1] && !field[xx * w + yy + 1])
        pque.push(
            cell(1, xx * w + yy + 1, d1 + (d2 == k - 1 ? 1 : 0), (d2 + 1) % k));
    } else if (dir == 2 && xx < h - 1) { // down
      if (!used[2][xx * w + yy + w] && !field[xx * w + yy + w])
        pque.push(
            cell(2, xx * w + yy + w, d1 + (d2 == k - 1 ? 1 : 0), (d2 + 1) % k));
    } else if (dir == 3 && yy > 0) { // left
      if (!used[3][xx * w + yy - 1] && !field[xx * w + yy - 1])
        pque.push(
            cell(3, xx * w + yy - 1, d1 + (d2 == k - 1 ? 1 : 0), (d2 + 1) % k));
    }
  }

  int ans = min(dp[0][x2 * w + y2], dp[1][x2 * w + y2]);
  ans = min(ans, dp[2][x2 * w + y2]);
  ans = min(ans, dp[3][x2 * w + y2]);
  cout << ans << endl;

  return 0;
}

/*
6 3
8 1
6 2
9 3
1 1
2 2
1 3
4 3
2 1
1 2

2 2
4208 1234
3056 5678
1 2020
2 2020
*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI 3.14159265359
using namespace std;
using ll = long long;
using P = pair<int, int>;

int dp[4][1000000];
bool used[4][1000000];
bool field[1000000];

struct cell {
  int dir, loc, d1, d2;
  cell(int d, int l, int dd1, int dd2) {
    dir = d;
    loc = l;
    d1 = dd1;
    d2 = dd2;
  }
};

bool cmp(cell &c1, cell &c2) {
  if (c1.d1 == c2.d1)
    return c1.d2 > c2.d2;
  else
    return c1.d1 > c2.d1;
}

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) {
      if (s[j] == '.')
        field[i * w + j] = false;
      else
        field[i * w + j] = true;
    }
  }
  rep(k, 4) rep(i, h) rep(j, w) {
    used[k][i * w + j] = false;
    dp[k][i * w + j] = -1;
  }

  priority_queue<cell, vector<cell>, decltype(&cmp)> pque(cmp);
  rep(i, 4) { pque.push(cell(i, x1 * w + y1, 0, 0)); }
  while (!pque.empty()) {
    int dir = pque.top().dir;
    int d1 = pque.top().d1, d2 = pque.top().d2;
    int loc = pque.top().loc;
    pque.pop();

    dp[dir][loc] = d1 + (d2 > 0 ? 1 : 0);
    used[dir][loc] = true;

    int xx = loc / w, yy = loc % w;

    rep(i, 4) {
      if (i != dir) {
        if (!used[i][loc])
          pque.push(cell(i, loc, d1 + (d2 > 0 ? 1 : 0), 0));
      }
    }

    if (dir == 0 && xx > 0) { // up
      if (!used[0][xx * w + yy - w] && !field[xx * w + yy - w])
        pque.push(
            cell(0, xx * w + yy - w, d1 + (d2 == k - 1 ? 1 : 0), (d2 + 1) % k));
    } else if (dir == 1 && yy < w - 1) { // right
      if (!used[1][xx * w + yy + 1] && !field[xx * w + yy + 1])
        pque.push(
            cell(1, xx * w + yy + 1, d1 + (d2 == k - 1 ? 1 : 0), (d2 + 1) % k));
    } else if (dir == 2 && xx < h - 1) { // down
      if (!used[2][xx * w + yy + w] && !field[xx * w + yy + w])
        pque.push(
            cell(2, xx * w + yy + w, d1 + (d2 == k - 1 ? 1 : 0), (d2 + 1) % k));
    } else if (dir == 3 && yy > 0) { // left
      if (!used[3][xx * w + yy - 1] && !field[xx * w + yy - 1])
        pque.push(
            cell(3, xx * w + yy - 1, d1 + (d2 == k - 1 ? 1 : 0), (d2 + 1) % k));
    }
  }

  int ans = min(dp[0][x2 * w + y2], dp[1][x2 * w + y2]);
  ans = min(ans, dp[2][x2 * w + y2]);
  ans = min(ans, dp[3][x2 * w + y2]);
  cout << ans << endl;

  return 0;
}

/*
6 3
8 1
6 2
9 3
1 1
2 2
1 3
4 3
2 1
1 2

2 2
4208 1234
3056 5678
1 2020
2 2020
*/