Problem p02670 (C++)

Buggy submission (status: Runtime Error):
/*
No matter how long a night, dawn will always break.

Simular saying ... The darkest hour is just before the dawn.
 ***************************************   ************************8
**********************************
*/
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define double long double
#define pb push_back
#define pf push_front
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define x first
#define y second
#define endl '\n'
#define sz(x) (int)(x).size()
#define fo(i, l, u) for (i = l; i < u; i++)
#define rfo(i, l, u) for (i = l; i >= u; i--)
#define allfo(s) for (auto it = (s).begin(); it != (s).end(); it++)
#define _init(b) memset(b, -1, sizeof(b))
#define _init0(b) memset(b, 0, sizeof(b))
#define MOD 1000000007
#define hell 998244353
#define output(x) cout << (x ? "YES" : "NO") << endl;

int gcd(int a, int b) {
  if (a > b)
    swap(a, b);
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
bool mod(double a, double b) { return a / b - floor(a / b); }

const int N = 55;
int grid[N][N], vis[N][N], dis[N][N], n;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

void dfs(int d, int x, int y) {
  int i, a, b, c, xx;
  // cout<<"("<<x<<","<<y<<") ";
  fo(i, 0, 4) {
    a = x + dx[i];
    b = y + dy[i];

    if ((a < 1) || (a > n) || (b < 1) || (b > n))
      continue;

    c = dis[a][b];
    xx = d + grid[x][y];
    // cout<<a<<","<<b<<","<<c<<"< ";
    if (c > xx) {
      // cout<<"["<<dis[a][b]<<","<<xx<<"] ";
      dis[a][b] = xx;

      dfs(xx, a, b);
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int i, j, x;
  cin >> n;
  fo(i, 1, n + 1) {
    fo(j, 1, n + 1) {
      grid[i][j] = 1;
      vis[i][j] = -1;
      dis[i][j] = min({i - 1, n - i, j - 1, n - j});
      // cout<<dis[i][j]<<" ";
    }
    //	cout<<endl;
  }

  vi v;
  fo(i, 0, n * n) {
    cin >> x;
    v.pb(x);
  }
  int ans = 0;
  fo(i, 0, n * n) {
    int a, b, c, d;
    x = v[i];
    a = ((x - 1) / n) + 1;
    b = ((x - 1) % n) + 1;
    // cout<<x<<" ";
    // cout<<"("<<a<<","<<b<<")"<<" ";
    ans += dis[a][b];
    grid[a][b] = 0;
    x = dis[a][b];
    fo(j, 0, 4) {
      c = a + dx[j];
      d = b + dy[j];
      if ((c < 1) || (c > n) || (d < 1) || (d > n))
        continue;
      x = min(dis[c][d] + grid[c][d], x);
    }
    dis[a][b] = x;
    // cout<<dis[a][b]<<" ";
    dfs(dis[a][b], a, b);
    // cout<<endl;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
/*
No matter how long a night, dawn will always break.

Simular saying ... The darkest hour is just before the dawn.
 ***************************************   ************************8
**********************************
*/
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define double long double
#define pb push_back
#define pf push_front
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define x first
#define y second
#define endl '\n'
#define sz(x) (int)(x).size()
#define fo(i, l, u) for (i = l; i < u; i++)
#define rfo(i, l, u) for (i = l; i >= u; i--)
#define allfo(s) for (auto it = (s).begin(); it != (s).end(); it++)
#define _init(b) memset(b, -1, sizeof(b))
#define _init0(b) memset(b, 0, sizeof(b))
#define MOD 1000000007
#define hell 998244353
#define output(x) cout << (x ? "YES" : "NO") << endl;

int gcd(int a, int b) {
  if (a > b)
    swap(a, b);
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
bool mod(double a, double b) { return a / b - floor(a / b); }

const int N = 505;
int grid[N][N], vis[N][N], dis[N][N], n;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

void dfs(int d, int x, int y) {
  int i, a, b, c, xx;
  // cout<<"("<<x<<","<<y<<") ";
  fo(i, 0, 4) {
    a = x + dx[i];
    b = y + dy[i];

    if ((a < 1) || (a > n) || (b < 1) || (b > n))
      continue;

    c = dis[a][b];
    xx = d + grid[x][y];
    // cout<<a<<","<<b<<","<<c<<"< ";
    if (c > xx) {
      // cout<<"["<<dis[a][b]<<","<<xx<<"] ";
      dis[a][b] = xx;

      dfs(xx, a, b);
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int i, j, x;
  cin >> n;
  fo(i, 1, n + 1) {
    fo(j, 1, n + 1) {
      grid[i][j] = 1;
      vis[i][j] = -1;
      dis[i][j] = min({i - 1, n - i, j - 1, n - j});
      // cout<<dis[i][j]<<" ";
    }
    //	cout<<endl;
  }

  vi v;
  fo(i, 0, n * n) {
    cin >> x;
    v.pb(x);
  }
  int ans = 0;
  fo(i, 0, n * n) {
    int a, b, c, d;
    x = v[i];
    a = ((x - 1) / n) + 1;
    b = ((x - 1) % n) + 1;
    // cout<<x<<" ";
    // cout<<"("<<a<<","<<b<<")"<<" ";
    ans += dis[a][b];
    grid[a][b] = 0;
    x = dis[a][b];
    fo(j, 0, 4) {
      c = a + dx[j];
      d = b + dy[j];
      if ((c < 1) || (c > n) || (d < 1) || (d > n))
        continue;
      x = min(dis[c][d] + grid[c][d], x);
    }
    dis[a][b] = x;
    // cout<<dis[a][b]<<" ";
    dfs(dis[a][b], a, b);
    // cout<<endl;
  }
  cout << ans << endl;

  return 0;
}