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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define bug(x) cout << "zdongdebug1: " << x << endl;
#define bug2(x, y) cout << "zdongdebug2: " << x << " " << y << endl;
#define bug3(x, y, z)                                                          \\
  cout << "zdongdebug3: " << x << " " << y << " " << z << endl;
using namespace std;
typedef long long ll;
const int maxn = 1000005;
const int mod = 1000000007;

int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int f0[maxn][4];
int f1[maxn][4];
int g[maxn][4];

int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
  // freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
  int n, m, K;
  cin >> n >> m >> K;
  memset(f0, -1, sizeof(f0));
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> s = vector<string>(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  queue<int> qx, qy, qz;
  for (int i = 0; i < 4; i++) {
    f0[x1 * m + y1][i] = 0;
    f1[x1 * m + y1][i] = 0;
    g[x1 * m + y1][i] = 1;
    qx.push(x1);
    qy.push(y1);
    qz.push(i);
  }
  while (!qx.empty()) {
    int x = qx.front();
    qx.pop();
    int y = qy.front();
    qy.pop();
    int z = qz.front();
    qz.pop();
    int tt = x * m + y;
    g[tt][z] = 0;
    for (int i = 0; i < 4; i++) {
      int nx = x + dir[i][0];
      int ny = y + dir[i][1];
      if (nx < 0 || nx >= n || ny < 0 || ny >= m || s[nx][ny] == '@')
        continue;
      int v0, v1;
      if (i != z) {
        v0 = f0[tt][z] + 1;
        v1 = 1;
      } else {
        v0 = f0[tt][z];
        if (v0 == 0)
          v0 = 1;
        v1 = f1[tt][z] + 1;
        if (v1 == K + 1) {
          v0++;
          v1 = 1;
        }
      }
      int tt2 = nx * m + ny;
      int ov0 = f0[tt2][i];
      int ov1 = f1[tt2][i];
      if (ov0 == -1 || (ov0 > v0) || (ov0 == v0 && v1 < ov1)) {
        f0[tt2][i] = v0;
        f1[tt2][i] = v1;
        if (!g[tt2][i]) {
          g[tt2][i] = 1;
          qx.push(nx);
          qy.push(ny);
          qz.push(i);
        }
      }
    }
  }
  int ret = -1;
  for (int i = 0; i < 4; i++) {
    if (f0[x2 * m + y2][i] != -1) {
      if (ret == -1 || ret > f0[x2 * m + y2][i]) {
        ret = f0[x2 * m + y2][i];
      }
    }
  }
  cout << ret << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define bug(x) cout << "zdongdebug1: " << x << endl;
#define bug2(x, y) cout << "zdongdebug2: " << x << " " << y << endl;
#define bug3(x, y, z)                                                          \\
  cout << "zdongdebug3: " << x << " " << y << " " << z << endl;
using namespace std;
typedef long long ll;
const int maxn = 200005;
const int mod = 1000000007;

int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
  // freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
  int n, m, K;
  cin >> n >> m >> K;
  vector<vector<vector<vector<int>>>> f = vector<vector<vector<vector<int>>>>(
      n, vector<vector<vector<int>>>(
             m, vector<vector<int>>(4, vector<int>(2, -1))));
  vector<vector<vector<int>>> g =
      vector<vector<vector<int>>>(n, vector<vector<int>>(m, vector<int>(4, 0)));
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> s = vector<string>(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  queue<int> qx, qy, qz;
  for (int i = 0; i < 4; i++) {
    f[x1][y1][i][0] = 0;
    f[x1][y1][i][1] = 0;
    g[x1][y1][i] = 1;
    qx.push(x1);
    qy.push(y1);
    qz.push(i);
  }
  while (!qx.empty()) {
    int x = qx.front();
    qx.pop();
    int y = qy.front();
    qy.pop();
    int z = qz.front();
    qz.pop();
    g[x][y][z] = 0;
    for (int i = 0; i < 4; i++) {
      if (i != z)
        continue;
      int nx = x + dir[i][0];
      int ny = y + dir[i][1];
      if (nx < 0 || nx >= n || ny < 0 || ny >= m || s[nx][ny] == '@')
        continue;
      int v0, v1;
      if (i != z) {
        v0 = f[x][y][z][0] + 1;
        v1 = 1;
      } else {
        v0 = f[x][y][z][0];
        if (v0 == 0)
          v0 = 1;
        v1 = f[x][y][z][1] + 1;
        if (v1 == K + 1) {
          v0++;
          v1 = 1;
        }
      }
      int ov0 = f[nx][ny][i][0];
      int ov1 = f[nx][ny][i][1];
      if (ov0 == -1 || (ov0 > v0) || (ov0 == v0 && v1 < ov1)) {
        f[nx][ny][i][0] = v0;
        f[nx][ny][i][1] = v1;
        if (!g[nx][ny][i]) {
          g[nx][ny][i] = 1;
          qx.push(nx);
          qy.push(ny);
          qz.push(i);
        }
      }
    }
    for (int i = 0; i < 4; i++) {
      if (i == z)
        continue;
      int nx = x + dir[i][0];
      int ny = y + dir[i][1];
      if (nx < 0 || nx >= n || ny < 0 || ny >= m || s[nx][ny] == '@')
        continue;
      int v0, v1;
      if (i != z) {
        v0 = f[x][y][z][0] + 1;
        v1 = 1;
      } else {
        v0 = f[x][y][z][0];
        if (v0 == 0)
          v0 = 1;
        v1 = f[x][y][z][1] + 1;
        if (v1 == K + 1) {
          v0++;
          v1 = 1;
        }
      }
      int ov0 = f[nx][ny][i][0];
      int ov1 = f[nx][ny][i][1];
      if (ov0 == -1 || (ov0 > v0) || (ov0 == v0 && v1 < ov1)) {
        f[nx][ny][i][0] = v0;
        f[nx][ny][i][1] = v1;
        if (!g[nx][ny][i]) {
          g[nx][ny][i] = 1;
          qx.push(nx);
          qy.push(ny);
          qz.push(i);
        }
      }
    }
  }
  int ret = -1;
  for (int i = 0; i < 4; i++) {
    if (f[x2][y2][i][0] != -1) {
      if (ret == -1 || ret > f[x2][y2][i][0]) {
        ret = f[x2][y2][i][0];
      }
    }
  }
  cout << ret << endl;
  return 0;
}