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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
const int N = 2e6 + 5;
int h, w;
int dec(int x, int y) { return x * w + y; }
int getx(int s) { return s / w; }
int gety(int s) { return s % w; }

int g[N];
int d[N];
int vis[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int k;
  cin >> h >> w >> k;
  int x, y, xx, yy;
  cin >> x >> y >> xx >> yy;
  x--;
  y--;
  xx--;
  yy--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      if (c == '.')
        g[dec(i, j)] = 1;
      else
        g[dec(i, j)] = -1;
    }
  }
  int s = dec(x, y);
  int en = dec(xx, yy);
  queue<int> q;
  d[s] = 0;
  vis[s] = 5;
  q.push(s);
  while (!q.empty()) {
    int node = q.front();
    q.pop();
    int nx = getx(node);
    int ny = gety(node);
    for (int i = 1; i <= k; i++) {
      if (nx + i >= h)
        break;
      int trt = dec(nx + i, ny);
      if (g[trt] == -1)
        break;
      if (k > 500 and (vis[trt] == 1 or vis[trt] == 2))
        break;
      ;
      if (vis[trt])
        continue;
      vis[trt] = 1;
      d[trt] = d[node] + 1;
      q.push(trt);
    }
    for (int i = 1; i <= k; i++) {
      if (nx - i < 0)
        break;
      int trt = dec(nx - i, ny);
      if (g[trt] == -1)
        break;
      if (k > 500 and (vis[trt] == 1 or vis[trt] == 2))
        break;
      if (vis[trt])
        continue;
      vis[trt] = 2;
      d[trt] = d[node] + 1;
      q.push(trt);
    }
    for (int i = 1; i <= k; i++) {
      if (ny + i >= w)
        break;
      int trt = dec(nx, ny + i);
      if (g[trt] == -1)
        break;
      if (k > 500 and (vis[trt] == 3 or vis[trt] == 4))
        break;
      ;
      if (vis[trt])
        continue;
      vis[trt] = 3;
      d[trt] = d[node] + 1;
      q.push(trt);
    }
    for (int i = 1; i <= k; i++) {
      if (ny - i < 0)
        break;
      int trt = dec(nx, ny - i);
      if (g[trt] == -1)
        break;
      if (k > 500 and (vis[trt] == 3 or vis[trt] == 4))
        break;
      ;
      if (vis[trt])
        continue;
      vis[trt] = 4;
      d[trt] = d[node] + 1;
      q.push(trt);
    }
  }
  if (vis[en])
    cout << d[en] << endl;
  else
    cout << -1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
const int N = 2e6 + 5;
int h, w;
int dec(int x, int y) { return x * w + y; }
int getx(int s) { return s / w; }
int gety(int s) { return s % w; }

int g[N];
int d[N];
bool vis[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int k;
  cin >> h >> w >> k;
  int x, y, xx, yy;
  cin >> x >> y >> xx >> yy;
  x--;
  y--;
  xx--;
  yy--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      if (c == '.')
        g[dec(i, j)] = 1;
      else
        g[dec(i, j)] = -1;
    }
  }
  int s = dec(x, y);
  int en = dec(xx, yy);
  queue<int> q;
  d[s] = 0;
  vis[s] = true;
  q.push(s);
  while (!q.empty()) {
    int node = q.front();
    q.pop();
    int nx = getx(node);
    int ny = gety(node);
    for (int i = 1; i <= k; i++) {
      if (nx + i >= h)
        break;
      int trt = dec(nx + i, ny);
      if (g[trt] == -1)
        break;
      if (vis[trt])
        continue;
      vis[trt] = true;
      d[trt] = d[node] + 1;
      q.push(trt);
    }
    for (int i = 1; i <= k; i++) {
      if (nx - i < 0)
        break;
      int trt = dec(nx - i, ny);
      if (g[trt] == -1)
        break;
      if (vis[trt])
        continue;
      vis[trt] = true;
      d[trt] = d[node] + 1;
      q.push(trt);
    }
    for (int i = 1; i <= k; i++) {
      if (ny + i >= w)
        break;
      int trt = dec(nx, ny + i);
      if (g[trt] == -1)
        break;
      if (vis[trt])
        continue;
      vis[trt] = true;
      d[trt] = d[node] + 1;
      q.push(trt);
    }
    for (int i = 1; i <= k; i++) {
      if (ny - i < 0)
        break;
      int trt = dec(nx, ny - i);
      if (g[trt] == -1)
        break;
      if (vis[trt])
        continue;
      vis[trt] = true;
      d[trt] = d[node] + 1;
      q.push(trt);
    }
  }
  if (vis[en])
    cout << d[en] << endl;
  else
    cout << -1 << endl;
  return 0;
}