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

#include <iostream>
#include <math.h>
#include <queue>
int kaki[1000000];
char p[1000000];
using namespace std;
int dflag[1000001];

int main() {

  // F
  int h, w, k;
  cin >> h >> w >> k;

  int i;
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  for (i = 0; i < h * w; i++) {
    cin >> p[i];
    kaki[i] = -1;
  }
  int j;
  queue<int> que;
  que.push(y1 + x1 * w);
  kaki[y1 + x1 * w] = 0;
  int place;
  int num = 0;
  while (que.size()) {
    place = que.front();
    que.pop();
    i = place;
    j = k;
    while ((i >= w) && (j > 0)) {
      i = i - w;
      j--;
      if (p[i] == '@')
        break;

      if (kaki[i] == -1) {
        que.push(i);
        kaki[i] = kaki[place] + 1;
        // cout << i << ' ' << kaki[i] << endl;
        if (i == y2 + x2 * w) {
          cout << kaki[i] << endl;
          return 0;
        }
      } else if (kaki[i] == kaki[place]) {
        break;
      }
    }
    i = place;
    j = k;
    while ((i % w >= 1) && (j > 0)) {
      i--;
      j--;
      if (p[i] == '@')
        break;
      if (kaki[i] == -1) {
        que.push(i);
        kaki[i] = kaki[place] + 1;
        // cout << i << ' ' << kaki[i] << endl;
        if (i == y2 + x2 * w) {
          cout << kaki[i] << endl;
          return 0;
        }
      } else if (kaki[i] == kaki[place]) {
        break;
      }
    }
    i = place;
    j = k;
    while ((i % w < w - 1) && (j > 0)) {
      i++;
      j--;
      if (p[i] == '@')
        break;
      if (kaki[i] == -1) {
        que.push(i);
        kaki[i] = kaki[place] + 1;
        // cout << i << ' ' << kaki[i] << endl;
        if (i == y2 + x2 * w) {
          cout << kaki[i] << endl;
          return 0;
        }
      } else if (kaki[i] == kaki[place]) {
        break;
      }
    }
    i = place;
    j = k;
    while ((i <= h * w - w - 1) && (j > 0)) {
      i = i + w;
      j--;
      if (p[i] == '@')
        break;
      if (kaki[i] == -1) {
        que.push(i);
        kaki[i] = kaki[place] + 1;
        // cout << i << ' ' << kaki[i] << endl;
        if (i == y2 + x2 * w) {
          cout << kaki[i] << endl;
          return 0;
        }
      } else if (kaki[i] == kaki[place]) {
        break;
      }
    }
  }
  cout << -1 << endl;
  return 0;

  // D
  /*
  int i, n;
  int a[200000];
  cin >> n;
  for (i = 0; i < n; i++) {
          cin >> a[i];
          dflag[a[i]]++;
  }
  int flag;
  if (n == 1) {
          cout << 1 << endl;
          return 0;
  }
  if (dflag[1] > 1) {
          cout << 0 << endl;
          return 0;
  }
  else if (dflag[1] == 1) {
          cout << 1 << endl;
          return 0;
  }
  int j;
  int counter = 0;
  for (i = 0; i < n; i++) {
          flag = 0;
          if (dflag[a[i]] > 1) {
                  flag = 1;
                  //cout << 'a' << a[i] << endl;
          }
          for (j = 2; j <= sqrt(a[i]); j++) {
                  if (a[i] % j == 0) {
                          if (dflag[j] > 0) {
                                  flag = 1;
                                  //cout << 'b' << a[i] << ':' << j << endl;
                          }
                          if (dflag[a[i] / j] > 0) {
                                  flag = 1;
                                  //cout << 'c' << a[i] << ':' << a[i] / j <<
  endl;
                          }
                  }
          }
          if (flag == 0) {
                  counter++;
          }

  }

  cout << counter << endl;
  return 0;
  */

  // C
  /*int x, n;
  cin >> x >> n;
  if (n == 0) {
          cout << x << endl;
          return 0;
  }
  int i, p[100], j;
  for (i = 0; i < n; i++) {
          cin >> p[i];
  }
  int flag;
  for (i = 0; i < 100; i++) {
          flag = 0;
          for (j = 0; j < n; j++) {
                  if (p[j] == x - i) {
                          flag = 1;
                          break;
                  }
          }
          if (flag == 0) {
                  cout << x - i << endl;
                  return 0;
          }
          flag = 0;
          for (j = 0; j < n; j++) {
                  if (p[j] == x + i) {
                          flag = 1;
                          break;
                  }
          }
          if (flag == 0) {
                  cout << x + i << endl;
                  return 0;
          }
  }
  */

  // B
  /*int x, y;
  cin >> x >> y;
  if ((y % 2 == 0) && (y - 2 * x >= 0) && (4 * x - y >= 0)) {
          cout << "Yes" << endl;
  }
  else {
          cout << "No" << endl;
  }
  return 0;
  */

  // A
  /*int n;
  int i;
  for (i = 0; i < 4; i++) {
          cin >> n;
          if (n == 0)cout << i + 1 << endl;
  }
  return 0;
  */
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
int kaki[1000000];
char p[1000000];
using namespace std;
int main() {

  // F
  int h, w, k;
  cin >> h >> w >> k;

  int i;
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  for (i = 0; i < h * w; i++) {
    cin >> p[i];
    kaki[i] = -1;
  }
  int j;
  queue<int> que;
  que.push(y1 + x1 * w);
  kaki[y1 + x1 * w] = 0;
  int place;
  int num = 0;
  while (que.size()) {
    place = que.front();
    que.pop();
    i = place;
    j = k;
    while ((i >= w) && (j > 0)) {
      i = i - w;
      j--;
      if (p[i] == '@')
        break;
      if (kaki[i] == -1) {
        que.push(i);
        kaki[i] = kaki[place] + 1;
        // cout << i << ' ' << kaki[i] << endl;
        if (i == y2 + x2 * w) {
          cout << kaki[i] << endl;
          return 0;
        }
      }
    }
    i = place;
    j = k;
    while ((i % w >= 1) && (j > 0)) {
      i--;
      j--;
      if (p[i] == '@')
        break;
      if (kaki[i] == -1) {
        que.push(i);
        kaki[i] = kaki[place] + 1;
        // cout << i << ' ' << kaki[i] << endl;
        if (i == y2 + x2 * w) {
          cout << kaki[i] << endl;
          return 0;
        }
      }
    }
    i = place;
    j = k;
    while ((i % w < w - 1) && (j > 0)) {
      i++;
      j--;
      if (p[i] == '@')
        break;
      if (kaki[i] == -1) {
        que.push(i);
        kaki[i] = kaki[place] + 1;
        // cout << i << ' ' << kaki[i] << endl;
        if (i == y2 + x2 * w) {
          cout << kaki[i] << endl;
          return 0;
        }
      }
    }
    i = place;
    j = k;
    while ((i <= h * w - w - 1) && (j > 0)) {
      i = i + w;
      j--;
      if (p[i] == '@')
        break;
      if (kaki[i] == -1) {
        que.push(i);
        kaki[i] = kaki[place] + 1;
        // cout << i << ' ' << kaki[i] << endl;
        if (i == y2 + x2 * w) {
          cout << kaki[i] << endl;
          return 0;
        }
      }
    }
  }
  cout << -1 << endl;
  return 0;

  // D

  // C
  /*int x, n;
  cin >> x >> n;
  if (n == 0) {
          cout << x << endl;
          return 0;
  }
  int i, p[100], j;
  for (i = 0; i < n; i++) {
          cin >> p[i];
  }
  int flag;
  for (i = 0; i < 100; i++) {
          flag = 0;
          for (j = 0; j < n; j++) {
                  if (p[j] == x - i) {
                          flag = 1;
                          break;
                  }
          }
          if (flag == 0) {
                  cout << x - i << endl;
                  return 0;
          }
          flag = 0;
          for (j = 0; j < n; j++) {
                  if (p[j] == x + i) {
                          flag = 1;
                          break;
                  }
          }
          if (flag == 0) {
                  cout << x + i << endl;
                  return 0;
          }
  }
  */

  // B
  /*int x, y;
  cin >> x >> y;
  if ((y % 2 == 0) && (y - 2 * x >= 0) && (4 * x - y >= 0)) {
          cout << "Yes" << endl;
  }
  else {
          cout << "No" << endl;
  }
  return 0;
  */

  // A
  /*int n;
  int i;
  for (i = 0; i < 4; i++) {
          cin >> n;
          if (n == 0)cout << i + 1 << endl;
  }
  return 0;
  */
}