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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long mod = 998244353;
int main() {
  int h, w, k;
  int sx, sy, gx, gy;
  char c[1000000];
  cin >> h >> w >> k;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  int s = sx * w + sy, g = gx * w + gy;
  for (int i = 0; i < h * w; i++) {
    cin >> c[i];
  }
  int d[1000000];
  fill(d, d + h * w, 1e9 + 7);
  queue<pair<int, pair<int, int>>> que;
  que.push(make_pair(s, make_pair(0, -1)));
  d[s] = 0;
  while (!que.empty()) {
    pair<int, pair<int, int>> hoge = que.front();
    int now = hoge.first;
    int dis = hoge.second.first;
    int dir = hoge.second.second;
    que.pop();
    int cnt = 0;
    int mow = now;
    mow -= w;
    while (cnt < k && mow >= 0 && dir != 0) {
      if (c[mow] == '@') {
        break;
      } else {
        if (mow == g) {
          cout << dis + 1 << endl;
          return 0;
        }
        if (d[mow] == 1e9 + 7) {
          d[mow] = dis + 1;
          que.push(make_pair(mow, make_pair(d[mow], 1)));
        } else if (d[mow] != dis + 1) {
          break;
        }
      }
      cnt++;
      mow -= w;
    }
    cnt = 0;
    mow = now;
    mow += w;
    while (cnt < k && mow < h * w && dir != 1) {
      if (c[mow] == '@') {
        break;
      } else {
        if (mow == g) {
          cout << dis + 1 << endl;
          return 0;
        }
        if (d[mow] == 1e9 + 7) {
          d[mow] = dis + 1;
          que.push(make_pair(mow, make_pair(d[mow], 0)));
        } else if (d[mow] != dis + 1) {
          break;
        }
      }
      cnt++;
      mow += w;
    }
    cnt = 0;
    mow = now;
    mow--;
    while (cnt < k && mow % w < now % w && dir != 2) {
      if (c[mow] == '@') {
        break;
      } else {
        if (mow == g) {
          cout << dis + 1 << endl;
          return 0;
        }
        if (d[mow] == 1e9 + 7) {
          d[mow] = dis + 1;
          que.push(make_pair(mow, make_pair(d[mow], 3)));
        } else if (d[mow] != dis + 1) {
          break;
        }
      }
      cnt++;
      mow--;
    }
    cnt = 0;
    mow = now;
    mow++;
    while (cnt < k && mow % w > now % w && dir != 3) {
      if (c[mow] == '@') {
        break;
      } else {
        if (mow == g) {
          cout << dis + 1 << endl;
          return 0;
        }
        if (d[mow] == 1e9 + 7) {
          d[mow] = dis + 1;
          que.push(make_pair(mow, make_pair(d[mow], 2)));
        } else if (d[mow] != dis + 1) {
          break;
        }
      }
      cnt++;
      mow++;
    }
  }
  cout << -1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long mod = 998244353;
int main() {
  int h, w, k;
  int sx, sy, gx, gy;
  char c[1000000];
  cin >> h >> w >> k;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  int s = sx * w + sy, g = gx * w + gy;
  for (int i = 0; i < h * w; i++) {
    cin >> c[i];
  }
  int d[1000000];
  fill(d, d + h * w, 1e9 + 7);
  queue<pair<int, pair<int, int>>> que;
  que.push(make_pair(s, make_pair(0, -1)));
  d[s] = 0;
  while (!que.empty()) {
    pair<int, pair<int, int>> hoge = que.front();
    int now = hoge.first;
    int dis = hoge.second.first;
    int dir = hoge.second.second;
    que.pop();
    int cnt = 0;
    int mow = now;
    mow -= w;
    while (cnt < k && mow >= 0 && dir != 0) {
      if (c[mow] == '@') {
        break;
      } else {
        if (mow == g) {
          cout << dis + 1 << endl;
          return 0;
        }
        if (d[mow] == 1e9 + 7) {
          d[mow] = dis + 1;
          que.push(make_pair(mow, make_pair(d[mow], 1)));
        }
      }
      cnt++;
      mow -= w;
    }
    cnt = 0;
    mow = now;
    mow += w;
    while (cnt < k && mow < h * w && dir != 1) {
      if (c[mow] == '@') {
        break;
      } else {
        if (mow == g) {
          cout << dis + 1 << endl;
          return 0;
        }
        if (d[mow] == 1e9 + 7) {
          d[mow] = dis + 1;
          que.push(make_pair(mow, make_pair(d[mow], 0)));
        }
      }
      cnt++;
      mow += w;
    }
    cnt = 0;
    mow = now;
    mow--;
    while (cnt < k && mow % w < now % w && dir != 2) {
      if (c[mow] == '@') {
        break;
      } else {
        if (mow == g) {
          cout << dis + 1 << endl;
          return 0;
        }
        if (d[mow] == 1e9 + 7) {
          d[mow] = dis + 1;
          que.push(make_pair(mow, make_pair(d[mow], 3)));
        }
      }
      cnt++;
      mow--;
    }
    cnt = 0;
    mow = now;
    mow++;
    while (cnt < k && mow % w > now % w && dir != 3) {
      if (c[mow] == '@') {
        break;
      } else {
        if (mow == g) {
          cout << dis + 1 << endl;
          return 0;
        }
        if (d[mow] == 1e9 + 7) {
          d[mow] = dis + 1;
          que.push(make_pair(mow, make_pair(d[mow], 2)));
        }
      }
      cnt++;
      mow++;
    }
  }
  cout << -1 << endl;
  return 0;
}