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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define x first
#define y second

int dist[500][500];
bool used[500][500];

vector<pair<int, int>> dir = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};

int n;

int us[500][500];
int pt = 0;

bool check(int x, int y) { return x >= 0 && x < n && y < n && y >= 0; }

void dfs(int x, int y, int d) {
  dist[x][y]--;
  us[x][y] = pt;
  for (auto el : dir) {
    int x2 = el.x + x;
    int y2 = el.y + y;
    if (check(x2, y2) && us[x2][y2] != pt)
      if (used[x][y] && dist[x2][y2] == dist[x][y] + 1) {
        dfs(x2, y2, d);
      } else if (dist[x2][y2] == d) {
        dfs(x2, y2, d + 1);
      }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  vector<pair<int, int>> order;
  for (int i = 0; i < n * n; i++) {
    int num;
    cin >> num;
    num--;
    int x = num / n;
    int y = num % n;
    order.push_back({x, y});
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dist[i][j] = min(min(i, n - i - 1), min(j, n - j - 1));
    }
  }
  long long ans = 0;
  for (auto el : order) {
    ans += dist[el.x][el.y];
    pt++;
    used[el.x][el.y] = 1;
    for (auto el2 : dir) {
      int x = el.x + el2.x;
      int y = el.y + el2.y;
      if (check(x, y) && dist[x][y] == dist[el.x][el.y] + 1) {
        // if (el.x == el.y && el.x == 0)
        //     cout << x << ' ' << y << endl;
        dfs(x, y, dist[x][y] + 1);
      }
    }
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define x first
#define y second

int dist[500][500];
bool used[500][500];

vector<pair<int, int>> dir = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};

int n;

bool check(int x, int y) { return x >= 0 && x < n && y < n && y >= 0; }

void dfs(int x, int y, int d) {
  dist[x][y]--;
  for (auto el : dir) {
    int x2 = el.x + x;
    int y2 = el.y + y;
    if (check(x2, y2))
      if (used[x][y] && dist[x2][y2] != dist[x][y]) {
        dfs(x2, y2, d);
      } else if (dist[x2][y2] == d) {
        dfs(x2, y2, d + 1);
      }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  vector<pair<int, int>> order;
  for (int i = 0; i < n * n; i++) {
    int num;
    cin >> num;
    num--;
    int x = num / n;
    int y = num % n;
    order.push_back({x, y});
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dist[i][j] = min(min(i, n - i - 1), min(j, n - j - 1));
    }
  }
  long long ans = 0;
  for (auto el : order) {
    ans += dist[el.x][el.y];
    used[el.x][el.y] = 1;
    for (auto el2 : dir) {
      int x = el.x + el2.x;
      int y = el.y + el2.y;
      if (check(x, y) && dist[x][y] == dist[el.x][el.y] + 1) {
        // if (el.x == el.y && el.x == 0)
        //     cout << x << ' ' << y << endl;
        dfs(x, y, dist[x][y] + 1);
      }
    }
  }
  cout << ans;
  return 0;
}