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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

int main() {
  int h, w, k;
  int x1, y1, x2, y2;

  cin >> h >> w >> k;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<vector<char>> v(h + 1, vector<char>(w + 1, 0));
  vector<vector<int>> c(h + 1, vector<int>(w + 1, INF));

  rep(i, h) {
    rep(j, w) { cin >> v[i][j]; }
  }

  queue<pair<int, int>> q;
  q.push({y1, x1});
  c[y1][x1] = 0;
  while (!q.empty()) {
    auto p = q.front();
    int px = p.second;
    int py = p.first;
    q.pop();

    for (int i = 0; i < 4; i++) {
      int xd = dx[i];
      int yd = dy[i];
      for (int j = 1; j <= k; j++) {
        int xdd = xd * j + px;
        int ydd = yd * j + py;
        if (xdd >= 0 && ydd >= 0 && xdd < w && ydd < h) {
          if (v[ydd][xdd] == '@') {
            break;
          } else {
            if (c[ydd][xdd] > c[py][px] + 1) {
              c[ydd][xdd] = c[py][px] + 1;
              //                            cout<<ydd<<" "<<xdd<<endl;
              //                            cout<<c[ydd][xdd]<<endl;
              q.push({ydd, xdd});
            } else if (c[ydd][xdd] <= c[py][px]) {
              break;
            }
          }
        }
      }
    }
  }
  //    rep(i,h){
  //        rep(j,w){
  //            cout<<c[i][j]<<" ";
  //        }
  //        cout<<endl;
  //    }
  if (c[y2][x2] == INF) {
    c[y2][x2] = -1;
  }

  cout << c[y2][x2] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

int main() {
  int h, w, k;
  int x1, y1, x2, y2;

  cin >> h >> w >> k;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<vector<char>> v(h + 1, vector<char>(w + 1, 0));
  vector<vector<int>> c(h + 1, vector<int>(w + 1, INF));

  rep(i, h) {
    rep(j, w) { cin >> v[i][j]; }
  }

  queue<pair<int, int>> q;
  q.push({y1, x1});
  c[y1][x1] = 0;
  while (!q.empty()) {
    auto p = q.front();
    int px = p.second;
    int py = p.first;
    q.pop();

    for (int i = 0; i < 4; i++) {
      int xd = dx[i];
      int yd = dy[i];
      for (int j = 1; j <= k; j++) {
        int xdd = xd * j + px;
        int ydd = yd * j + py;
        if (xdd >= 0 && ydd >= 0 && xdd < w && ydd < h) {
          if (v[ydd][xdd] == '@') {
            break;
          } else {
            if (c[ydd][xdd] >= c[py][px] + 1) {
              c[ydd][xdd] = c[py][px] + 1;
              //                            cout<<ydd<<" "<<xdd<<endl;
              //                            cout<<c[ydd][xdd]<<endl;
              q.push({ydd, xdd});
            } else {
              break;
            }
          }
        }
      }
    }
  }
  //    rep(i,h){
  //        rep(j,w){
  //            cout<<c[i][j]<<" ";
  //        }
  //        cout<<endl;
  //    }
  if (c[y2][x2] == INF) {
    c[y2][x2] = -1;
  }

  cout << c[y2][x2] << endl;

  return 0;
}