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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long int LL;
typedef pair<int, int> P;
typedef pair<LL, int> LP;
const int INF = 1 << 30;
const LL MAX = 1e9 + 7;

void array_show(int *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%d%c", array[i], (i != array_n - 1 ? middle : '\\n'));
}
void array_show(LL *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%lld%c", array[i], (i != array_n - 1 ? middle : '\\n'));
}
void array_show(vector<int> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%d%c", vec_s[i], (i != vec_n - 1 ? middle : '\\n'));
}
void array_show(vector<LL> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%lld%c", vec_s[i], (i != vec_n - 1 ? middle : '\\n'));
}

vector<vector<int>> dis;
vector<vector<char>> used;
queue<P> q1;

int main() {
  int n, m, p;
  int i, j, k;
  int a, b, c;
  int x, y;
  string sa;
  cin >> n >> m >> p;
  cin >> a >> b >> x >> y;
  a--, b--, x--, y--;
  dis.assign(n, vector<int>(m, INF));
  used.assign(n, vector<char>(m, 0));
  for (i = 0; i < n; i++) {
    cin >> sa;
    for (j = 0; j < m; j++) {
      if (sa[j] == '@')
        dis[i][j] = -1;
    }
  }
  q1.push(make_pair(a, b));
  dis[a][b] = 0;
  for (i = 0; !q1.empty(); i++) {
    j = q1.size();
    while (j--) {
      a = q1.front().first, b = q1.front().second, q1.pop();
      if (dis[a][b] != i || used[a][b])
        continue;
      used[a][b] = true;
      for (k = a + 1; k < n && k <= a + p; k++) {
        if (dis[k][b] <= i)
          break;
        dis[k][b] = i + 1;
        q1.push(make_pair(k, b));
      }
      for (k = a - 1; k >= 0 && k >= a - p; k--) {
        if (dis[k][b] <= i)
          break;
        dis[k][b] = i + 1;
        q1.push(make_pair(k, b));
      }

      for (k = b + 1; k < m && k <= b + p; k++) {
        if (dis[a][k] <= i)
          break;
        dis[a][k] = i + 1;
        q1.push(make_pair(a, k));
      }
      for (k = b - 1; k >= 0 && k >= b - p; k--) {
        if (dis[a][k] <= i)
          break;
        dis[a][k] = i + 1;
        q1.push(make_pair(a, k));
      }
    }
  }
  if (dis[x][y] == INF)
    dis[x][y] = -1;
  cout << dis[x][y] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long int LL;
typedef pair<int, int> P;
typedef pair<LL, int> LP;
const int INF = 1 << 30;
const LL MAX = 1e9 + 7;

void array_show(int *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%d%c", array[i], (i != array_n - 1 ? middle : '\\n'));
}
void array_show(LL *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%lld%c", array[i], (i != array_n - 1 ? middle : '\\n'));
}
void array_show(vector<int> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%d%c", vec_s[i], (i != vec_n - 1 ? middle : '\\n'));
}
void array_show(vector<LL> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%lld%c", vec_s[i], (i != vec_n - 1 ? middle : '\\n'));
}

vector<vector<int>> dis;
queue<P> q1;

int main() {
  int n, m, p;
  int i, j, k;
  int a, b, c;
  int x, y;
  string sa;
  cin >> n >> m >> p;
  cin >> a >> b >> x >> y;
  a--, b--, x--, y--;
  dis.assign(n, vector<int>(m, INF));
  for (i = 0; i < n; i++) {
    cin >> sa;
    for (j = 0; j < m; j++) {
      if (sa[j] == '@')
        dis[i][j] = -1;
    }
  }
  q1.push(make_pair(a, b));
  dis[a][b] = 0;
  for (i = 0; !q1.empty(); i++) {
    j = q1.size();
    while (j--) {
      a = q1.front().first, b = q1.front().second, q1.pop();
      if (dis[a][b] != i)
        continue;
      for (k = a + 1; k < n && k <= a + p; k++) {
        if (dis[k][b] <= i)
          break;
        dis[k][b] = i + 1;
        q1.push(make_pair(k, b));
      }
      for (k = a - 1; k >= 0 && k >= a - p; k--) {
        if (dis[k][b] <= i)
          break;
        dis[k][b] = i + 1;
        q1.push(make_pair(k, b));
      }

      for (k = b + 1; k < m && k <= b + p; k++) {
        if (dis[a][k] <= i)
          break;
        dis[a][k] = i + 1;
        q1.push(make_pair(a, k));
      }
      for (k = b - 1; k >= 0 && k >= b - p; k--) {
        if (dis[a][k] <= i)
          break;
        dis[a][k] = i + 1;
        q1.push(make_pair(a, k));
      }
    }
  }
  if (dis[x][y] == INF)
    dis[x][y] = -1;
  cout << dis[x][y] << endl;
}