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

#include <bits/stdc++.h>
using namespace std;

#define INF 2e9
#define endl "\\n"
#define mp make_pair

const int maxn = 12;

vector<vector<int>> pond(maxn, vector<int>(maxn, 0));
vector<vector<int>> moves(maxn, vector<int>(maxn, INF));
vector<vector<bool>> visited(maxn, vector<bool>(maxn, false));

int h, w, k;
int x, y, xx, yy;

bool valid(int row, int col) {
  return (row >= 0 and row < h and col >= 0 and col < w and
          pond[row][col] == 1);
}

void bfs(int source_row, int source_col) {
  moves[source_row][source_col] = 0;
  queue<pair<int, int>> q;
  q.push(mp(source_row, source_col));

  while (!q.empty()) {
    int now1 = q.front().first;
    int now2 = q.front().second;
    q.pop();
    visited[now1][now2] = true;

    for (int i = 1; i <= k; i++) {

      int row = now1;
      int col = i + now2;

      if (valid(row, col) == false or moves[row][col] < moves[now1][now2] + 1)
        break;
      if (visited[row][col] == false) {
        q.push(mp(row, col));
        visited[row][col] = true;
        moves[row][col] = moves[now1][now2] + 1;
      }
    }

    for (int i = 1; i <= k; i++) {

      int row = now1;
      int col = now2 - i;

      if (valid(row, col) == false or moves[row][col] < moves[now1][now2] + 1)
        break;
      if (visited[row][col] == false) {
        q.push(mp(row, col));
        visited[row][col] = true;
        moves[row][col] = moves[now1][now2] + 1;
      }
    }

    for (int i = 1; i <= k; i++) {

      int row = now1 - i;
      int col = now2;

      if (valid(row, col) == false or moves[row][col] < moves[now1][now2] + 1)
        break;
      if (visited[row][col] == false) {
        q.push(mp(row, col));
        visited[row][col] = true;
        moves[row][col] = moves[now1][now2] + 1;
      }
    }

    for (int i = 1; i <= k; i++) {

      int row = now1 + i;
      int col = now2;

      if (valid(row, col) == false or moves[row][col] < moves[now1][now2] + 1)
        break;
      if (visited[row][col] == false) {
        q.push(mp(row, col));
        visited[row][col] = true;
        moves[row][col] = moves[now1][now2] + 1;
      }
    }
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // #define int ll

  cin >> h >> w >> k;
  cin >> x >> y >> xx >> yy;

  vector<vector<int>> aaa(h + 5, vector<int>(w + 5, 0));
  vector<vector<int>> hhh(h + 5, vector<int>(w + 5, INF));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      if (c == '.')
        aaa[i][j] = 1;
      else
        aaa[i][j] = 0;
      hhh[i][j] = INF;
    }
  }

  swap(aaa, pond);
  swap(hhh, moves);
  vector<vector<bool>> ww(h, vector<bool>(w, false));
  swap(visited, ww);

  x--;
  xx--;
  y--;
  yy--;

  bfs(x, y);

  if (moves[xx][yy] == INF) {
    cout << -1 << endl;
  }

  else
    cout << moves[xx][yy] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define INF 2e9
#define endl "\\n"
#define mp make_pair

const int maxn = 12;

vector<vector<int>> pond(maxn, vector<int>(maxn, 0));
vector<vector<int>> moves(maxn, vector<int>(maxn, INF));
// int pond[1001][1001];
// int moves[1001][1001];

int h, w, k;
int x, y, xx, yy;

bool valid(int row, int col) {
  return (row >= 0 and row < h and col >= 0 and col < w and
          pond[row][col] == 1);
}

void bfs(int source_row, int source_col) {
  moves[source_row][source_col] = 0;
  queue<pair<int, int>> q;
  q.push(mp(source_row, source_col));

  while (!q.empty()) {
    int now1 = q.front().first;
    int now2 = q.front().second;
    q.pop();

    for (int i = 1; i <= k; i++) {

      int row = now1;
      int col = i + now2;

      if (valid(row, col) == false)
        break;
      if (moves[row][col] > moves[now1][now2] + 1) {
        q.push(mp(row, col));
        moves[row][col] = moves[now1][now2] + 1;
      }
    }

    for (int i = 1; i <= k; i++) {

      int row = now1;
      int col = now2 - i;

      if (valid(row, col) == false)
        break;
      if (moves[row][col] > moves[now1][now2] + 1) {
        q.push(mp(row, col));
        moves[row][col] = moves[now1][now2] + 1;
      }
    }

    for (int i = 1; i <= k; i++) {

      int row = now1 - i;
      int col = now2;

      if (valid(row, col) == false)
        break;
      if (moves[row][col] > moves[now1][now2] + 1) {
        q.push(mp(row, col));
        moves[row][col] = moves[now1][now2] + 1;
      }
    }

    for (int i = 1; i <= k; i++) {

      int row = now1 + i;
      int col = now2;

      if (valid(row, col) == false)
        break;
      if (moves[row][col] > moves[now1][now2] + 1) {
        q.push(mp(row, col));
        moves[row][col] = moves[now1][now2] + 1;
      }
    }
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // #define int ll

  cin >> h >> w >> k;
  cin >> x >> y >> xx >> yy;
  vector<vector<int>> aaa(h + 5, vector<int>(w + 5, 0));
  vector<vector<int>> hhh(h + 5, vector<int>(w + 5, INF));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      if (c == '.')
        aaa[i][j] = 1;
      else
        aaa[i][j] = 0;
      hhh[i][j] = INF;
    }
  }

  swap(aaa, pond);
  swap(hhh, moves);

  x--;
  xx--;
  y--;
  yy--;

  bfs(x, y);

  if (moves[xx][yy] == INF) {
    cout << -1 << endl;
  }

  else
    cout << moves[xx][yy] << endl;

  return 0;
}