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

#include <algorithm>
#include <array>
#include <bitset>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>

using namespace std;

const int xp[] = {1, 0, -1, 0};
const int yp[] = {0, 1, 0, -1};

vector<vector<bool>> l;
vector<vector<int>> prva[4];
int h, w, k;
vector<vector<int>> dist;

int find(int ind, int x, int y) {
  if (prva[ind][x][y] == -1) {
    return -1;
  }
  if (ind % 2) {
    if (prva[ind][x][y] == y) {
      return y;
    }
    return prva[ind][x][y] = find(ind, x, prva[ind][x][y]);
  } else {
    if (prva[ind][x][y] == x) {
      return x;
    }
    return prva[ind][x][y] = find(ind, prva[ind][x][y], y);
  }
}

void scan(int &a) {
  a = 0;
  char c = getchar();
  while (c >= '0' && c <= '9') {
    a *= 10;
    a += c - '0';
    c = getchar();
  }
}

void bfs(int x, int y) {
  queue<pair<int, int>> q;
  q.push({x, y});
  pair<int, int> p;
  int xs, ys, xd, yd;
  while (!q.empty()) {
    p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      xs = p.first + xp[i];
      ys = p.second + yp[i];
      if (xs > -1 && ys > -1 && xs < h && ys < w) {
        if (i % 2) {
          ys = find(i, xs, ys);
        } else {
          xs = find(i, xs, ys);
        }
        //				printf("proso\\n");
        while (xs > -1 && ys > -1 && xs < h && ys < w &&
               abs(p.first - xs) <= k && abs(p.second - ys) <= k && l[xs][ys]) {
          //					printf("%d %d\\n", xs, ys);
          dist[xs][ys] = dist[p.first][p.second] + 1;
          q.push({xs, ys});
          for (int j = 0; j < 4; j++) {
            xd = xs + xp[j];
            yd = ys + yp[j];
            if (xd > -1 && yd > -1 && xd < h && yd < w) {
              prva[j][xs][ys] = prva[j][xd][yd];
            } else {
              prva[j][xs][ys] = -1;
            }
          }
          if (i % 2) {
            ys = find(i, xs, ys);
          } else {
            xs = find(i, xs, ys);
          }
        }
        //				printf("dalje\\n");
      }
    }
  }
}

int main() {
  scan(h);
  scan(w);
  scan(k);
  int x1, y1, x2, y2;
  scan(x1);
  scan(y1);
  scan(x2);
  scan(y2);
  x1--;
  x2--;
  y1--;
  y2--;
  vector<bool> vi;
  vector<int> v;
  vector<int> x, y;
  char c;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      c = getchar();
      if (c == '.') {
        vi.push_back(1);
      } else {
        vi.push_back(0);
      }
      if (!i) {
        v.push_back(-1);
      }
      x.push_back(i);
      y.push_back(j);
    }
    getchar();
    prva[0].push_back(x);
    prva[1].push_back(y);
    prva[2].push_back(x);
    prva[3].push_back(y);
    x.clear();
    y.clear();
    l.push_back(vi);
    dist.push_back(v);
    vi.clear();
  }
  dist[x1][y1] = 0;
  bfs(x1, y1);
  /*	for(int i=0; i<h; i++){
                  for(int j=0; j<w; j++){
                          printf("%d ", dist[i][j]);
                  }
                  printf("\\n");
          }*/
  printf("%d\\n", dist[x2][y2]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>

using namespace std;

const int xp[] = {1, 0, -1, 0};
const int yp[] = {0, 1, 0, -1};

vector<vector<bool>> l;
int h, w, k;
vector<vector<int>> dist;

void scan(int &a) {
  a = 0;
  char c = getchar();
  while (c >= '0' && c <= '9') {
    a *= 10;
    a += c - '0';
    c = getchar();
  }
}

void bfs(int x, int y) {
  queue<pair<int, int>> q;
  q.push({x, y});
  pair<int, int> p;
  int xs, ys;
  while (!q.empty()) {
    p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        xs = p.first + xp[i] * j;
        ys = p.second + yp[i] * j;
        if (xs > -1 && ys > -1 && xs < h && ys < w && l[xs][ys]) {
          if (dist[xs][ys] == -1) {
            dist[xs][ys] = dist[p.first][p.second] + 1;
            q.push({xs, ys});
          }
        } else {
          break;
        }
      }
    }
  }
}

int main() {
  scan(h);
  scan(w);
  scan(k);
  int x1, y1, x2, y2;
  scan(x1);
  scan(y1);
  scan(x2);
  scan(y2);
  x1--;
  x2--;
  y1--;
  y2--;
  vector<bool> vi;
  vector<int> v;
  char c;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      c = getchar();
      if (c == '.') {
        vi.push_back(1);
      } else {
        vi.push_back(0);
      }
      if (!i) {
        v.push_back(-1);
      }
    }
    getchar();
    l.push_back(vi);
    dist.push_back(v);
    vi.clear();
  }
  dist[x1][y1] = 0;
  bfs(x1, y1);
  /*	for(int i=0; i<h; i++){
                  for(int j=0; j<w; j++){
                          printf("%d ", dist[i][j]);
                  }
                  printf("\\n");
          }*/
  printf("%d\\n", dist[x2][y2]);
  return 0;
}