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



#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
void printvec(vec &v) {
  Rep(i, (ll)v.size()) { cout << v[i] << " \\n"[i + 1 == (ll)v.size()]; }
}

vec dx = {0, -1, 0, 1};
vec dy = {1, 0, -1, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll H, W, K;
  cin >> H >> W >> K;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<string> c(H);
  Rep(i, H) { cin >> c[i]; }

  x1--;
  x2--;
  y1--;
  y2--;
  mat dist(H, vec(W, inf));
  dist[x1][y1] = 0;

  queue<pair<ll, ll>> q;
  q.push({x1, y1});
  while (q.size()) {
    auto v = q.front();
    q.pop();
    ll x = v.first;
    ll y = v.second;
    Rep(i, 4) {
      for (ll j = 1; j <= K; j++) {
        ll ny = y + dy[i] * j;
        ll nx = x + dx[i] * j;
        if (0 <= nx and nx < H and 0 <= ny and ny < W) {
          if (c[nx][ny] == '@')
            break;
          if (dist[nx][ny] <= dist[x][y])
            break;
          if (dist[nx][ny] > dist[x][y] + 1) {
            dist[nx][ny] = dist[x][y] + 1;
            q.push({nx, ny});
          }
        }
      }
    }
  }

  // Rep (i, H) {
  //     Rep (j, W) {
  //         if (dist[i][j] == inf) cout << "_" << " \\n"[j==W-1];
  //         else cout << dist[i][j] << " \\n"[j==W-1];
  //     }
  // }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
void printvec(vec &v) {
  Rep(i, (ll)v.size()) { cout << v[i] << " \\n"[i + 1 == (ll)v.size()]; }
}

vec dx = {0, -1, 0, 1};
vec dy = {1, 0, -1, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll H, W, K;
  cin >> H >> W >> K;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<string> c(H);
  Rep(i, H) { cin >> c[i]; }

  x1--;
  x2--;
  y1--;
  y2--;
  mat dist(H, vec(W, inf));
  dist[x1][y1] = 0;

  queue<pair<ll, ll>> q;
  q.push({x1, y1});
  while (q.size()) {
    auto v = q.front();
    q.pop();
    ll x = v.first;
    ll y = v.second;
    Rep(i, 4) {
      for (ll j = 1; j <= K; j++) {
        ll ny = y + dy[i] * j;
        ll nx = x + dx[i] * j;
        if (0 <= nx and nx < H and 0 <= ny and ny < W) {
          if (c[nx][ny] == '@')
            break;
          // if (dist[nx][ny] <= dist[x][y]) break;
          if (dist[nx][ny] > dist[x][y] + 1) {
            dist[nx][ny] = dist[x][y] + 1;
            q.push({nx, ny});
          }
        }
      }
    }
  }

  // Rep (i, H) {
  //     Rep (j, W) {
  //         if (dist[i][j] == inf) cout << "_" << " \\n"[j==W-1];
  //         else cout << dist[i][j] << " \\n"[j==W-1];
  //     }
  // }

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