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

#include <iostream>
#include <queue>
#include <sstream>
#include <utility>

#define INF 100000000

using namespace std;

int delta_x[4] = {1, -1, 0, 0};
int delta_y[4] = {0, 0, 1, -1};
int H, W, K;
int SX, SY, GX, GY;

void print_board(int **mapp) {

  for (int i = 0; i < H + 2; i++) {
    ostringstream oss;
    for (int j = 0; j < W + 2; j++) {
      oss << mapp[i][j] << " ";
    }
    cout << oss.str() << endl;
  }
}

int main() {
  scanf("%d %d %d", &H, &W, &K);
  scanf("%d %d %d %d", &SX, &SY, &GX, &GY);
  char a[H][W + 1];
  for (int h = 0; h < H; h++) {
    scanf("%s", a[h]);
  }

  int map[H + 2][W + 2];
  int *mapp[W + 2];
  for (int i = 0; i < H + 2; i++) {
    mapp[i] = map[i];
  }
  // make wall of up and down
  for (int w = 0; w < W + 2; w++) {
    map[0][w] = map[H + 1][w] = -1;
  }
  for (int h = 1; h <= H; h++) {
    map[h][0] = map[h][W + 1] = -1; // wall of left and right
    for (int w = 1; w <= W; w++) {
      map[h][w] = a[h - 1][w - 1] == '.' ? INF : -1;
    }
  }

  queue<pair<int, int>> que;
  que.push(make_pair(SX, SY));
  map[SX][SY] = 0;
  pair<int, int> pos;

  int ct = 0;
  while (!que.empty()) {
    pos = que.front();
    que.pop();
    int x = pos.first;
    int y = pos.second;

    for (int i = 0; i < 4; i++) {
      int dx = delta_x[i];
      int dy = delta_y[i];

      for (int j = 1; j <= K; j++) {
        int xx = x + j * dx;
        int yy = y + j * dy;
        if (map[xx][yy] <= map[x][y]) {
          break;
        }
        if (map[xx][yy] == INF) {
          map[xx][yy] = map[x][y] + 1;
          que.push(make_pair(xx, yy));
        }
      }
    }
  }

  int ans = map[GX][GY];
  if (ans == INF) {
    ans = -1;
  }

  printf("%d\\n", ans);

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <sstream>
#include <utility>

#define INF 100000000

using namespace std;

int delta_x[4] = {1, -1, 0, 0};
int delta_y[4] = {0, 0, 1, -1};
int H, W, K;
int SX, SY, GX, GY;

void print_board(int **mapp) {

  for (int i = 0; i < H + 2; i++) {
    ostringstream oss;
    for (int j = 0; j < W + 2; j++) {
      oss << mapp[i][j] << " ";
    }
    cout << oss.str() << endl;
  }
}

int main() {
  scanf("%d %d %d", &H, &W, &K);
  scanf("%d %d %d %d", &SX, &SY, &GX, &GY);
  char a[H][W + 1];
  for (int h = 0; h < H; h++) {
    scanf("%s", a[h]);
  }

  int map[H + 2][W + 2];
  int *mapp[W + 2];
  for (int i = 0; i < H + 2; i++) {
    mapp[i] = map[i];
  }
  // make wall of up and down
  for (int w = 0; w < W + 2; w++) {
    map[0][w] = map[H + 1][w] = -1;
  }
  for (int h = 1; h <= H; h++) {
    map[h][0] = map[h][W + 1] = -1; // wall of left and right
    for (int w = 1; w <= W; w++) {
      map[h][w] = a[h - 1][w - 1] == '.' ? INF : -1;
    }
  }

  queue<pair<int, int>> que;
  que.push(make_pair(SX, SY));
  map[SX][SY] = 0;
  pair<int, int> pos;

  while (!que.empty()) {
    pos = que.front();
    que.pop();
    int x = pos.first;
    int y = pos.second;

    for (int i = 0; i < 4; i++) {
      int dx = delta_x[i];
      int dy = delta_y[i];

      for (int j = 1; j <= K; j++) {
        if (map[x + j * dx][y + j * dy] <= map[x][y]) {
          break;
        }
        map[x + j * dx][y + j * dy] = map[x][y] + 1;
        que.push(make_pair(x + j * dx, y + j * dy));
      }
    }
  }

  int ans = map[GX][GY];
  if (ans == INF) {
    ans = -1;
  }

  printf("%d\\n", ans);

  return 0;
}