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

#include <iostream>
#include <queue>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

int main() {

  int H, W, K;
  int x, y, gx, gy;

  cin >> H >> W >> K;
  cin >> x >> y >> gx >> gy;

  x--;
  y--;
  gx--;
  gy--;

  vector<vector<bool>> masu(H, vector<bool>(W, false));
  vector<vector<int>> step(H, vector<int>(W, -1));

  rep(i, H) {
    rep(j, W) {
      char c;
      cin >> c;
      if (c == '.')
        masu[i][j] = true;
    }
  }

  /*
  cout << endl << x << y << endl;

  */

  queue<pair<pair<int, int>, int>> q;
  q.push({{x, y}, 15});
  step[x][y] = 0;

  while (!q.empty()) {
    auto tmp = q.front();
    q.pop();
    int i = tmp.first.first;
    int j = tmp.first.second;
    int dir = tmp.second;
    bool l = (dir & 1) == 1;
    bool r = (dir & 2) == 2;
    bool u = (dir & 4) == 4;
    bool d = (dir & 8) == 8;

    // cout << endl << i << "," << j << "," << dir <<":"<< l<<r<<u<<d<<endl;

    rep(k, K + 1) {
      if (k == 0)
        continue;

      if (u && i - k >= 0 && masu[i - k][j] && step[i - k][j] == -1) {
        step[i - k][j] = step[i][j] + 1;
        int nex = 1 | 2;
        if (k == K)
          nex |= 4;
        q.push({{i - k, j}, nex});
      } else if (u && i - k >= 0 && masu[i - k][j] &&
                 step[i - k][j] == step[i][j] + 1) {
        ;
      } else {
        u = false;
      }
      if (d && i + k < H && masu[i + k][j] == true && step[i + k][j] == -1) {
        step[i + k][j] = step[i][j] + 1;
        int nex = 1 | 2;
        if (k == K)
          nex |= 8;
        q.push({{i + k, j}, nex});
      } else if (d && i + k < H && masu[i + k][j] == true &&
                 step[i + k][j] == step[i][j] + 1) {
        ;
      } else {
        d = false;
      }
      if (l && j - k >= 0 && masu[i][j - k] == true && step[i][j - k] == -1) {
        step[i][j - k] = step[i][j] + 1;
        int nex = 4 | 8;
        if (k == K)
          nex |= 1;
        q.push({{i, j - k}, nex});
      } else if (l && j - k >= 0 && masu[i][j - k] == true &&
                 step[i][j - k] == step[i][j] + 1) {
        ;
      } else {
        l = false;
      }
      if (r && j + k < W && masu[i][j + k] == true && step[i][j + k] == -1) {
        step[i][j + k] = step[i][j] + 1;
        int nex = 4 | 8;
        if (k == K)
          nex |= 2;
        q.push({{i, j + k}, nex});
      } else if (r && j + k < W && masu[i][j + k] == true &&
                 step[i][j + k] == step[i][j] + 1) {
        ;
      } else {
        r = false;
      }

      if (!u && !d && !l && !r)
        break;
    }

    /*
    rep(i, H) {
            rep(j, W) {
                    if (!masu[i][j])cout << " @";
                    else printf("%2d", step[i][j]);
            }
            cout << endl;
    }
    cout << endl;
    */
  }

  cout << step[gx][gy] << endl;

  /*
  rep(i, H) {
          rep(j, W) {
                  cout << step[i][j];
          }
          cout << endl;
  }
  */
  // cin >> H;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

int main() {

  int H, W, K;
  int x, y, gx, gy;
  cin >> H >> W >> K;
  cin >> x >> y >> gx >> gy;

  x--;
  y--;
  gx--;
  gy--;

  vector<vector<bool>> masu(H, vector<bool>(W, false));
  vector<vector<int>> step(H, vector<int>(W, -1));

  rep(i, H) {
    rep(j, W) {
      char c;
      cin >> c;
      if (c == '.')
        masu[i][j] = true;
    }
  }

  /*
  cout << endl << x << y << endl;

  */

  queue<pair<int, int>> q;
  q.push({x, y});
  step[x][y] = 0;

  while (!q.empty()) {
    auto tmp = q.front();
    q.pop();
    int i = tmp.first;
    int j = tmp.second;
    bool l = true;
    bool r = true;
    bool u = true;
    bool d = true;

    /*
    rep(i, H) {
            rep(j, W) {
                    printf("%2d",step[i][j]);
            }
            cout << endl;
    }
    cout << endl;
    */

    rep(k, K + 1) {
      if (k == 0)
        continue;

      if (l && i - k >= 0 && masu[i - k][j] && step[i - k][j] == -1) {
        step[i - k][j] = step[i][j] + 1;
        q.push({i - k, j});
      } else if (l && i - k >= 0 && masu[i - k][j] &&
                 step[i - k][j] == step[i][j] + 1) {
        ;
      } else {
        l = false;
      }
      if (r && i + k < H && masu[i + k][j] == true && step[i + k][j] == -1) {
        step[i + k][j] = step[i][j] + 1;
        q.push({i + k, j});
      } else if (r && i + k < H && masu[i + k][j] == true &&
                 step[i + k][j] == step[i][j] + 1) {
        ;
      } else {
        r = false;
      }
      if (u && j - k >= 0 && masu[i][j - k] == true && step[i][j - k] == -1) {
        step[i][j - k] = step[i][j] + 1;
        q.push({i, j - k});
      } else if (u && j - k >= 0 && masu[i][j - k] == true &&
                 step[i][j - k] == step[i][j] + 1) {
        ;
      } else {
        u = false;
      }
      if (d && j + k < W && masu[i][j + k] == true && step[i][j + k] == -1) {
        step[i][j + k] = step[i][j] + 1;
        q.push({i, j + k});
      } else if (d && j + k < W && masu[i][j + k] == true &&
                 step[i][j + k] == step[i][j] + 1) {
        ;
      } else {
        d = false;
      }
    }
  }

  cout << step[gx][gy] << endl;

  /*
  rep(i, H) {
          rep(j, W) {
                  cout << step[i][j];
          }
          cout << endl;
  }
  */
  // cin >> H;

  return 0;
}