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

#include <bits/stdc++.h>
using namespace std;
#define END '\\n'
#define int long long
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define trace(x) cerr << x << END;
#define loop(i, a, b) for (int i = (a); i < (b); i++)
#define loopb(i, b, a) for (int i = (b); i > (a); --i)
const int mod = 1e9 + 7;
const int mod1 = 998244353;
const int inf = 9223372036ll;
struct mywish {
  bool operator()(const pair<int, pii> &a, const pair<int, pii> &b) {
    if (a.ff == b.ff) {
      if (a.ss.ff == b.ss.ff)
        return a.ss.ss < b.ss.ss;
      else
        return a.ss.ff < a.ss.ff;
    }
    return a.ff < b.ff;
  }
};
int n, m, k;
vector<string> grid;
vector<vector<int>> steps;
int sx, sy, fx, fy;
int dy[] = {0, -1, 0, 1};
int dx[] = {-1, 0, 1, 0};
inline bool valid(int y, int x) {
  return (y >= 0 && y < n && x >= 0 && x < m && grid[y][x] == '.');
}
int bfs() {
  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      q;
  q.push({0ll, {sy, sx}});
  steps[sy][sx] = 0;
  while (!q.empty()) {
    int cy = q.top().ss.ff, cx = q.top().ss.ss;
    int c_steps = q.top().ff;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int proceed = 1;
      while (proceed <= k) {
        int ny = cy + proceed * dy[i], nx = cx + proceed * dx[i];
        if (!valid(ny, nx) || steps[ny][nx] < c_steps + 1)
          break;
        if (steps[ny][nx] == c_steps + 1) {
          proceed++;
          continue;
        }
        steps[ny][nx] = min(steps[ny][nx], c_steps + 1);
        q.push({steps[ny][nx], {ny, nx}});
        proceed++;
      }
    }
  }
  return steps[fy][fx];
}

void solve() {
  cin >> n >> m >> k;
  grid = vector<string>(n);
  steps = vector<vector<int>>(n, vector<int>(m, inf));
  cin >> sy >> sx >> fy >> fx;
  sy--, sx--, fy--, fx--;
  loop(i, 0, n) cin >> grid[i];
  int ans = bfs();
  if (ans == inf)
    ans = -1;
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define END '\\n'
#define int long long
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define trace(x) cerr << x << END;
#define loop(i, a, b) for (int i = (a); i < (b); i++)
#define loopb(i, b, a) for (int i = (b); i > (a); --i)
const int mod = 1e9 + 7;
const int mod1 = 998244353;
const int inf = 9223372036ll;
struct mywish {
  bool operator()(const pair<int, pii> &a, const pair<int, pii> &b) {
    if (a.ff == b.ff) {
      if (a.ss.ff == b.ss.ff)
        return a.ss.ss < b.ss.ss;
      else
        return a.ss.ff < a.ss.ff;
    }
    return a.ff < b.ff;
  }
};
int n, m, k;
vector<string> grid;
vector<vector<int>> steps;
int sx, sy, fx, fy;
int dy[] = {0, -1, 0, 1};
int dx[] = {-1, 0, 1, 0};
inline bool valid(int y, int x) {
  return (y >= 0 && y < n && x >= 0 && x < m && grid[y][x] == '.');
}
int bfs() {
  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      q;
  q.push({0ll, {sy, sx}});
  // grid[sy][sx] = '@';
  steps[sy][sx] = 0;
  while (!q.empty()) {
    int cy = q.top().ss.ff, cx = q.top().ss.ss;
    int c_steps = steps[cy][cx];
    // cout<<cy<<" "<<cx<<" "<<c_steps<<END;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int proceed = 1;
      while (proceed <= k) {
        int ny = cy + proceed * dy[i], nx = cx + proceed * dx[i];
        if (!valid(ny, nx) || steps[ny][nx] <= c_steps)
          break;
        // grid[ny][nx] = '@';
        steps[ny][nx] = min(steps[ny][nx], c_steps + 1);
        q.push({steps[ny][nx], {ny, nx}});
        proceed++;
      }
    }
  }
  return steps[fy][fx];
}

void solve() {
  cin >> n >> m >> k;
  grid = vector<string>(n);
  steps = vector<vector<int>>(n, vector<int>(m, inf));
  cin >> sy >> sx >> fy >> fx;
  sy--, sx--, fy--, fx--;
  loop(i, 0, n) cin >> grid[i];
  int ans = bfs();
  if (ans == inf)
    ans = -1;
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  // cerr<<END<<1.0*clock();
  return 0;
}