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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  int M = 2147483646;
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  char *s = new char[W + 1];
  vector<vector<int>> P(H + 2, vector<int>(W + 2, -1));
  for (int i = 1; i <= H; i++) {
    cin >> s;
    for (int j = 1; j <= W; j++) {
      if (s[j - 1] == 64) {
        P[i][j] = -1;
      } else {
        P[i][j] = M;
      }
    }
  }

  //	for (int i = 0; i < H + 2; i++) {
  //		cout << "::::";
  //		for (int j = 0; j < W + 2; j++) {
  //			if (P[i][j] == M) cout << ".";
  //			else if (P[i][j] == -1) cout << "@";
  //			else cout << P[i][j];
  //		}
  //		cout << endl;
  //	}

  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};
  vector<int> b = {2, 3, 0, 1};

  // start
  deque<pair<pair<int, int>, int>> Q;
  Q.push_back(pair<pair<int, int>, int>(pair<int, int>(x1, y1), -1));
  P[x1][y1] = 0;
  while (!Q.empty()) {
    pair<pair<int, int>, int> qq = *Q.begin();
    pair<int, int> q = qq.first;
    Q.pop_front();
    int c = P[q.first][q.second] + 1;
    for (int d = 0; d < 4; d++) {
      if (qq.second == b[d])
        continue;
      for (int i = 1; i <= K; i++) {
        if (P[q.first + dx[d] * i][q.second + dy[d] * i] == -1)
          break;
        if (c < P[q.first + dx[d] * i][q.second + dy[d] * i]) {
          P[q.first + dx[d] * i][q.second + dy[d] * i] = c;
          Q.push_back(pair<pair<int, int>, int>(
              pair<int, int>(q.first + dx[d] * i, q.second + dy[d] * i), d));
        }
      }
    }
    if (P[x2][y2] < M) {
      cout << P[x2][y2] << endl;
      return (0);
    }

    /*
    for (int i = 0; i < H + 2; i++) {
            cout << "::::";
            for (int j = 0; j < W + 2; j++) {
                    if (P[i][j] == M) cout << ".";
                    else if (P[i][j] == -1) cout << "@";
                    else cout << P[i][j];
            }
            cout << endl;
    }
    */
  }
  cout << -1 << endl;
  return (0);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  int M = 2147483646;
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  char *s = new char[W + 1];
  vector<vector<int>> P(H + 2, vector<int>(W + 2, -1));
  for (int i = 1; i <= H; i++) {
    cin >> s;
    for (int j = 1; j <= W; j++) {
      if (s[j - 1] == 64) {
        P[i][j] = -1;
      } else {
        P[i][j] = M;
      }
    }
  }

  //	for (int i = 0; i < H + 2; i++) {
  //		cout << "::::";
  //		for (int j = 0; j < W + 2; j++) {
  //			if (P[i][j] == M) cout << ".";
  //			else if (P[i][j] == -1) cout << "@";
  //			else cout << P[i][j];
  //		}
  //		cout << endl;
  //	}

  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};

  // start
  deque<pair<int, int>> Q;
  Q.push_back(pair<int, int>(x1, y1));
  P[x1][y1] = 0;
  while (!Q.empty()) {
    pair<int, int> q = *Q.begin();
    Q.pop_front();
    int c = P[q.first][q.second] + 1;
    for (int d = 0; d < 4; d++) {
      for (int i = 1; i <= K; i++) {
        if (P[q.first + dx[d] * i][q.second + dy[d] * i] == -1)
          break;
        if (c < P[q.first + dx[d] * i][q.second + dy[d] * i]) {
          P[q.first + dx[d] * i][q.second + dy[d] * i] = c;
          Q.push_back(
              pair<int, int>(q.first + dx[d] * i, q.second + dy[d] * i));
        }
      }
    }
    if (P[x2][y2] < M) {
      cout << P[x2][y2] << endl;
      return (0);
    }

    //		for (int i = 0; i < H + 2; i++) {
    //			cout << "::::";
    //			for (int j = 0; j < W + 2; j++) {
    //				if (P[i][j] == M) cout << ".";
    //				else if (P[i][j] == -1) cout << "@";
    //				else cout << P[i][j];
    //			}
    //			cout << endl;
    //		}
  }
  cout << -1 << endl;
  return (0);
}