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

#include <bits/stdc++.h>
using namespace std;
#define SZ(v) ((int)(v).size())
#define zero(v) memset(v, 0, sizeof(v))
#define rep(i, n) for (int i = 0; i < (n); i++)
#define foreach(it, c)                                                         \\
  for (__typeof(c.begin()) it = c.begin(); it != c.end(); it++)
#define all(v) (v).begin(), (v).end()
template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
typedef long long ll;
typedef unsigned uint;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-10;
typedef pair<int, int> pii;
int inline in() {
  int x = 0, c;
  for (; (uint)((c = getchar()) - '0') >= 10;) {
    if (c == '-')
      return -in();
    if (!~c)
      throw ~0;
  }
  do {
    x = (x << 3) + (x << 1) + (c - '0');
  } while ((uint)((c = getchar()) - '0') < 10);
  return x;
}
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
int n, m, k;
int sx, sy, ex, ey;
queue<pair<int, pii>> q;
int main() {
  cin >> n >> m >> k;
  cin >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  vector<string> c(n, "");
  rep(i, n) cin >> c[i];
  vector<vector<int>> dp(n, vector<int>(m, INF));

  q.push(make_pair(0, pii(sx, sy)));
  dp[sx][sy] = 0;
  while (!q.empty()) {
    int cost = q.front().first, tx = q.front().second.first,
        ty = q.front().second.second;
    q.pop();
    //		cout<<tx<<' '<<ty<<' '<<dp[tx][ty]<<endl;
    rep(i, 4) {
      int ncost = cost + 1;
      int nx = tx, ny = ty;
      rep(j, k) {
        nx += dx[i];
        ny += dy[i];
        if (nx < 0 || nx >= n || ny < 0 || ny >= m || c[nx][ny] == '@')
          break;
        if (dp[nx][ny] > ncost) {
          dp[nx][ny] = ncost;
          q.push(make_pair(ncost, pii(nx, ny)));
        } else if (dp[nx][ny] == ncost)
          continue;
        else
          break;
      }
    }
  }
  if (dp[ex][ey] == INF)
    cout << -1 << endl;
  else
    cout << dp[ex][ey] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define SZ(v) ((int)(v).size())
#define zero(v) memset(v, 0, sizeof(v))
#define rep(i, n) for (int i = 0; i < (n); i++)
#define foreach(it, c)                                                         \\
  for (__typeof(c.begin()) it = c.begin(); it != c.end(); it++)
#define all(v) (v).begin(), (v).end()
template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
typedef long long ll;
typedef unsigned uint;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-10;
typedef pair<int, int> pii;
int inline in() {
  int x = 0, c;
  for (; (uint)((c = getchar()) - '0') >= 10;) {
    if (c == '-')
      return -in();
    if (!~c)
      throw ~0;
  }
  do {
    x = (x << 3) + (x << 1) + (c - '0');
  } while ((uint)((c = getchar()) - '0') < 10);
  return x;
}
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
int n, m, k;
int sx, sy, ex, ey;
priority_queue<pair<int, pii>> q;
int main() {
  cin >> n >> m >> k;
  cin >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  vector<string> c(n, "");
  rep(i, n) cin >> c[i];
  vector<vector<int>> dp(n, vector<int>(m, INF));

  q.push(make_pair(0, pii(sx, sy)));
  dp[sx][sy] = 0;
  while (!q.empty()) {
    int dis = -q.top().first, tx = q.top().second.first,
        ty = q.top().second.second;
    q.pop();
    if (dis > dp[tx][ty])
      continue;
    if (tx == ex && ty == ey) {
      cout << dp[tx][ty] << endl;
      return 0;
    }
    //		cout<<tx<<' '<<ty<<' '<<dp[tx][ty]<<endl;
    rep(d, 4) {
      int cnt = 0;
      int nx = tx;
      int ny = ty;
      while (1) {
        cnt++;
        if (cnt > k)
          break;
        nx += dx[d];
        ny += dy[d];
        if (nx < 0 || nx >= n || ny < 0 || ny >= m)
          break;
        if (c[nx][ny] == '@')
          break;
        if (dp[nx][ny] > dp[tx][ty] + 1) {
          dp[nx][ny] = dp[tx][ty] + 1;
          q.push(make_pair(-dp[nx][ny], pii(nx, ny)));
        }
      }
    }
  }
  if (dp[ex][ey] == INF)
    cout << -1 << endl;
  else
    cout << dp[ex][ey] << endl;
  return 0;
}