Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <string.h>
#define ll long long
#define pf printf
#define sf scanf
#define ff first
#define ss second
#define clr clear()
#define sz size()
#define pb push_back
#define mk make_pair
#define pi acos(-1)
#define inf 123456789
#define mod 1000000007
#define ull unsigned long long int
#define f(i, k, n) for (i = k; i < n; i++)
#define fr(i, n, k) for (i = n; i >= k; i--)
#define ent(a) scanf("%lld", &a)
#define ent2(a, b) scanf("%lld%lld", &a, &b)
#define ent3(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define mem(a) memset(a, 0, sizeof(a))
#define vec(v, s) vector<ll> v[s]
#define arr(a, s) ll a[s];
/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
// knight and king//
int dr[] = {2, 2, -2, -2, 1, -1, 1, -1};
int dc[] = {1, -1, 1, -1, 2, 2, -2, -2};
int dr1[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dc1[] = {1, -1, 1, 0, -1, 0, 1, -1};
int dr2[] = {0, 0, 1, -1};
int dc2[] = {1, -1, 0, 0};
////////////////////////////
using namespace std;
#define ma 1000005

vector<vector<int>> a, mark;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll tc, i, j, x1, y1, k, cas = 0, q, m, n, x2, y2;
  while (cin >> n >> m >> k) {
    cin >> x1 >> y1 >> x2 >> y2;
    x1--;
    y1--;
    x2--;
    y2--;
    a.resize(n + 1);
    mark.resize(n + 1);
    for (i = 0; i < n; i++) {
      a[i].resize(m + 1);
      mark[i].resize(m + 1);
      for (j = 0; j < m; j++) {
        mark[i][j] = inf;
        char ch;
        cin >> ch;
        if (ch == '.')
          a[i][j] = 0;
        else
          a[i][j] = 1;
      }
    }
    queue<ll> q;
    mark[x1][y1] = 0;
    q.push(x1);
    q.push(y1);
    ll ans = inf;
    ll cn = 0;

    while (q.sz > 0) {
      ll ur = q.front();
      q.pop();
      ll uc = q.front();
      q.pop();
      cn++;
      for (i = 1; i <= k && ur + i < n; i++) {
        if (a[ur + i][uc] == 1)
          break;
        if (mark[ur + i][uc] > mark[ur][uc] + 1) {

          mark[ur + i][uc] = mark[ur][uc] + 1;
          q.push(ur + i);
          q.push(uc);
        } else if (mark[ur + i][uc] < mark[ur][uc])
          break;
      }
      for (i = 1; i <= k && ur - i >= 0; i++) {
        if (a[ur - i][uc] == 1)
          break;
        if (mark[ur - i][uc] > mark[ur][uc] + 1) {
          mark[ur - i][uc] = mark[ur][uc] + 1;
          q.push(ur - i);
          q.push(uc);
        } else if (mark[ur - i][uc] < mark[ur][uc])
          break;
      }
      for (i = 1; i <= k && uc + i < m; i++) {
        if (a[ur][uc + i] == 1)
          break;
        if (mark[ur][uc + i] > mark[ur][uc] + 1) {
          mark[ur][uc + i] = mark[ur][uc] + 1;
          q.push(ur);
          q.push(uc + i);
        } else if (mark[ur][uc + i] < mark[ur][uc])
          break;
      }
      for (i = 1; i <= k && uc - i >= 0; i++) {
        if (a[ur][uc - i] == 1)
          break;
        if (mark[ur][uc - i] > mark[ur][uc] + 1) {
          mark[ur][uc - i] = mark[ur][uc] + 1;
          q.push(ur);
          q.push(uc - i);
        } else if (mark[ur][uc - i] < mark[ur][uc])
          break;
      }
    }
    ans = mark[x2][y2];
    if (ans == inf)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <string.h>
#define ll long long
#define pf printf
#define sf scanf
#define ff first
#define ss second
#define clr clear()
#define sz size()
#define pb push_back
#define mk make_pair
#define pi acos(-1)
#define inf 123456789
#define mod 1000000007
#define ull unsigned long long int
#define f(i, k, n) for (i = k; i < n; i++)
#define fr(i, n, k) for (i = n; i >= k; i--)
#define ent(a) scanf("%lld", &a)
#define ent2(a, b) scanf("%lld%lld", &a, &b)
#define ent3(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define mem(a) memset(a, 0, sizeof(a))
#define vec(v, s) vector<ll> v[s]
#define arr(a, s) ll a[s];
/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
// knight and king//
int dr[] = {2, 2, -2, -2, 1, -1, 1, -1};
int dc[] = {1, -1, 1, -1, 2, 2, -2, -2};
int dr1[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dc1[] = {1, -1, 1, 0, -1, 0, 1, -1};
int dr2[] = {0, 0, 1, -1};
int dc2[] = {1, -1, 0, 0};
////////////////////////////
using namespace std;
#define ma 1000005

vector<vector<int>> a, mark;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll tc, i, j, x1, y1, k, cas = 0, q, m, n, x2, y2;
  while (cin >> n >> m >> k) {
    cin >> x1 >> y1 >> x2 >> y2;
    x1--;
    y1--;
    x2--;
    y2--;
    a.resize(n + 1);
    mark.resize(n + 1);
    for (i = 0; i < n; i++) {
      a[i].resize(m + 1);
      mark[i].resize(m + 1);
      for (j = 0; j < m; j++) {
        mark[i][j] = inf;
        char ch;
        cin >> ch;
        if (ch == '.')
          a[i][j] = 0;
        else
          a[i][j] = 1;
      }
    }
    queue<ll> q;
    mark[x1][y1] = 0;
    q.push(x1);
    q.push(y1);
    ll ans = inf;
    ll cn = 0;

    while (q.sz > 0) {
      if (mark[x2][y2] != inf)
        break;
      ll ur = q.front();
      q.pop();
      ll uc = q.front();
      q.pop();
      cn++;
      for (i = 1; i <= k && ur + i < n; i++) {
        if (a[ur + i][uc] == 1)
          break;
        if (mark[ur + i][uc] > mark[ur][uc] + 1) {

          mark[ur + i][uc] = mark[ur][uc] + 1;
          q.push(ur + i);
          q.push(uc);
        } else if (mark[ur + i][uc] < mark[ur][uc])
          break;
      }
      for (i = 1; i <= k && ur - i >= 0; i++) {
        if (a[ur - i][uc] == 1)
          break;
        if (mark[ur - i][uc] > mark[ur][uc] + 1) {
          mark[ur - i][uc] = mark[ur][uc] + 1;
          q.push(ur - i);
          q.push(uc);
        } else if (mark[ur - i][uc] < mark[ur][uc])
          break;
      }
      for (i = 1; i <= k && uc + i < m; i++) {
        if (a[ur][uc + i] == 1)
          break;
        if (mark[ur][uc + i] > mark[ur][uc] + 1) {
          mark[ur][uc + i] = mark[ur][uc] + 1;
          q.push(ur);
          q.push(uc + i);
        } else if (mark[ur][uc + i] < mark[ur][uc])
          break;
      }
      for (i = 1; i <= k && uc - i >= 0; i++) {
        if (a[ur][uc - i] == 1)
          break;
        if (mark[ur][uc - i] > mark[ur][uc] + 1) {
          mark[ur][uc - i] = mark[ur][uc] + 1;
          q.push(ur);
          q.push(uc - i);
        } else if (mark[ur][uc - i] < mark[ur][uc])
          break;
      }
    }
    ans = mark[x2][y2];
    if (ans == inf)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}