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

#include <bitset>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int MAX = 1e6;
struct koor {
  int r, c, val;
};
int main() {
  int R, C, k, ans;
  cin >> R >> C >> k;
  int *row, *column;
  row = new int[R], column = new int[C];
  memset(row, 0, sizeof(row)), memset(column, 0, sizeof(column));
  bool visited[R][C];
  memset(visited, 0, sizeof(visited));
  string area[R];
  koor mula, akhir;
  mula.val = 0;

  cin >> mula.r >> mula.c >> akhir.r >> akhir.c;
  --mula.r, --mula.c, --akhir.r, --akhir.c;

  for (int i = 0; i < R; i++)
    cin >> area[i];

  bool ketemu = 0;
  queue<koor> q;
  q.push(mula);
  while (!q.empty()) {
    koor now = q.front();
    q.pop();
    if (now.r == akhir.r && now.c == akhir.c) {
      ketemu = 1, ans = now.val;
      break;
    }

    if (column[now.c] < R)
      for (int i = now.r - 1, x = k; x && i >= 0 && area[i][now.c] == '.';
           i--, x--) { // ke atas
        if (!visited[i][now.c])
          visited[i][now.c] = 1, column[now.c]++, row[i]++,
          q.push({i, now.c, now.val + 1});
      }

    if (column[now.c] < R)
      for (int i = now.r + 1, x = k; x && i < R && area[i][now.c] == '.';
           i++, x--) {
        if (!visited[i][now.c])
          visited[i][now.c] = 1, column[now.c]++, row[i]++,
          q.push({i, now.c, now.val + 1});
      }

    if (row[now.r] < C)
      for (int i = now.c - 1, x = k; x && i >= 0 && area[now.r][i] == '.';
           i--, x--) {
        if (!visited[now.r][i])
          visited[now.r][i] = 1, column[i]++, row[now.r]++,
          q.push({now.r, i, now.val + 1});
      }

    if (row[now.r] < C)
      for (int i = now.c + 1, x = k; x && i < C && area[now.r][i] == '.';
           i++, x--) {
        if (!visited[now.r][i])
          visited[now.r][i] = 1, column[i]++, row[now.r]++,
          q.push({now.r, i, now.val + 1});
      }
  }
  if (!ketemu)
    cout << "-1\\n";
  else
    cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
struct koor {
  int r, c, val;
};
int main() {
  int R, C, k, ans;
  cin >> R >> C >> k;
  bool visited[R][C];
  memset(visited, 0, sizeof(visited));
  string area[R];
  koor mula, akhir;
  mula.val = 0;

  cin >> mula.r >> mula.c >> akhir.r >> akhir.c;
  --mula.r, --mula.c, --akhir.r, --akhir.c;

  for (int i = 0; i < R; i++)
    cin >> area[i];

  bool ketemu = 0;
  queue<koor> q;
  q.push(mula);
  while (!q.empty()) {
    koor now = q.front();
    q.pop();
    if (now.r == akhir.r && now.c == akhir.c) {
      ketemu = 1, ans = now.val;
      break;
    }

    for (int i = now.r - 1, x = k; x && i >= 0 && area[i][now.c] == '.';
         i--, x--) { // ke atas
      if (!visited[i][now.c])
        visited[i][now.c] = 1, q.push({i, now.c, now.val + 1});
    }
    for (int i = now.r + 1, x = k; x && i < R && area[i][now.c] == '.';
         i++, x--) {
      if (!visited[i][now.c])
        visited[i][now.c] = 1, q.push({i, now.c, now.val + 1});
    }
    for (int i = now.c - 1, x = k; x && i >= 0 && area[now.r][i] == '.';
         i--, x--) {
      if (!visited[now.r][i])
        visited[now.r][i] = 1, q.push({now.r, i, now.val + 1});
    }
    for (int i = now.c + 1, x = k; x && i < C && area[now.r][i] == '.';
         i++, x--) {
      if (!visited[now.r][i])
        visited[now.r][i] = 1, q.push({now.r, i, now.val + 1});
    }
  }
  if (!ketemu)
    cout << "-1\\n";
  else
    cout << ans << endl;
}