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

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

#define gap " "
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ok cout << "ok" << endl
#define dbg(x) cout << (#x) << " is " << x << endl
#define fastio                                                                 \\
  {                                                                            \\
    ios_base::sync_with_stdio(false);                                          \\
    cin.tie(NULL);                                                             \\
  }

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 1000009;
vector<string> vec;
int fx[] = {-1, 1, 0, 0};
int fy[] = {0, 0, -1, 1};

int row, col, k;
int level[N], vis[N];
int id(int r, int c) { return (r * col) + (c + 1); }
int bfs(int x1, int y1, int x2, int y2) {
  int idx, lv, i, sx, sy, tx, ty, j, lx, ly;
  queue<pii> final;
  final.push(mp(x1, y1));
  idx = id(x1, y1);
  level[idx] = 0;
  vis[idx] = 1;
  pii u;
  while (!final.empty()) {
    u = final.front();
    final.pop();
    lx = u.ff;
    ly = u.ss;
    lv = level[id(lx, ly)] + 1;

    for (i = 0; i < 4; i++) {
      sx = lx;
      sy = ly;
      for (j = 1; j <= k; j++) {
        tx = sx + fx[i];
        ty = sy + fy[i];
        idx = id(tx, ty);
        if (tx >= 0 && tx < row && ty >= 0 && ty < col && vec[tx][ty] == '.' &&
            (level[idx] < 1 || level[idx] >= lv)) {
          level[idx] = lv;
          if (vis[idx] == 0)
            final.push(mp(tx, ty));
          if (tx == x2 && ty == y2)
            return level[idx];
          sx = tx;
          sy = ty;
          vis[idx] = 1;
        } else {
          break;
        }
      }
    }
  }

  return -1;
}
void print() {
  int i, j, idx;
  for (i = 0; i < row; i++) {
    for (j = 0; j < col; j++) {
      if (vec[i][j] == '.') {
        idx = id(i, j);
        cout << level[idx];
      } else
        cout << vec[i][j];
    }
    cout << endl;
  }
}
int main() {
  fastio;
  string s;
  char ch;
  int i, ans, x1, y1, x2, y2, j;
  // scanf("%d %d %d",&row,&col,&k);//
  cin >> row >> col >> k;
  // scanf("%d %d %d %d",&x1,&y1,&x2,&y2);//
  cin >> x1 >> y1 >> x2 >> y2;

  x1--;
  y1--;
  x2--;
  y2--; // for 0 based
  for (i = 0; i < row; i++) {
    cin >> s;
    vec.pb(s);
  }
  ans = bfs(x1, y1, x2, y2);
  // print();
  cout << ans << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

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

#define gap " "
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ok cout << "ok" << endl
#define dbg(x) cout << (#x) << " is " << x << endl
#define fastio                                                                 \\
  {                                                                            \\
    ios_base::sync_with_stdio(false);                                          \\
    cin.tie(NULL);                                                             \\
  }

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 1000009;
vector<string> vec;
int fx[] = {-1, 1, 0, 0};
int fy[] = {0, 0, -1, 1};

int row, col, k;
int level[N];
int id(int r, int c) { return (r * col) + (c + 1); }
int bfs(int x1, int y1, int x2, int y2) {
  int idx, lv, i, sx, sy, tx, ty, j, lx, ly;
  queue<pii> final;
  final.push(mp(x1, y1));
  idx = id(x1, y1);
  level[idx] = 0;

  pii u;
  while (!final.empty()) {
    u = final.front();
    final.pop();
    lx = u.ff;
    ly = u.ss;
    lv = level[id(lx, ly)] + 1;

    for (i = 0; i < 4; i++) {
      sx = lx;
      sy = ly;
      for (j = 1; j <= k; j++) {
        tx = sx + fx[i];
        ty = sy + fy[i];
        idx = id(tx, ty);
        if (tx >= 0 && tx < row && ty >= 0 && ty < col && vec[tx][ty] == '.' &&
            (level[idx] < 1 || level[idx] >= lv)) {
          level[idx] = lv;
          final.push(mp(tx, ty));
          if (tx == x2 && ty == y2)
            return level[idx];
          sx = tx;
          sy = ty;
        } else {
          break;
        }
      }
    }
  }

  return -1;
}
void print() {
  int i, j, idx;
  for (i = 0; i < row; i++) {
    for (j = 0; j < col; j++) {
      if (vec[i][j] == '.') {
        idx = id(i, j);
        cout << level[idx];
      } else
        cout << vec[i][j];
    }
    cout << endl;
  }
}
int main() {
  // fastio;
  string s;
  char ch;
  int i, ans, x1, y1, x2, y2, j;
  scanf("%d %d %d", &row, &col, &k);        // cin>>row>>col>>k;
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2); // cin>>x1>>y1>>x2>>y2;

  x1--;
  y1--;
  x2--;
  y2--; // for 0 based
  for (i = 0; i < row; i++) {
    getchar();
    s = "";
    for (j = 0; j < col; j++) {
      scanf("%c", &ch); // cin>>s;
      s += ch;
    }
    // cout<<s<<endl;
    vec.pb(s);
  }
  ans = bfs(x1, y1, x2, y2);
  // print();
  printf("%d\\n", ans); // cout<<ans<<'\\n';
  return 0;
}