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

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define repr(e, x) for (auto &e : x)
using namespace std;
typedef long long ll;
typedef pair<int, int> Pa;

int const veci[4] = {0, -1, 0, 1};
int const vecj[4] = {-1, 0, 1, 0};
int N;
int P[250000];
int G[500][500];
int S[500][500];

int main() {
  cin >> N;
  rep(i, N * N) cin >> P[i];
  rep(i, N) {
    rep(j, N) { G[i][j] = min(min(i, j), min(N - i - 1, N - j - 1)); }
  }
  rep(i, N) fill(S[i], S[i] + N, 1);
  // rep(i,N){rep(j,N){cout<<G[i][j];}cout<<endl;}cout<<endl;

  int ans = 0;
  rep(i, N * N) {
    int i1 = (P[i] - 1) / N;
    int j1 = (P[i] - 1) % N;
    ans += G[i1][j1];
    // cout<<"P="<<P[i]<<" ans="<<ans<<endl;
    S[i1][j1] = 0;

    queue<Pa> que;
    que.push(Pa(i1, j1));
    while (!que.empty()) {
      Pa p = que.front();
      que.pop();
      // cout<<"i="<<p.first<<", j="<<p.second<<endl;
      rep(k, 4) {
        int i2 = p.first + veci[k];
        int j2 = p.second + vecj[k];
        if (0 <= i2 && i2 < N && 0 <= j2 && j2 < N) {
          if (S[p.first][p.second] == 0 && G[p.first][p.second] < G[i2][j2]) {
            G[i2][j2] = G[p.first][p.second];
            que.push(Pa(i2, j2));
          }
          if (S[p.first][p.second] == 1 &&
              G[p.first][p.second] + 1 < G[i2][j2]) {
            G[i2][j2] = G[p.first][p.second] + 1;
            que.push(Pa(i2, j2));
          }
        }
      }
    }
    // rep(x,N){rep(y,N){cout<<G[x][y];}cout<<endl;}cout<<endl;
    // rep(x,N){rep(y,N){cout<<S[x][y];}cout<<endl;}cout<<'\\n'<<endl;
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define repr(e, x) for (auto &e : x)
using namespace std;
typedef long long ll;
typedef pair<int, int> Pa;

int const veci[4] = {0, -1, 0, 1};
int const vecj[4] = {-1, 0, 1, 0};
int N;
int P[25000];
int G[500][500];
int S[500][500];

int main() {
  cin >> N;
  rep(i, N * N) cin >> P[i];
  rep(i, N) {
    rep(j, N) { G[i][j] = min(min(i, j), min(N - i - 1, N - j - 1)); }
  }
  rep(i, N) fill(S[i], S[i] + N, 1);
  // rep(i,N){rep(j,N){cout<<G[i][j];}cout<<endl;}cout<<endl;

  int ans = 0;
  rep(i, N * N) {
    int i1 = (P[i] - 1) / N;
    int j1 = (P[i] - 1) % N;
    ans += G[i1][j1];
    // cout<<"P="<<P[i]<<" ans="<<ans<<endl;
    S[i1][j1] = 0;

    queue<Pa> que;
    que.push(Pa(i1, j1));
    while (!que.empty()) {
      Pa p = que.front();
      que.pop();
      // cout<<"i="<<p.first<<", j="<<p.second<<endl;
      rep(k, 4) {
        int i2 = p.first + veci[k];
        int j2 = p.second + vecj[k];
        if (0 <= i2 && i2 < N && 0 <= j2 && j2 < N) {
          if (S[p.first][p.second] == 0 && G[p.first][p.second] < G[i2][j2]) {
            G[i2][j2] = G[p.first][p.second];
            que.push(Pa(i2, j2));
          }
          if (S[p.first][p.second] == 1 &&
              G[p.first][p.second] + 1 < G[i2][j2]) {
            G[i2][j2] = G[p.first][p.second] + 1;
            que.push(Pa(i2, j2));
          }
        }
      }
    }
    // rep(x,N){rep(y,N){cout<<G[x][y];}cout<<endl;}cout<<endl;
    // rep(x,N){rep(y,N){cout<<S[x][y];}cout<<endl;}cout<<'\\n'<<endl;
  }

  cout << ans << endl;
}