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

// agrawal117
// chahatagrawal117
#include <bits/stdc++.h>
#define endl '\\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;
#define MAX 100005
const int oo = 1000000007;
const int dx[] = {1, 0, 0, -1};
const int dy[] = {0, 1, -1, 0};
deque<pair<int, int>> dq;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, k;
  cin >> n >> m >> k;

  int dist[n + 3][m + 3];
  int vis[n + 3][m + 3];
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      dist[i][j] = oo, vis[i][j] = 0;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;

  char arr[n + 4][m + 4];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      cin >> arr[i][j];
  }
  dist[x1][y1] = 0;
  vis[x1][y1] = 1;

  dq.push_back({x1, y1});
  while (!dq.empty()) {
    int x = dq[0].first;
    int y = dq[0].second;
    dq.pop_front();
    // cout<<x<<" "<<y<<" "<<dist[x][y]<<endl;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int xx = x + dx[i] * j;
        int yy = y + (dy[i] * j);
        if (xx < 1 || xx > n || yy < 1 || yy > m)
          break;
        if (arr[xx][yy] == '@')
          break;
        // cout<<xx<<" C "<<yy<<endl;
        if (vis[xx][yy] == 0) {
          dist[xx][yy] = dist[x][y] + 1;
          vis[xx][yy] = 1;
          dq.push_back({xx, yy});
        } else {
          if (dist[xx][yy] <= dist[x][y]) {
            break;
          }
        }
      }
    }
  }

  ll ans = dist[x2][y2];
  if (ans == oo)
    ans = -1;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

// agrawal117
// chahatagrawal117
#include <bits/stdc++.h>
#define endl '\\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;
#define MAX 100005
const int oo = 1000000007;
const int dx[] = {1, 0, 0, -1};
const int dy[] = {0, 1, -1, 0};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  ll dist[n + 3][m + 3];
  int vis[n + 3][m + 3];
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      dist[i][j] = oo, vis[i][j] = 1;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  char arr[n + 4][m + 4];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      cin >> arr[i][j];
  }
  dist[x1][y1] = 0;
  vis[x1][y1] = 1;
  deque<pair<int, int>> dq;
  dq.push_back({x1, y1});
  while (!dq.empty()) {
    int x = dq[0].first;
    int y = dq[0].second;
    dq.pop_front();
    // cout<<x<<" "<<y<<" "<<dist[x][y]<<endl;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int xx = x + dx[i] * j;
        int yy = y + (dy[i] * j);
        if (xx < 1 || xx > n || yy < 1 || yy > m)
          break;
        if (arr[xx][yy] == '@')
          break;
        if (vis[xx][yy] == 0) {
          dist[xx][yy] = dist[x][y] + 1;
          vis[xx][yy] = 1;
          dq.push_back({xx, yy});
        } else {
          if (dist[xx][yy] >= dist[x][y] + 1) {
            dist[xx][yy] = dist[x][y] + 1;
            dq.push_back({xx, yy});
          } else
            break;
        }
      }
    }
  }

  ll ans = dist[x2][y2];
  if (ans == oo)
    ans = -1;
  cout << ans << endl;
}