Problem p02644 (C++) — buggy submission, verdict: Runtime Error


// 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)

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define eb emplace_back
#define pb push_back
#define size(s) (int)s.size()
#define int long long
#define vi vector<int>
#define vs vector<string>
#define vv vector<vector<int>>
#define pii pair<int, int>
#define m_p(x, y) make_pair(x, y)
#define vp vector<pair<int, int>>
#define setbits(x) __builtin_popcountll(x)
#define f first
#define se second
#define inc(v, n, x) v.assign(n, x)
#define incd(v, n) v.resize(n)
#define iniz(n) memset(n, 0, sizeof(n))
#define inin(n) memset(n, -1, sizeof(n))
#define inimi(n) memset(n, 0xc0, sizeof(n))
#define inima(n) memset(n, 0x3f, sizeof(n))
#define all(v) (v).begin(), (v).end()

using namespace std;

template <typename T1, typename T2>
istream &operator>>(istream &is, vector<pair<T1, T2>> &v) {
  for (pair<T1, T2> &t : v)
    is >> t.f >> t.se;
  return is;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v)
    is >> t;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &t : v) {
    os << t << " ";
  }
  os << '\\n';
  return os;
}

double pi = acos(-1.0);
const int md = 1e9 + 7;
const int INF = 1e15;
int dx1[] = {0, 0, -1, 1};
int dy1[] = {1, -1, 0, 0};

template <class T> T abst(T a) { return a < 0 ? -a : a; }

template <class T> T max2(T a, T b) { return a > b ? a : b; }
template <class T> T min2(T a, T b) { return a < b ? a : b; }
vector<vi> dis;
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  incd(dis, n);
  for (int i = 0; i < n; i++)
    inc(dis[i], m, INT_MAX);
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  vs v(n);
  cin >> v;
  set<pair<int, pii>> s;
  s.insert({0, {sx, sy}});
  dis[sx][sy] = 0;
  while (!s.empty()) {
    pair<int, pii> p = *s.begin();
    pii t = p.se;
    if (t == m_p(ex, ey)) {
      cout << p.f;
      return;
    }
    s.erase(s.begin());
    for (int i = t.f + 1; i < min2(n, t.f + k + 1); i++) {
      if (v[i][t.se] == '@')
        break;
      if (dis[i][t.se] >= p.f + 1) {
        dis[i][t.se] = p.f + 1;
        s.insert({p.f + 1, {i, t.se}});
      } else
        break;
    }
    for (int j = t.se + 1; j < min2(m, t.se + k + 1); j++) {
      if (v[t.f][j] == '@')
        break;
      if (dis[t.f][j] >= p.f + 1) {
        dis[t.f][j] = p.f + 1;
        s.insert({p.f + 1, {t.f, j}});
      } else
        break;
    }
    for (int i = t.f - 1; i >= max2(0LL, t.f - k); i--) {
      if (v[i][t.se] == '@')
        break;
      if (dis[i][t.se] >= p.f + 1) {
        dis[i][t.se] = p.f + 1;
        s.insert({p.f + 1, {i, t.se}});
      } else
        break;
    }
    for (int j = t.se - 1; j >= max2(0LL, t.se - k); j--) {
      if (v[t.f][j] == '@')
        break;
      if (dis[t.f][j] >= p.f + 1) {
        dis[t.f][j] = p.f + 1;
        s.insert({p.f + 1, {t.f, j}});
      } else
        break;
    }
  }
  cout << -1;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}


Error: -11

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)

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define eb emplace_back
#define pb push_back
#define size(s) (int)s.size()
#define int long long
#define vi vector<int>
#define vs vector<string>
#define vv vector<vector<int>>
#define pii pair<int, int>
#define m_p(x, y) make_pair(x, y)
#define vp vector<pair<int, int>>
#define setbits(x) __builtin_popcountll(x)
#define f first
#define se second
#define inc(v, n, x) v.assign(n, x)
#define incd(v, n) v.resize(n)
#define iniz(n) memset(n, 0, sizeof(n))
#define inin(n) memset(n, -1, sizeof(n))
#define inimi(n) memset(n, 0xc0, sizeof(n))
#define inima(n) memset(n, 0x3f, sizeof(n))
#define all(v) (v).begin(), (v).end()

using namespace std;

template <typename T1, typename T2>
istream &operator>>(istream &is, vector<pair<T1, T2>> &v) {
  for (pair<T1, T2> &t : v)
    is >> t.f >> t.se;
  return is;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v)
    is >> t;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &t : v) {
    os << t << " ";
  }
  os << '\\n';
  return os;
}

double pi = acos(-1.0);
const int md = 1e9 + 7;
const int INF = 1e15;
int dx1[] = {0, 0, -1, 1};
int dy1[] = {1, -1, 0, 0};

template <class T> T abst(T a) { return a < 0 ? -a : a; }

template <class T> T max2(T a, T b) { return a > b ? a : b; }
template <class T> T min2(T a, T b) { return a < b ? a : b; }
vector<vi> dis;
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  incd(dis, n);
  for (int i = 0; i < n; i++)
    inc(dis[i], m, INT_MAX);
  vs v(n);
  cin >> v;
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  set<pair<int, pii>> s;
  s.insert({0, {sx, sy}});
  dis[sx][sy] = 0;
  while (!s.empty()) {
    pair<int, pii> p = *s.begin();
    pii t = p.se;
    if (t == m_p(ex, ey)) {
      cout << p.f;
      return;
    }
    s.erase(s.begin());
    for (int i = t.f + 1; i < min2(n, t.f + k + 1); i++) {
      if (v[i][t.se] == '#')
        break;
      if (dis[i][t.se] >= p.f + 1) {
        dis[i][t.se] = p.f + 1;
        s.insert({p.f + 1, {i, t.se}});
      } else
        break;
    }
    for (int j = t.se + 1; j < min2(m, t.se + k + 1); j++) {
      if (v[t.f][j] == '#')
        break;
      if (dis[t.f][j] >= p.f + 1) {
        dis[t.f][j] = p.f + 1;
        s.insert({p.f + 1, {t.f, j}});
      } else
        break;
    }
    for (int i = t.f - 1; i >= max2(0LL, t.f - k); i--) {
      if (v[i][t.se] == '#')
        break;
      if (dis[i][t.se] >= p.f + 1) {
        dis[i][t.se] = p.f + 1;
        s.insert({p.f + 1, {i, t.se}});
      } else
        break;
    }
    for (int j = t.se - 1; j >= max2(0LL, t.se - k); j--) {
      if (v[t.f][j] == '#')
        break;
      if (dis[t.f][j] >= p.f + 1) {
        dis[t.f][j] = p.f + 1;
        s.insert({p.f + 1, {t.f, j}});
      } else
        break;
    }
  }
  cout << -1;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}