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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <new>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

typedef long long ll;
typedef unsigned long long ull;
constexpr ll mop = 1000000007;
constexpr ll mop2 = 998244353;

using namespace std;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  pair<int, int> st, go;
  cin >> st.first >> st.second >> go.first >> go.second;
  vector<vector<int>> grid, pushed;
  grid.resize(h);
  pushed.resize(h);
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    grid[i].resize(w);
    pushed[i].resize(w);
    for (int j = 0; j < w; j++) {
      if (s[j] == '@') {
        grid[i][j] = INT32_MIN;
      } else {
        grid[i][j] = INT32_MAX;
      }
    }
  }

  queue<pair<int, int>> Q;
  st.first--;
  st.second--;
  Q.push(st);
  grid[st.first][st.second] = 0;
  pushed[st.first][st.second] = true;
  while (!Q.empty()) {
    pair<int, int> now = Q.front();
    Q.pop();
    int nowDis = grid[now.first][now.second];
    if (now.first == go.first - 1 && now.second == go.second - 1) {
      break;
    }
    pair<int, int> newPoint;

    newPoint = now;
    int xp = 1;
    while (xp <= k) {
      newPoint.first++;
      if (newPoint.first >= h) {
        break;
      }
      if (grid[newPoint.first][newPoint.second] <= nowDis) {
        break;
      }
      grid[newPoint.first][newPoint.second] = nowDis + 1;
      if (pushed[newPoint.first][newPoint.second] == false) {
        Q.push(newPoint);
        pushed[newPoint.first][newPoint.second] = true;
      }
      xp++;
    }
    newPoint = now;
    int xm = 1;
    while (xm <= k) {
      newPoint.first--;
      if (newPoint.first < 0) {
        break;
      }
      if (grid[newPoint.first][newPoint.second] <= nowDis) {
        break;
      }
      grid[newPoint.first][newPoint.second] = nowDis + 1;
      if (pushed[newPoint.first][newPoint.second] == false) {
        Q.push(newPoint);
        pushed[newPoint.first][newPoint.second] = true;
      }

      xm++;
    }
    newPoint = now;
    int yp = 1;
    while (yp <= k) {
      newPoint.second++;
      if (newPoint.second >= w) {
        break;
      }
      if (grid[newPoint.first][newPoint.second] <= nowDis) {
        break;
      }
      grid[newPoint.first][newPoint.second] = nowDis + 1;
      if (pushed[newPoint.first][newPoint.second] == false) {
        Q.push(newPoint);
        pushed[newPoint.first][newPoint.second] = true;
      }

      yp++;
    }
    newPoint = now;
    int ym = 1;
    while (ym <= k) {
      newPoint.second--;
      if (newPoint.second < 0) {
        break;
      }
      if (grid[newPoint.first][newPoint.second] <= nowDis) {
        break;
      }
      grid[newPoint.first][newPoint.second] = nowDis + 1;
      if (pushed[newPoint.first][newPoint.second] == false) {
        Q.push(newPoint);
        pushed[newPoint.first][newPoint.second] = true;
      }

      ym++;
    }
  }
  if (grid[go.first - 1][go.second - 1] == INT32_MAX) {
    cout << -1 << endl;
  } else {
    cout << grid[go.first - 1][go.second - 1] << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <new>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

typedef long long ll;
typedef unsigned long long ull;
constexpr ll mop = 1000000007;
constexpr ll mop2 = 998244353;

using namespace std;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  pair<int, int> st, go;
  cin >> st.first >> st.second >> go.first >> go.second;
  vector<vector<int>> grid, pushed;
  grid.resize(h);
  pushed.resize(h);
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    grid[i].resize(w);
    pushed[i].resize(w);
    for (int j = 0; j < w; j++) {
      if (s[j] == '@') {
        grid[i][j] = INT32_MIN;
      } else {
        grid[i][j] = INT32_MAX;
      }
    }
  }

  queue<pair<int, int>> Q;
  st.first--;
  st.second--;
  Q.push(st);
  grid[st.first][st.second] = 0;
  pushed[st.first][st.second] = true;
  while (!Q.empty()) {
    pair<int, int> now = Q.front();
    Q.pop();
    int nowDis = grid[now.first][now.second];
    if (now.first == go.first - 1 && now.second == go.second - 1) {
      break;
    }
    pair<int, int> newPoint;

    newPoint = now;
    int xp = 1;
    while (xp <= k) {
      newPoint.first++;
      if (newPoint.first >= h) {
        break;
      }
      if (grid[newPoint.first][newPoint.second] <= nowDis) {
        break;
      }
      grid[newPoint.first][newPoint.second] = nowDis + 1;
      if (pushed[newPoint.first][newPoint.second] == false) {
        Q.push(newPoint);
      }
      xp++;
    }
    newPoint = now;
    int xm = 1;
    while (xm <= k) {
      newPoint.first--;
      if (newPoint.first < 0) {
        break;
      }
      if (grid[newPoint.first][newPoint.second] <= nowDis) {
        break;
      }
      grid[newPoint.first][newPoint.second] = nowDis + 1;
      if (pushed[newPoint.first][newPoint.second] == false) {
        Q.push(newPoint);
      }
      xm++;
    }
    newPoint = now;
    int yp = 1;
    while (yp <= k) {
      newPoint.second++;
      if (newPoint.second >= w) {
        break;
      }
      if (grid[newPoint.first][newPoint.second] <= nowDis) {
        break;
      }
      grid[newPoint.first][newPoint.second] = nowDis + 1;
      if (pushed[newPoint.first][newPoint.second] == false) {
        Q.push(newPoint);
      }
      yp++;
    }
    newPoint = now;
    int ym = 1;
    while (ym <= k) {
      newPoint.second--;
      if (newPoint.second < 0) {
        break;
      }
      if (grid[newPoint.first][newPoint.second] <= nowDis) {
        break;
      }
      grid[newPoint.first][newPoint.second] = nowDis + 1;
      if (pushed[newPoint.first][newPoint.second] == false) {
        Q.push(newPoint);
      }
      ym++;
    }
  }
  if (grid[go.first - 1][go.second - 1] == INT32_MAX) {
    cout << -1 << endl;
  } else {
    cout << grid[go.first - 1][go.second - 1] << endl;
  }
}