Problem p02670 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define MX 100005
#define mod 1000000007
#define ll long long
#define ld long double
#define mem(a, b) memset(a, b, sizeof(a))
#define fast()                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define F first
#define S second
// #define     mp        make_pair
#define inf 2e18
#define PI acos(-1)

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

int n;
int p[2505];
int dp[505][505];
int ok[505][505];
// int mark[505][505][4];
int ans = 0;
int cur;

int getMin(int ro, int col) {
  int res = n;
  res = min(res, dp[ro - 1][col] + ok[ro - 1][col]);
  res = min(res, dp[ro + 1][col] + ok[ro + 1][col]);
  res = min(res, dp[ro][col - 1] + ok[ro][col - 1]);
  res = min(res, dp[ro][col + 1] + ok[ro][col + 1]);
  return res;
}
void process(int ro, int col);

void tryToUpdate(int ro, int col, int val) {
  if (dp[ro][col] <= val)
    return;
  // cout<<"updated "<<ro<<" "<<col<<" "<<" val "<<val<<endl;

  dp[ro][col] = val;
  process(ro, col);
}

void process(int ro, int col) {
  // cout<<"process "<<ro<<" "<<col<<endl;
  int need = getMin(ro, col) + ok[ro][col];

  if (ro > 1)
    tryToUpdate(ro - 1, col, need);
  if (ro < n)
    tryToUpdate(ro + 1, col, need);
  if (col > 1)
    tryToUpdate(ro, col - 1, need);
  if (col < n)
    tryToUpdate(ro, col + 1, need);
}

int main() {
  fast();
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> n;
  for (int i = 1; i <= n * n; i++)
    cin >> p[i];
  for (int i = 1; i <= n * n; i++) {
    int ro = p[i] / n;
    int col = p[i] % n;
    if (col)
      ro++;
    else
      col = n;

    dp[ro][col] = min(min(ro - 1, n - ro), min(col - 1, n - col));
    ok[ro][col] = 1;
  }

  for (int i = 1; i <= n * n; i++) {
    int ro = p[i] / n;
    int col = p[i] % n;
    if (col)
      ro++;
    else
      col = n;
    ans += dp[ro][col];
    ok[ro][col] = 0;
    process(ro, col);
    /*
    cout<<p[i]<<" "<<ro<< " "<<col<<endl;
    for(int j=1;j<=n;j++){
        for(int k=1;k<=n;k++) cout<<dp[j][k]<<" ";
        cout<<endl;
    }
    cout<<endl;
    */
  }
  cout << ans << endl;
  return 0;
}


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

#define MX 100005
#define mod 1000000007
#define ll long long
#define ld long double
#define mem(a, b) memset(a, b, sizeof(a))
#define fast()                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define F first
#define S second
// #define     mp        make_pair
#define inf 2e18
#define PI acos(-1)

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

int n;
int p[250005];
int dp[505][505];
int ok[505][505];
// int mark[505][505][4];
int ans = 0;
int cur;

int getMin(int ro, int col) {
  int res = n;
  res = min(res, dp[ro - 1][col] + ok[ro - 1][col]);
  res = min(res, dp[ro + 1][col] + ok[ro + 1][col]);
  res = min(res, dp[ro][col - 1] + ok[ro][col - 1]);
  res = min(res, dp[ro][col + 1] + ok[ro][col + 1]);
  return res;
}
void process(int ro, int col);

void tryToUpdate(int ro, int col, int val) {
  if (dp[ro][col] <= val)
    return;
  // cout<<"updated "<<ro<<" "<<col<<" "<<" val "<<val<<endl;

  dp[ro][col] = val;
  process(ro, col);
}

void process(int ro, int col) {
  // cout<<"process "<<ro<<" "<<col<<endl;
  int need = getMin(ro, col) + ok[ro][col];

  if (ro > 1)
    tryToUpdate(ro - 1, col, need);
  if (ro < n)
    tryToUpdate(ro + 1, col, need);
  if (col > 1)
    tryToUpdate(ro, col - 1, need);
  if (col < n)
    tryToUpdate(ro, col + 1, need);
}

int main() {
  fast();
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> n;
  for (int i = 1; i <= n * n; i++)
    cin >> p[i];
  for (int i = 1; i <= n * n; i++) {
    int ro = p[i] / n;
    int col = p[i] % n;
    if (col)
      ro++;
    else
      col = n;

    dp[ro][col] = min(min(ro - 1, n - ro), min(col - 1, n - col));
    ok[ro][col] = 1;
  }

  for (int i = 1; i <= n * n; i++) {
    int ro = p[i] / n;
    int col = p[i] % n;
    if (col)
      ro++;
    else
      col = n;
    ans += dp[ro][col];
    ok[ro][col] = 0;
    process(ro, col);
    /*
    cout<<p[i]<<" "<<ro<< " "<<col<<endl;
    for(int j=1;j<=n;j++){
        for(int k=1;k<=n;k++) cout<<dp[j][k]<<" ";
        cout<<endl;
    }
    cout<<endl;
    */
  }
  cout << ans << endl;
  return 0;
}