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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
#define yn                                                                     \\
  { puts("Yes"); }                                                             \\
  else {                                                                       \\
    puts("No");                                                                \\
  }
#define MAX_N 200005

int main() {
  int h, w, K;
  cin >> h >> w >> K;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  string s[h + 2];
  rep(i, h + 2) {
    if (i == 0 || i == h + 1) {
      rep(j, w + 2) s[i] += '@';
    } else {
      string ss;
      cin >> ss;
      s[i] = '@' + ss + '@';
    }
  }

  pair<int, P> p;
  priority_queue<pair<int, P>, vector<pair<int, P>>, greater<pair<int, P>>> que;

  int dp[h + 2][w + 2];
  int look[h + 2][w + 2][4];
  rep(i, h + 2) {
    rep(j, w + 2) {
      if (s[i][j] == '@')
        dp[i][j] = -1;
      else
        dp[i][j] = 1001001001;
      rep(k, 4) look[i][j][k] = 0;
    }
  }

  p.first = 0;
  p.second.first = sx;
  p.second.second = sy;
  dp[sx][sy] = 0;

  int dx[4] = {-1, 1, 0, 0};
  int dy[4] = {0, 0, -1, 1};

  que.push(p);
  while (que.size() > 0) {
    p = que.top();
    que.pop();
    int val = p.first;
    int x = p.second.first;
    int y = p.second.second;
    if (val > dp[x][y])
      continue;
    // cout << x << y << endl;
    rep(i, 4) {
      if (look[x][y][i] == 1)
        continue;
      srep(j, 1, K + 1) {
        int nx = x + dx[i] * j;
        int ny = y + dy[i] * j;
        if (s[nx][ny] == '@')
          break;
        if (s[nx][ny] == '.' && dp[nx][ny] > dp[x][y] + 1) {
          dp[nx][ny] = val + 1;
          p.first = val + 1;
          p.second.first = nx;
          p.second.second = ny;
          que.push(p);
          if (j < K)
            look[nx][ny][i] = 1;
          int ii = i + 1;
          if (ii % 2 == 0)
            ii -= 2;
          look[nx][ny][ii] = 1;
        } else if (dp[nx][ny] <= dp[x][y]) {
          break;
        }
      }
      look[x][y][i] = 1;
    }
  }

  if (dp[gx][gy] == 1001001001)
    dp[gx][gy] = -1;
  cout << dp[gx][gy] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
#define yn                                                                     \\
  { puts("Yes"); }                                                             \\
  else {                                                                       \\
    puts("No");                                                                \\
  }
#define MAX_N 200005

int main() {
  int h, w, K;
  cin >> h >> w >> K;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  string s[h + 2];
  rep(i, h + 2) {
    if (i == 0 || i == h + 1) {
      rep(j, w + 2) s[i] += '@';
    } else {
      string ss;
      cin >> ss;
      s[i] = '@' + ss + '@';
    }
  }

  pair<int, P> p;
  priority_queue<pair<int, P>> que;

  int dp[h + 2][w + 2];
  rep(i, h + 2) {
    rep(j, w + 2) {
      if (s[i][j] == '@')
        dp[i][j] = -1;
      else
        dp[i][j] = 1001001001;
    }
  }

  p.first = 0;
  p.second.first = sx;
  p.second.second = sy;
  dp[sx][sy] = 0;

  int dx[4] = {-1, 1, 0, 0};
  int dy[4] = {0, 0, -1, 1};

  que.push(p);
  while (que.size() > 0) {
    p = que.top();
    que.pop();
    int val = p.first;
    int x = p.second.first;
    int y = p.second.second;
    if (val > dp[x][y])
      continue;
    // cout << x << y << endl;
    rep(i, 4) {
      srep(j, 1, K + 1) {
        int nx = x + dx[i] * j;
        int ny = y + dy[i] * j;
        if (s[nx][ny] == '@')
          break;
        if (s[nx][ny] == '.' && dp[nx][ny] > dp[x][y] + 1) {
          dp[nx][ny] = val + 1;
          p.first = val + 1;
          p.second.first = nx;
          p.second.second = ny;
          que.push(p);
        } else if (dp[nx][ny] <= dp[x][y]) {
          break;
        }
      }
    }
  }

  if (dp[gx][gy] == 1001001001)
    dp[gx][gy] = -1;
  cout << dp[gx][gy] << endl;
  return 0;
}