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

#include <bits/stdc++.h>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \\
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e18
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;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}
template <class T> void show(vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << "\\n";
}
typedef long long ll;

struct cor {
  int x;
  int y;
  int depth;
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  vector<string> grid(h);
  for (int i = 0; i < h; i++) {
    cin >> grid[i];
  }
  queue<cor> q;
  q.push({sx, sy, 0});
  vector<vector<int>> flag(h, vector<int>(w, INF));
  flag[sx][sy] = 0;
  while (!q.empty()) {
    cor v = q.front();
    q.pop();
    int x = v.x;
    int y = v.y;
    int d = v.depth;
    for (int i = 1; i <= k; i++) {
      int nx = x;
      int ny = y + i;
      if (ny < 0 || ny >= w || flag[nx][ny] < d + 1 || grid[nx][ny] == '@') {
        break;
      }
      if (flag[nx][ny] > d + 1) {
        flag[nx][ny] = d + 1;
        q.push({nx, ny, d + 1});
      }
    }
    for (int i = 1; i <= k; i++) {
      int nx = x;
      int ny = y - i;
      if (ny < 0 || ny >= w || flag[nx][ny] < d + 1 || grid[nx][ny] == '@') {
        break;
      }
      if (flag[nx][ny] > d + 1) {
        flag[nx][ny] = d + 1;
        q.push({nx, ny, d + 1});
      }
    }
    for (int i = 1; i <= k; i++) {
      int nx = x + i;
      int ny = y;
      if (nx < 0 || nx >= h || flag[nx][ny] < d + 1 || grid[nx][ny] == '@') {
        break;
      }
      if (flag[nx][ny] > d + 1) {
        flag[nx][ny] = d + 1;
        q.push({nx, ny, d + 1});
      }
    }
    for (int i = 1; i <= k; i++) {
      int nx = x - i;
      int ny = y;
      if (nx < 0 || nx >= h || flag[nx][ny] < d + 1 || grid[nx][ny] == '@') {
        break;
      }
      if (flag[nx][ny] > d + 1) {
        flag[nx][ny] = d + 1;
        q.push({nx, ny, d + 1});
      }
    }
  }
  /*  for (int i = 0; i < h; i++)
  {
      for (int j = 0; j < w; j++)
      {
          cerr << flag[i][j] << " ";
      }
      cerr << '\\n';
  }*/
  if (flag[gx][gy] == INF) {
    cout << -1 << '\\n';
  } else
    cout << flag[gx][gy] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \\
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e18
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;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}
template <class T> void show(vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << "\\n";
}
typedef long long ll;

struct cor {
  int x;
  int y;
  int depth;
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  vector<string> grid(h);
  for (int i = 0; i < h; i++) {
    cin >> grid[i];
  }
  queue<cor> q;
  q.push({sx, sy, 0});
  vector<vector<int>> flag(h, vector<int>(w, INF));
  flag[sx][sy] = 0;
  while (!q.empty()) {
    cor v = q.front();
    q.pop();
    int x = v.x;
    int y = v.y;
    int d = v.depth;
    bool hasu1 = false;
    bool hasu2 = false;
    bool hasu3 = false;
    bool hasu4 = false;
    for (int i = 1; i <= k; i++) {
      if (x + i < h && grid[x + i][y] == '@') {
        hasu1 = true;
      }
      if (x - i >= 0 && grid[x - i][y] == '@') {
        hasu2 = true;
      }
      if (hasu1 && hasu2) {
        break;
      }
      if (x + i < h && flag[x + i][y] == INF && !hasu1) {
        q.push({x + i, y, d + 1});
        flag[x + i][y] = d + 1;
      }
      if (x - i >= 0 && flag[x - i][y] == INF && !hasu2) {
        q.push({x - i, y, d + 1});
        flag[x - i][y] = d + 1;
      }
    }
    for (int i = 1; i <= k; i++) {
      if (y + i < w && grid[x][y + i] == '@') {
        hasu3 = true;
      }
      if (y - i >= 0 && grid[x][y - i] == '@') {
        hasu4 = true;
      }
      if (hasu3 && hasu4) {
        break;
      }
      if (y + i < w && flag[x][y + i] == INF && !hasu3) {
        q.push({x, y + i, d + 1});
        flag[x][y + i] = d + 1;
      }
      if (y - i >= 0 && flag[x][y - i] == INF && !hasu4) {
        q.push({x, y - i, d + 1});
        flag[x][y - i] = d + 1;
      }
    }
  }
  /*  for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            cerr << flag[i][j] << " ";
        }
        cerr << '\\n';
    }*/
  if (flag[gx][gy] == INF) {
    cout << -1 << '\\n';
  } else
    cout << flag[gx][gy] << endl;

  return 0;
}