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

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

typedef long long ll;
typedef long double ld;
// use unique(x) - removec consecutive items, returns vec.begin() + number of
// items for vector: sort(all(vec)); vec.erase(unique(all(vec)), vec.end()); use
// iota(all(vec), 0) for filling a vector with 0,1,2... use fill(all(vec), 1)
// for filling a vector with 1,1,1... use rotate(vec.begin(), vec.begin() + 1,
// vec.end()) to rotate a vector | middle arg becomes first use merge(all(vec1),
// all(vec2), vec3.begin()) to merge 2 vectors, use vec3.resize(vec1.size() +
// vec2.size()) first ll pos = partition_point(all(vec), pred) - vec.begin();
// bool pred(ll a){return a<3}; print number in binary -> cout << bitset<20>(n);
#define inf 9e18 + 1

int n;
int ar[502][502];
bool dead[502][502];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

void dfs(int y, int x) {
  int z = ar[y][x] + !dead[y][x];
  for (int i = 0; i < 4; ++i) {
    int y1 = y + dy[i];
    int x1 = x + dx[i];
    if (ar[y1][x1] > z) {
      ar[y1][x1] = z;
      dfs(y1, x1);
    }
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n + 2; ++i)
    ar[n + 1][i] = ar[i][n + 1] = ar[0][i] = ar[i][0] = -10;
  for (int i = 1; i < n + 1; ++i)
    for (int j = 1; j < n + 1; ++j)
      ar[i][j] = min({i - 1, j - 1, n - i, n - j});
  ll res = 0;
  for (int i = 0; i < n * n; ++i) {
    //	for(int i=0;i<n+2;++i,cout<<endl)
    //		for(int j=0;j<n+2;++j)
    //			cout<<ar[i][j]<<" ";
    // cout<<res<<endl;
    int viewer;
    cin >> viewer;
    --viewer;
    int y = viewer / n + 1;
    int x = viewer % n + 1;
    res += ar[y][x];
    dead[y][x] = true;
    dfs(y, x);
  }
  cout << res << endl;
}


Error: 0

Fixed submission:

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

typedef long long ll;
typedef long double ld;
// use unique(x) - removec consecutive items, returns vec.begin() + number of
// items for vector: sort(all(vec)); vec.erase(unique(all(vec)), vec.end()); use
// iota(all(vec), 0) for filling a vector with 0,1,2... use fill(all(vec), 1)
// for filling a vector with 1,1,1... use rotate(vec.begin(), vec.begin() + 1,
// vec.end()) to rotate a vector | middle arg becomes first use merge(all(vec1),
// all(vec2), vec3.begin()) to merge 2 vectors, use vec3.resize(vec1.size() +
// vec2.size()) first ll pos = partition_point(all(vec), pred) - vec.begin();
// bool pred(ll a){return a<3}; print number in binary -> cout << bitset<20>(n);
#define inf 9e18 + 1

int n;
int ar[500][500];
bool dead[500][500];
vector<pair<int, int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

void dfs(int y, int x) {
  --ar[y][x];
  for (auto d : dirs)
    if (y + d.first < n && y + d.first >= 0 && x + d.second < n &&
        x + d.second >= 0 &&
        (ar[y + d.first][x + d.second] > ar[y][x] + 1 ||
         (dead[y][x] && dead[y + d.first][x + d.second] &&
          ar[y + d.first][x + d.second] != ar[y][x])))
      dfs(y + d.first, x + d.second);
}

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      ar[i][j] = min({i, j, n - i - 1, n - j - 1});
  ll res = 0;
  for (int i = 0; i < n * n; ++i) {
    //		for(int i=0;i<n;++i,cout<<endl)
    //			for(int j=0;j<n;++j)
    //				cout<<ar[i][j]<<" ";
    //		cout<<res<<endl;

    int viewer;
    cin >> viewer;
    --viewer;
    res += ar[viewer / n][viewer % n];
    dead[viewer / n][viewer % n] = true;
    dfs(viewer / n, viewer % n);
  }
  cout << res << endl;
}