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

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
/************************************************/
#define rep(i, n) for (int i = 0; i < n; i++)
#define m_p make_pair
#define pb push_back
#define fr first
#define se second
#define forn(i, a, n) for (int i = a; i < n; i++)
#define foreach(i, c)                                                          \\
  for (__typeof(c.begin()) i = (c.begin()); i != (c).end(); i++)
#define pii pair<int, int>
#define vi vector<int>
#define ll long long
#define sz(s) s.size()
#define all(s) s.begin(), s.end()
#define zero(x) memset(x, 0, sizeof(x))
#define vii vector<pair<int, int>>
#define mpis map<int, string>
#define mpii map<int, int>
#define mpsi map<string, int>
#define re return
#define mod 1000000007
/************************************************/
using namespace std;

long long get() {
  char c = getchar();
  long long x = 0LL;
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9') {
    x *= 10LL;
    x += (c - '0');
    c = getchar();
  }
  return x;
}
int h, w, k;
int x1, y11, x2, y2;
string s[1000001];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
bool in(int x, int y) { re(x >= 0 && x < h && y >= 0 && y < w); }
int main() {
  ios::sync_with_stdio(0);
  cin >> h >> w >> k >> x1 >> y11 >> x2 >> y2;
  vector<vi> v(h, vi(w));
  x1--, y11--, x2--, y2--;
  rep(i, h) cin >> s[i];
  rep(i, h) rep(j, w) v[i][j] = 1e8;
  v[x1][y11] = 0;
  queue<pii> q;
  q.push(m_p(x1, y11));
  while (!q.empty()) {
    pii p = q.front();
    int x = p.fr, y = p.se;
    q.pop();
    rep(i, 4) rep(j, k) {
      int xx = x + dx[i] * (j + 1), yy = y + dy[i] * (j + 1);
      if (!in(xx, yy))
        break;
      if (s[xx][yy] == '@')
        break;
      ;
      if (v[xx][yy] > v[x][y] + 1) {
        if (xx == x2 && yy == y2) {
          cout << v[x][y] + 1;
          re 0;
        }
        v[xx][yy] = v[x][y] + 1;
        q.push(m_p(xx, yy));
      }
      if (v[xx][yy] <= v[x][y]) {
        break;
      }
    }
  }
  cout << -1;
  re 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
/************************************************/
#define rep(i, n) for (int i = 0; i < n; i++)
#define m_p make_pair
#define pb push_back
#define fr first
#define se second
#define forn(i, a, n) for (int i = a; i < n; i++)
#define foreach(i, c)                                                          \\
  for (__typeof(c.begin()) i = (c.begin()); i != (c).end(); i++)
#define pii pair<int, int>
#define vi vector<int>
#define ll long long
#define sz(s) s.size()
#define all(s) s.begin(), s.end()
#define zero(x) memset(x, 0, sizeof(x))
#define vii vector<pair<int, int>>
#define mpis map<int, string>
#define mpii map<int, int>
#define mpsi map<string, int>
#define re return
#define mod 1000000007
/************************************************/
using namespace std;

long long get() {
  char c = getchar();
  long long x = 0LL;
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9') {
    x *= 10LL;
    x += (c - '0');
    c = getchar();
  }
  return x;
}
int h, w, k;
int x1, y11, x2, y2;
string s[1000001];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
bool in(int x, int y) { re(x >= 0 && x < h && y >= 0 && y < w); }
int main() {
  ios::sync_with_stdio(0);
  cin >> h >> w >> k >> x1 >> y11 >> x2 >> y2;
  vector<vi> v(h, vi(w));
  x1--, y11--, x2--, y2--;
  rep(i, h) cin >> s[i];
  rep(i, h) rep(j, w) v[i][j] = 1e8;
  v[x1][y11] = 0;
  queue<pii> q;
  q.push(m_p(x1, y11));
  while (!q.empty()) {
    pii p = q.front();
    int x = p.fr, y = p.se;
    q.pop();
    rep(i, 4) rep(j, k) {
      int xx = x + dx[i] * (j + 1), yy = y + dy[i] * (j + 1);
      if (!in(xx, yy))
        break;
      if (s[xx][yy] == '@')
        break;
      ;
      if (v[xx][yy] > v[x][y] + 1) {
        if (xx == x2 && yy == y2) {
          cout << v[x][y] + 1;
          re 0;
        }
        v[xx][yy] = v[x][y] + 1;
        q.push(m_p(xx, yy));
      }
    }
  }
  if (v[x2][y2] == 1e8)
    cout << -1;
  else
    cout << v[x2][y2];
  re 0;
}