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

#include <bits/stdc++.h>
using namespace std;
// using namespace __gnu_pbds;
// typedef
// tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update>
// set_t;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define SUM(v) accumulate(v.begin(), v.end(), lli(0))
#define rev(n, d) for (int i = n; i >= d; i--)
#define FOR0(n) for (int i = 0; i < n; i++)
#define FOR(a, n) for (int i = a; i < n; i++)
#define FORQ(a, n) for (int i = a; i <= n; i++)
#define inf 1000000009
#define inff 0x3f3f3f3f
#define sz(a) int((a).size())
#define all(c) c.begin(), c.end()
#define ff first
#define ss second
#define endl "\\n"
#define mod 1000000007
#define mem(ar, x) memset(ar, x, sizeof ar)
#define present(container, element) (container.find(element) != container.end())
#define max3(a, b, c)                                                          \\
  return (lli(a) > lli(b) ? (lli(a) > lli(c) ? a : c)                          \\
                          : (lli(b) > lli(c) ? b : c))
#define max4(a, b, c, d) return (max3(a, b, c) > lli(d) ? (max3(a, b, c)) : d)
#define min3(a, b, c)                                                          \\
  return (lli(a) < lli(b) ? (lli(a) < lli(c) ? a : c)                          \\
                          : (lli(b) < lli(c) ? b : c))
#define min4(a, b, c, d) return (max3(a, b, c) < lli(d) ? (max3(a, b, c)) : d)
typedef long long int lli;
typedef pair<int, int> pii;
typedef pair<int, pii> ppii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
const int N = 100005;
int dr8[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dc8[] = {1, -1, 0, 0, -1, 1, -1, 1};
int dr4[] = {0, 1, -1, 0};
int dc4[] = {1, 0, 0, -1};
int n, m, k;
int main() {
  fast cin >> n >> m >> k;
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  pii came[n][m];
  sx--;
  sy--;
  ex--;
  ey--;
  char a[n][m];
  FOR0(n) {
    for (int j = 0; j < m; j++)
      cin >> a[i][j];
  }
  int vis[n][m];
  mem(vis, -1);
  queue<pair<int, int>> q;
  vis[sx][sy] = 0;
  q.push({sx, sy});
  mem(came, -1);
  came[sx][sy] = {-1, -1};
  while (!q.empty()) {
    pii f = q.front();
    q.pop();
    int x = f.ff, y = f.ss;
    for (int i = 1; i <= k; i++) {
      int yy = y + i;
      if (yy >= m)
        break;
      if (a[x][yy] == '@')
        break;
      else if (vis[x][yy] == -1) {
        vis[x][yy] = vis[x][y] + 1;
        q.push({x, yy});
        came[x][yy] = {x, y};
      } else if (came[x][yy] == came[x][y])
        break;
    }
    for (int i = 1; i <= k; i++) {
      int yy = y - i;
      if (yy < 0)
        break;
      if (a[x][yy] == '@')
        break;
      else if (vis[x][yy] == -1) {
        vis[x][yy] = vis[x][y] + 1;
        q.push({x, yy});
        came[x][yy] = {x, y};
      } else if (came[x][yy] == came[x][y])
        break;
    }
    for (int i = 1; i <= k; i++) {
      int xx = x + i;
      if (xx >= n)
        break;
      if (a[xx][y] == '@')
        break;
      else if (vis[xx][y] == -1) {
        vis[xx][y] = vis[x][y] + 1;
        q.push({xx, y});
        came[xx][y] = {x, y};
      } else if (came[xx][y] == came[x][y])
        break;
    }
    for (int i = 1; i <= k; i++) {
      int xx = x - i;
      if (xx < 0)
        break;
      if (a[xx][y] == '@')
        break;
      else if (vis[xx][y] == -1) {
        vis[xx][y] = vis[x][y] + 1;
        q.push({xx, y});
        came[xx][y] = {x, y};
      } else if (came[xx][y] == came[x][y])
        break;
    }
  }
  cout << vis[ex][ey];
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// using namespace __gnu_pbds;
// typedef
// tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update>
// set_t;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define SUM(v) accumulate(v.begin(), v.end(), lli(0))
#define rev(n, d) for (int i = n; i >= d; i--)
#define FOR0(n) for (int i = 0; i < n; i++)
#define FOR(a, n) for (int i = a; i < n; i++)
#define FORQ(a, n) for (int i = a; i <= n; i++)
#define inf 1000000009
#define inff 0x3f3f3f3f
#define sz(a) int((a).size())
#define all(c) c.begin(), c.end()
#define ff first
#define ss second
#define endl "\\n"
#define mod 1000000007
#define mem(ar, x) memset(ar, x, sizeof ar)
#define present(container, element) (container.find(element) != container.end())
#define max3(a, b, c)                                                          \\
  return (lli(a) > lli(b) ? (lli(a) > lli(c) ? a : c)                          \\
                          : (lli(b) > lli(c) ? b : c))
#define max4(a, b, c, d) return (max3(a, b, c) > lli(d) ? (max3(a, b, c)) : d)
#define min3(a, b, c)                                                          \\
  return (lli(a) < lli(b) ? (lli(a) < lli(c) ? a : c)                          \\
                          : (lli(b) < lli(c) ? b : c))
#define min4(a, b, c, d) return (max3(a, b, c) < lli(d) ? (max3(a, b, c)) : d)
typedef long long int lli;
typedef pair<int, int> pii;
typedef pair<int, pii> ppii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
const int N = 100005;
int dr8[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dc8[] = {1, -1, 0, 0, -1, 1, -1, 1};
int dr4[] = {0, 1, -1, 0};
int dc4[] = {1, 0, 0, -1};
int n, m, k;
int main() {
  fast cin >> n >> m >> k;
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  char a[n][m];
  FOR0(n) {
    for (int j = 0; j < m; j++)
      cin >> a[i][j];
  }
  int vis[n][m];
  mem(vis, -1);
  queue<pair<int, int>> q;
  vis[sx][sy] = 0;
  q.push({sx, sy});
  while (!q.empty()) {
    pii f = q.front();
    q.pop();
    int x = f.ff, y = f.ss;
    for (int i = 1; i <= k; i++) {
      int yy = y + i;
      if (yy >= m)
        break;
      if (a[x][yy] == '@')
        break;
      else if (vis[x][yy] == -1) {
        vis[x][yy] = vis[x][y] + 1;
        q.push({x, yy});
      }
    }
    for (int i = 1; i <= k; i++) {
      int yy = y - i;
      if (yy < 0)
        break;
      if (a[x][yy] == '@')
        break;
      else if (vis[x][yy] == -1) {
        vis[x][yy] = vis[x][y] + 1;
        q.push({x, yy});
      }
    }
    for (int i = 1; i <= k; i++) {
      int xx = x + i;
      if (xx >= n)
        break;
      if (a[xx][y] == '@')
        break;
      else if (vis[xx][y] == -1) {
        vis[xx][y] = vis[x][y] + 1;
        q.push({xx, y});
      }
    }
    for (int i = 1; i <= k; i++) {
      int xx = x - i;
      if (xx < 0)
        break;
      if (a[xx][y] == '@')
        break;
      else if (vis[xx][y] == -1) {
        vis[xx][y] = vis[x][y] + 1;
        q.push({xx, y});
      }
    }
  }
  cout << vis[ex][ey];
}