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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef pair<ld, ld> id;
typedef tuple<ll, ll, ll> tl;
typedef tuple<ll, ll, ll, ll> ql;

#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define ROF(i, a, b) for (ll i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
#define FILL(x, n, v) fill(x, x + n, v);
#define ALL(x) x.begin(), x.end()
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

#define f first
#define s second
#define ins insert
#define e pb
#define eb emplace_back
#define ef emplace_front
#define p push
#define pf push_front
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define ft front
#define bk back
#define pp pop
#define ppb pop_back
#define ppf pop_front

#define db cout << "YEET\\n";
#define ct(x) cout << x << '\\n';

const ll MOD = 1e9 + 7; // 998244353
const ll MAXN = 500 + 5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);

int main() {
  FAST ll grid[MAXN][MAXN];
  bool left[MAXN][MAXN];
  ll n;
  cin >> n;
  FOR(i, 1, n) {
    FOR(j, 1, n) {
      ll vert = min(i - 1, n - i);
      ll horz = min(j - 1, n - j);
      ll minimum = min(vert, horz);
      grid[i][j] = minimum;
      left[i][j] = 0;
    }
  }

  deque<ii> bfs;
  ll ans = 0;

  ll c1[4] = {-1, 0, 0, 1};
  ll c2[4] = {0, -1, 1, 0};

  FOR(i, 1, n * n) {
    ll x;
    cin >> x;
    x--;

    ll row = x / n + 1;
    ll col = x % n + 1;

    ans += grid[row][col];
    grid[row][col]--;
    left[row][col] = 1;

    bfs.eb(row, col);
    while (!bfs.empty()) {
      ll r = bfs.front().f;
      ll c = bfs.front().s;
      bfs.ppf();

      FOR(j, 0, 3) {
        ll newr = r + c1[j];
        ll newc = c + c2[j];

        if (newr == 0 || newc == 0 || newr > n || newc > n)
          continue;
        if (grid[r][c] + 1 < grid[newr][newc] && !left[newr][newc]) {
          grid[newr][newc] = grid[r][c] + 1;
          bfs.eb(newr, newc);
        } else if (left[newr][newc] && grid[r][c] < grid[newr][newc]) {
          grid[newr][newc] = grid[r][c];
          bfs.ef(newr, newc);
        }
      }
    }
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef pair<ld, ld> id;
typedef tuple<ll, ll, ll> tl;
typedef tuple<ll, ll, ll, ll> ql;

#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define ROF(i, a, b) for (ll i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
#define FILL(x, n, v) fill(x, x + n, v);
#define ALL(x) x.begin(), x.end()
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

#define f first
#define s second
#define ins insert
#define e pb
#define eb emplace_back
#define ef emplace_front
#define p push
#define pf push_front
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define ft front
#define bk back
#define pp pop
#define ppb pop_back
#define ppf pop_front

#define db cout << "YEET\\n";
#define ct(x) cout << x << '\\n';

const ll MOD = 1e9 + 7; // 998244353
const ll MAXN = 10 + 5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);

int main() {
  FAST ll grid[MAXN][MAXN];
  bool left[MAXN][MAXN];
  ll n;
  cin >> n;
  FOR(i, 1, n) {
    FOR(j, 1, n) {
      ll vert = min(i - 1, n - i);
      ll horz = min(j - 1, n - j);
      ll minimum = min(vert, horz);
      grid[i][j] = minimum;
      left[i][j] = 0;
    }
  }

  deque<ii> bfs;
  ll ans = 0;

  ll c1[4] = {-1, 0, 0, 1};
  ll c2[4] = {0, -1, 1, 0};

  FOR(i, 1, n * n) {
    ll x;
    cin >> x;
    x--;

    ll row = x / n + 1;
    ll col = x % n + 1;

    ans += grid[row][col];
    grid[row][col]--;
    left[row][col] = 1;

    bfs.eb(row, col);
    while (!bfs.empty()) {
      ll r = bfs.front().f;
      ll c = bfs.front().s;
      bfs.ppf();

      FOR(j, 0, 3) {
        ll newr = r + c1[j];
        ll newc = c + c2[j];

        if (newr == 0 || newc == 0 || newr > n || newc > n)
          continue;
        if (grid[r][c] + 1 < grid[newr][newc] && !left[newr][newc]) {
          grid[newr][newc] = grid[r][c] + 1;
          bfs.eb(newr, newc);
        } else if (left[newr][newc] && grid[r][c] < grid[newr][newc]) {
          grid[newr][newc] = grid[r][c];
          bfs.ef(newr, newc);
        }
      }
    }
  }
  cout << ans;
}