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


// Problem : F - Pond Skater
// Contest : AtCoder - AtCoder Beginner Contest 170
// URL : https://atcoder.jp/contests/abc170/tasks/abc170_f
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define db double
#define mod 1000000007
#define pb insert_back
#define pp pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define maxn 200005
#define ss second
#define ff first
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
int inv(int a, int b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }
int X[] = {1, -1, 0, 0};
int Y[] = {0, 0, 1, -1};
int n, m;
bool check(int x, int y) {
  if (x >= 0 and y >= 0 and x < n and y < m)
    return true;
  return false;
}
void solve() {
  int k;
  cin >> n >> m >> k;
  string s[n];
  int dist[n][m];
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  rep(i, 0, n) cin >> s[i];
  set<pair<int, pair<int, int>>> q;
  // cout << INT_MAX <<" ";
  rep(i, 0, n) { rep(j, 0, m) dist[i][j] = INT_MAX; }
  dist[x1][y1] = 0;
  q.insert({0, {x1, y1}});
  while (q.size()) {
    int x = q.begin()->ss.ff;
    int y = q.begin()->ss.ss;
    q.erase(q.begin());
    rep(i, 0, 4) {
      int l = x, r = y;
      rep(j, 0, k) {
        l += X[i];
        r += Y[i];
        if (check(l, r) == false || dist[l][r] <= dist[x][y] || s[l][r] == '@')
          break;
        dist[l][r] = dist[x][y] + 1;
        q.insert({dist[l][r], {l, r}});
        // cout << l <<" "<< r <<"\\n";
      }
    }
  }

  if (dist[x2][y2] == INT_MAX)
    cout << -1;
  else
    cout << dist[x2][y2];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}


Error: TLE

Fixed submission:


// Problem : F - Pond Skater
// Contest : AtCoder - AtCoder Beginner Contest 170
// URL : https://atcoder.jp/contests/abc170/tasks/abc170_f
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define db double
#define mod 1000000007
#define pb push_back
#define pp pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define maxn 200005
#define ss second
#define ff first
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
int inv(int a, int b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }
int X[] = {1, -1, 0, 0};
int Y[] = {0, 0, 1, -1};
int n, m;
bool check(int x, int y) {
  if (x >= 0 and y >= 0 and x < n and y < m)
    return true;
  return false;
}
void solve() {
  int k;
  cin >> n >> m >> k;
  string s[n];
  int dist[n][m];
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  rep(i, 0, n) cin >> s[i];
  queue<pair<int, int>> q;
  // cout << INT_MAX <<" ";
  rep(i, 0, n) { rep(j, 0, m) dist[i][j] = INT_MAX; }
  dist[x1][y1] = 0;
  q.push({x1, y1});
  while (q.size()) {
    int x = q.front().ff;
    int y = q.front().ss;
    q.pop();
    rep(i, 0, 4) {
      int l = x, r = y;
      rep(j, 0, k) {
        l += X[i];
        r += Y[i];
        if (check(l, r) == false || dist[l][r] <= dist[x][y] || s[l][r] == '@')
          break;
        dist[l][r] = dist[x][y] + 1;
        q.push({l, r});
        // cout << l <<" "<< r <<"\\n";
      }
    }
  }

  if (dist[x2][y2] == INT_MAX)
    cout << -1;
  else
    cout << dist[x2][y2];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}