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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<long long> vll;
typedef vector<int> vi;

#define io ios_base::sync_with_stdio(false)
#define pb push_back
#define eb emplace_back
#define mod 1000000007
#define PI 2 * acos(0.0)

const int N = 200005;
int dirx[] = {1, -1, 0, 0}, diry[] = {0, 0, 1, -1};

int32_t main() {
  io;
  int n, m, k, sx, sy, tx, ty;
  cin >> n >> m >> k >> sx >> sy >> tx >> ty;
  sx--;
  sy--;
  tx--;
  ty--;
  string inp[n];
  for (int i = 0; i < n; i++)
    cin >> inp[i];

  int dist[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      dist[i][j] = INT_MAX;

  dist[sx][sy] = 0;
  bool vis[n][m];
  memset(vis, 0, sizeof(vis));
  queue<pair<int, pii>> q;
  q.push({0, {sx, sy}});
  while (!q.empty()) {
    int cost = q.front().first, ax = q.front().second.first,
        ay = q.front().second.second;
    q.pop();
    /*if(vis[ax][ay])
        continue;
    vis[ax][ay] = 1;*/
    for (int i = 0; i < 4; i++) {
      for (int d = 1; d <= k; d++) {
        int nx = ax + dirx[i] * d, ny = ay + diry[i] * d;
        if (nx >= n || ny >= m || nx < 0 || ny < 0 || inp[nx][ny] == '@')
          break;
        if (dist[nx][ny] > cost + 1) {
          dist[nx][ny] = cost + 1;
          q.push({cost + 1, {nx, ny}});
        } else if (dist[nx][ny] == cost + 1)
          continue;
        else
          break;
      }
    }
  }
  if (dist[tx][ty] == INT_MAX)
    cout << -1;
  else
    cout << dist[tx][ty];
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<long long> vll;
typedef vector<int> vi;

#define io ios_base::sync_with_stdio(false)
#define pb push_back
#define eb emplace_back
#define mod 1000000007
#define PI 2 * acos(0.0)

const int N = 200005;
int dirx[] = {1, -1, 0, 0}, diry[] = {0, 0, 1, -1};

int32_t main() {
  io;
  int n, m, k, sx, sy, tx, ty;
  cin >> n >> m >> k >> sx >> sy >> tx >> ty;
  sx--;
  sy--;
  tx--;
  ty--;
  string inp[n];
  for (int i = 0; i < n; i++)
    cin >> inp[i];

  int dist[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      dist[i][j] = INT_MAX;

  dist[sx][sy] = 0;
  bool vis[n][m];
  memset(vis, 0, sizeof(vis));
  priority_queue<pair<int, pii>> q;
  q.push({0, {sx, sy}});
  while (!q.empty()) {
    int cost = q.top().first, ax = q.top().second.first,
        ay = q.top().second.second;
    q.pop();
    /*if(vis[ax][ay])
        continue;
    vis[ax][ay] = 1;*/
    for (int i = 0; i < 4; i++) {
      for (int d = 1; d <= k; d++) {
        int nx = ax + dirx[i] * d, ny = ay + diry[i] * d;
        if (nx >= n || ny >= m || nx < 0 || ny < 0 || inp[nx][ny] == '@')
          break;
        if (dist[nx][ny] > cost + 1) {
          dist[nx][ny] = cost + 1;
          q.push({cost + 1, {nx, ny}});
        } else if (dist[nx][ny] == cost + 1)
          continue;
        else
          break;
      }
    }
  }
  if (dist[tx][ty] == INT_MAX)
    cout << -1;
  else
    cout << dist[tx][ty];
  return 0;
}