Problem p02670 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int N;
const int INF = 1000;
int ans[505][505];
bool used[505][505];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool in_field(int h, int w) { return h >= 0 && h < N && w >= 0 && w < N; }
int p[50000];

int to_h(int idx) { return idx / N; }

int to_w(int idx) { return idx % N; }

int calc_max(int h, int w) {
  int ret = INF;
  for (int i = 0; i < 4; i++) {
    if (in_field(h + dx[i], w + dy[i])) {
      ret = min(ret, ans[h + dx[i]][w + dy[i]]);
    } else {
      ret = 0;
    }
  }
  if (used[h][w])
    ret++;
  return ret;
}

void dfs(int h, int w) {
  ans[h][w] = calc_max(h, w);
  for (int i = 0; i < 4; i++) {
    if (in_field(h + dx[i], w + dy[i])) {
      if (calc_max(h + dx[i], w + dy[i]) < ans[h + dx[i]][w + dy[i]])
        dfs(h + dx[i], w + dy[i]);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N;
  for (int i = 0; i < N * N; i++) {
    int s;
    cin >> s;
    s--;
    p[i] = s;
  }
  ll ret = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      used[i][j] = true;
      ans[i][j] = min({i, j, (N - i - 1), (N - j - 1)}) + 1;
    }
  }

  for (int i = 0; i < N * N; i++) {
    int idx = p[i];
    int h = to_h(idx);
    int w = to_w(idx);
    ret += ans[h][w];
    used[h][w] = false;
    dfs(h, w);
    // for(int j = 0; j < N; j++){
    //     for(int k = 0; k < N; k++){
    //         cout << ans[j][k] << ' ';
    //     }
    //     cout << endl;
    // }
    // cout << endl;
  }

  cout << ret - N * N << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int N;
const int INF = 1000;
int ans[505][505];
bool used[505][505];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool in_field(int h, int w) { return h >= 0 && h < N && w >= 0 && w < N; }
int p[250005];

int to_h(int idx) { return idx / N; }

int to_w(int idx) { return idx % N; }

int calc_max(int h, int w) {
  int ret = INF;
  for (int i = 0; i < 4; i++) {
    if (in_field(h + dx[i], w + dy[i])) {
      ret = min(ret, ans[h + dx[i]][w + dy[i]]);
    } else {
      ret = 0;
    }
  }
  if (used[h][w])
    ret++;
  return ret;
}

void dfs(int h, int w) {
  ans[h][w] = calc_max(h, w);
  for (int i = 0; i < 4; i++) {
    if (in_field(h + dx[i], w + dy[i])) {
      if (calc_max(h + dx[i], w + dy[i]) < ans[h + dx[i]][w + dy[i]])
        dfs(h + dx[i], w + dy[i]);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N;
  for (int i = 0; i < N * N; i++) {
    int s;
    cin >> s;
    s--;
    p[i] = s;
  }
  ll ret = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      used[i][j] = true;
      ans[i][j] = min({i, j, (N - i - 1), (N - j - 1)}) + 1;
    }
  }

  for (int i = 0; i < N * N; i++) {
    int idx = p[i];
    int h = to_h(idx);
    int w = to_w(idx);
    ret += ans[h][w];
    used[h][w] = false;
    dfs(h, w);
    // for(int j = 0; j < N; j++){
    //     for(int k = 0; k < N; k++){
    //         cout << ans[j][k] << ' ';
    //     }
    //     cout << endl;
    // }
    // cout << endl;
  }

  cout << ret - N * N << endl;
}