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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int convdir(int direction, int width) {
  switch (direction) {
  case 0:
    return -4;
  case 1:
    return 0 - 4 * width;
  case 2:
    return 4;
  default:
    return 4 * width;
  }
}

int main(void) {
  int H, W, K, x1, y1, x2, y2;
  char cbuf;
  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<vector<char>> c(H, vector<char>(W, ' '));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> cbuf;
      c[i][j] = cbuf;
    }
  }
  vector<int> Cost(H * W * 4, -1);
  Cost[4 * (x1 * W + y1)] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  que.push(pair<int, int>(0, 4 * (x1 * W + y1)));
  que.push(pair<int, int>(0, 4 * (x1 * W + y1) + 2));
  while (!que.empty()) {
    pair<int, int> curPoint = que.top();
    que.pop();
    if (curPoint.first > Cost[curPoint.second])
      continue;
    int turnbuf = (Cost[curPoint.second] + K - 1) / K * K;
    int pointbuf = curPoint.second / 4 * 4;
    int dirbuf = curPoint.second - pointbuf;
    for (int direction = 0; direction < 4; direction++) {
      if ((Cost[pointbuf + direction] == -1) ||
          (Cost[pointbuf + direction] > turnbuf))
        Cost[pointbuf + direction] = turnbuf;
      int nxtPoint = pointbuf + direction + convdir(direction, W);
      if ((nxtPoint < 0) || (nxtPoint >= H * W * 4))
        continue;
      if (abs(nxtPoint / 4 / W - pointbuf / 4 / W) +
              abs(nxtPoint / 4 % W - pointbuf / 4 % W) !=
          1)
        continue;
      if (c[nxtPoint / 4 / W][nxtPoint / 4 % W] == '@')
        continue;
      if (abs(direction - dirbuf) == 2)
        continue;
      if (direction == dirbuf) {
        if ((Cost[nxtPoint] > Cost[curPoint.second] + 1) ||
            (Cost[nxtPoint] == -1)) {
          Cost[nxtPoint] = Cost[curPoint.second] + 1;
          que.push(pair<int, int>(Cost[nxtPoint], nxtPoint));
        }
      } else {
        if ((Cost[nxtPoint] > turnbuf + 1) || (Cost[nxtPoint] == -1)) {
          Cost[nxtPoint] = turnbuf + 1;
          que.push(pair<int, int>(Cost[nxtPoint], nxtPoint));
        }
      }
    }
  }
  int res = 100000000;
  for (int i = 0; i < 4; i++)
    if (Cost[4 * (x2 * W + y2) + i] != -1)
      res = min(res, Cost[4 * (x2 * W + y2) + i]);
  cout << (res == 100000000 ? -1 : (res + K - 1) / K) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int convdir(int direction, int width) {
  switch (direction) {
  case 0:
    return -4;
  case 1:
    return 0 - 4 * width;
  case 2:
    return 4;
  case 3:
    return 4 * width;
  }
}

int main(void) {
  int H, W, K, x1, y1, x2, y2;
  char cbuf;
  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<vector<char>> c(H, vector<char>(W, ' '));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> cbuf;
      c[i][j] = cbuf;
    }
  }
  vector<int> Cost(H * W * 4, -1);
  Cost[4 * (x1 * W + y1)] = 0;
  priority_queue<pair<int, int>> que;
  que.push(pair<int, int>(0, 4 * (x1 * W + y1)));
  while (!que.empty()) {
    pair<int, int> curPoint = que.top();
    que.pop();
    if (curPoint.first > Cost[curPoint.second])
      continue;
    int turnbuf = (Cost[curPoint.second] + K - 1) / K * K;
    int pointbuf = curPoint.second / 4 * 4;
    int dirbuf = curPoint.second - pointbuf;
    for (int direction = 0; direction < 4; direction++) {
      int nxtPoint = pointbuf + direction + convdir(direction, W);
      if ((nxtPoint / 4 / W != pointbuf / 4 / W) &&
          (nxtPoint / 4 % W != pointbuf / 4 % W))
        continue;
      if (direction == dirbuf) {
        if ((nxtPoint >= 0) && (nxtPoint < H * W * 4)) {
          if (c[nxtPoint / 4 / W][nxtPoint / 4 % W] == '@')
            continue;
          if ((Cost[nxtPoint] > Cost[curPoint.second] + 1) ||
              Cost[nxtPoint] == -1) {
            Cost[nxtPoint] = Cost[curPoint.second] + 1;
            que.push(pair<int, int>(Cost[nxtPoint], nxtPoint));
          }
        }
        continue;
      }
      Cost[pointbuf + direction] = min(Cost[pointbuf + direction], turnbuf);
      if ((nxtPoint >= 0) && (nxtPoint < H * W * 4)) {
        if (c[nxtPoint / 4 / W][nxtPoint / 4 % W] == '@')
          continue;
        if ((Cost[nxtPoint] > turnbuf + 1) || (Cost[nxtPoint] == -1)) {
          Cost[nxtPoint] = turnbuf + 1;
          que.push(pair<int, int>(Cost[nxtPoint], nxtPoint));
        }
      }
    }
  }
  int res = 100000000;
  for (int i = 0; i < 4; i++)
    if (Cost[4 * (x2 * W + y2) + i] != -1)
      res = min(res, Cost[4 * (x2 * W + y2) + i]);
  cout << (res == 100000000 ? -1 : (res + K - 1) / K) << endl;
  return 0;
}