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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> i_i;

#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define SZ(x) ((ll)(x).size())

const double PI = acos(-1.0);
// const ll INF = 1e18+3;
const ll mod = 998244353;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define print(x) cerr << #x << " is " << x << endl;

// #include<iostream>
// #include<queue>
// using namespace std;
const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
const int INF = 1e9;

int main() {
  int n, m, k, x1, y1, x2, y2;
  cin >> n >> m >> k >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  char mp[n][m] = {};
  int dis[n][m] = {};
  int i, j, ans = 0;
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      cin >> mp[i][j], dis[i][j] = INF;
  queue<pair<int, int>> que;
  dis[x1][y1] = 0;
  que.push(make_pair(x1, y1));
  while (!que.empty()) {
    int x = que.front().first;
    int y = que.front().second;
    que.pop();
    for (i = 0; i < 4; i++) {
      int nx = x, ny = y;
      int nd = dis[x][y] + 1;
      for (j = 1; j <= k; j++) {
        nx += dir[i][0];
        ny += dir[i][1];
        if (nx < 0 || ny < 0 || nx >= n || ny >= m || mp[nx][ny] == '@' ||
            dis[nx][ny] < nd)
          break;
        if (dis[nx][ny] > nd) {
          dis[nx][ny] = nd;
          que.push(make_pair(nx, ny));
        }
      }
    }
  }
  ans = dis[x2][y2];
  cout << (ans == INF ? -1 : ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> i_i;

#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define SZ(x) ((ll)(x).size())

const double PI = acos(-1.0);
// const ll INF = 1e18+3;
const ll mod = 998244353;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define print(x) cerr << #x << " is " << x << endl;

#include <iostream>
#include <vector>
using namespace std;
using DD = int;
const int INF = 10000000;

struct edge {
  int to, cost;
};

#define MAX_V 1000005
int V;
vector<edge> G[MAX_V];
int d[MAX_V];
int pre[MAX_V];

void add_edge(int from, int to, DD dist) {
  G[from].push_back((edge){to, dist});
  G[to].push_back((edge){from, dist});
}
void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  fill(pre, pre + V, -1);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pre[e.to] = v;
        que.push(P(d[e.to], e.to));
      }
    }
  }
  // for (int i = 0; i < V; i++){
  //     fprintf(stderr, "%d, %d: %d\\n", s, i, d[i]);
  // }
  return;
}
////////////////////////////////////////
// grid dijkstra
// x = i/W, y = i%W;
// xy = j * W + i;
int main() {
  int H, W, K, S, G, x1, y1, x2, y2;
  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  V = H * W;
  vector<string> A(H);
  rep(i, H) cin >> A[i];
  S = (x1 - 1) * W + (y1 - 1);
  G = (x2 - 1) * W + (y2 - 1);
  // G1 = V - 1;
  // rep(i, H)rep(j, W)cout << A[i][j];
  // exit(1);
  rep(now, H * W) {
    int i = now / W;
    int j = now % W;
    // d[now][now] = 0;
    if (A[i][j] == '.') {
      int k;
      for (int x = now - W, k = 1; k <= K; x -= W, k++) {
        if (x / W >= 0 && i - k >= 0 && A[i - k][j] != '@') {
          add_edge(now, x, 1);
          //                   fprintf(stderr, "%d -> %d\\n", now, x);
        } else {
          break;
        }
      }
      for (int x = now - 1, k = 1; k <= K; x -= 1, k++) {
        if (x % W >= 0 && j - k >= 0 && A[i][j - k] != '@') {
          add_edge(now, x, 1);
          // fprintf(stderr, "%d -> %d\\n", now, x);
        } else {
          break;
        }
      }
#if 0                
            //if (now /W >= 1 && A[i - 1][j] != '@'){
            //    add_edge(now, now - W, 1);
            //}
            if (now /W < H - 1 && A[i + 1][j] != '@'){
                add_edge(now, now + W, 1);
            }
            if (now %W >= 1 && A[i][j - 1] != '@'){
                add_edge(now, now - 1, 1);
            }
            if (now % W < W - 1 && A[i][j + 1] != '@'){
                add_edge(now, now + 1, 1);
                //d[now][now + 1] = 1;
                //d[now + 1][now] = 1;
            }
#endif
    }
  }

  dijkstra(S);
  if (d[G] == INF) {
    cout << -1;
  } else {
    cout << d[G];
  }
  return 0;
}