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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

void update(int **field, int n, int r, int c);

bool ch[500][500];

int main() {
  int n;
  cin >> n;
  int **field = new int *[n];
  for (int i = 0; i < n; i++) {
    field[i] = new int[n];
    for (int j = 0; j < n; j++) {
      field[i][j] = min(min(min(i, j), n - 1 - i), n - 1 - j);
      field[i][j] *= 2;
    }
  }
  pair<int, int> *p = new pair<int, int>[n * n];
  int tmp;
  for (int i = 0; i < n * n; i++) {
    cin >> tmp;
    tmp--;
    p[i] = make_pair(tmp / n, tmp % n);
  }
  long long ans = 0;
  for (int i = 0; i < n * n; i++) {
    ans += field[p[i].first][p[i].second] / 2;
    update(field, n, p[i].first, p[i].second);
    field[p[i].first][p[i].second]++;
  }
  cout << ans << endl;
}

void update(int **field, int n, int r, int c) {
  bool flag = false;
  if (field[r][c] % 2 == 1) {
    flag = true;
    field[r][c]--;
  }
  if (r < n - 1) {
    if (field[r + 1][c] > field[r][c]) {
      update(field, n, r + 1, c);
    }
  }
  if (c > 0) {

    if (field[r][c - 1] > field[r][c]) {
      update(field, n, r, c - 1);
    }
  }

  if (c < n - 1) {

    if (field[r][c + 1] > field[r][c]) {
      update(field, n, r, c + 1);
    }
  }
  if (r > 0) {

    if (field[r - 1][c] > field[r][c]) {
      update(field, n, r - 1, c);
    }
  }
  if (flag) {
    field[r][c]++;
  }
  field[r][c] -= 2;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int func(int **field, int r, int c, int n);

void update(int **field, int n, int r, int c, int a);

bool ch[500][500];

int main() {
  int n;
  cin >> n;
  int **field = new int *[n];
  for (int i = 0; i < n; i++) {
    field[i] = new int[n];
    for (int j = 0; j < n; j++) {
      field[i][j] = min(min(min(i, j), n - 1 - i), n - 1 - j);
      field[i][j] *= 2;
    }
  }
  pair<int, int> *p = new pair<int, int>[n * n];
  int tmp;
  for (int i = 0; i < n * n; i++) {
    cin >> tmp;
    tmp--;
    p[i] = make_pair(tmp / n, tmp % n);
  }
  long long ans = 0;
  for (int i = 0; i < n * n; i++) {
    ans += field[p[i].first][p[i].second] / 2;
    update(field, n, p[i].first, p[i].second, field[p[i].first][p[i].second]);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        ch[i][j] = false;
      }
    }
    field[p[i].first][p[i].second]++;
  }
  cout << ans << endl;
}

void update(int **field, int n, int r, int c, int a) {
  if (r < n - 1) {

    if (!ch[r + 1][c]) {
      if (field[r + 1][c] > a) {
        update(field, n, r + 1, c, field[r + 1][c]);
      } else if (field[r + 1][c] == a && a % 2 == 1) {
        update(field, n, r + 1, c, field[r + 1][c]);
      }
    }
    ch[r + 1][c] = true;
  }
  if (c > 0) {
    if (!ch[r][c - 1]) {
      if (field[r][c - 1] > a) {
        update(field, n, r, c - 1, field[r][c - 1]);
      } else if (field[r][c - 1] == a && a % 2 == 1) {
        update(field, n, r, c - 1, field[r][c - 1]);
      }
    }
    ch[r][c - 1] = true;
  }

  if (c < n - 1) {
    if (!ch[r][c + 1]) {
      if (field[r][c + 1] > a) {
        update(field, n, r, c + 1, field[r][c + 1]);
      } else if (field[r][c + 1] == a && a % 2 == 1) {
        update(field, n, r, c + 1, field[r][c + 1]);
      }
    }
    ch[r][c + 1] = true;
  }
  if (r > 0) {
    if (ch[r - 1][c]) {
      if (field[r - 1][c] > a) {
        update(field, n, r - 1, c, field[r - 1][c]);
      } else if (field[r - 1][c] == a && a % 2 == 1) {
        update(field, n, r - 1, c, field[r - 1][c]);
      }
    }
    ch[r - 1][c] = true;
  }
  field[r][c] -= 2;
}

int func(int **field, int r, int c, int n) {
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  pair<int, pair<int, int>> now;
  pq.push(make_pair(0, make_pair(r, c)));
  bool **arl = new bool *[n];
  for (int i = 0; i < n; i++) {
    arl[i] = new bool[n];
    for (int j = 0; j < n; j++) {
      arl[i][j] = false;
    }
  }
  arl[r][c] = true;
  while (1) {
    now = pq.top();
    pq.pop();
    if (now.second.first == 0 || now.second.first == n - 1 ||
        now.second.second == 0 || now.second.second == n - 1) {
      return now.first;
    }
    if (!arl[now.second.first - 1][now.second.second]) {
      pq.push(
          make_pair(now.first + field[now.second.first - 1][now.second.second],
                    make_pair(now.second.first - 1, now.second.second)));
      arl[now.second.first - 1][now.second.second] = true;
    }
    if (!arl[now.second.first + 1][now.second.second]) {
      pq.push(
          make_pair(now.first + field[now.second.first + 1][now.second.second],
                    make_pair(now.second.first + 1, now.second.second)));
      arl[now.second.first + 1][now.second.second] = true;
    }
    if (!arl[now.second.first][now.second.second - 1]) {
      pq.push(
          make_pair(now.first + field[now.second.first][now.second.second - 1],
                    make_pair(now.second.first, now.second.second - 1)));
      arl[now.second.first][now.second.second - 1] = true;
    }
    if (!arl[now.second.first][now.second.second + 1]) {
      pq.push(
          make_pair(now.first + field[now.second.first][now.second.second + 1],
                    make_pair(now.second.first, now.second.second + 1)));
      arl[now.second.first][now.second.second + 1] = true;
    }
  }
}