Problem p02670 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mo 100000007
using namespace std;
long long read() {
  long long xx = 0, flagg = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-') {
    flagg = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * flagg;
}
void pus(long long xx, long long flagg) {
  if (xx < 0) {
    putchar('-');
    xx = -xx;
  }
  if (xx >= 10)
    pus(xx / 10, 0);
  putchar(xx % 10 + '0');
  if (flagg == 1)
    putchar(' ');
  if (flagg == 2)
    putchar('\n');
  return;
}
int n, m, i, j, k, p, vis[505][505], head, tail, zhanx[100005], zhany[100005],
    x, y;
int book[505][505], now1, now2, ans;
void BFS(int u, int v) {
  vis[u][v]--;
  ans += vis[u][v];
  book[u][v] = 1;
  head = 1;
  tail = 1;
  zhanx[1] = u;
  zhany[1] = v;
  now2 = 0;
  while (tail != now2) {
    now1 = head;
    while (head <= tail) {
      x = zhanx[head];
      y = zhany[head];
      if (book[x - 1][y] == 1 && x >= 1) {
        if (vis[x - 1][y] > vis[x][y]) {
          tail++;
          zhanx[tail] = x - 1;
          zhany[tail] = y;
          vis[x - 1][y] = vis[x][y];
        }
      }
      if (book[x + 1][y] == 1 && x <= n) {
        if (vis[x + 1][y] > vis[x][y]) {
          tail++;
          zhanx[tail] = x + 1;
          zhany[tail] = y;
          vis[x + 1][y] = vis[x][y];
        }
      }
      if (book[x][y - 1] == 1 && y >= 1) {
        if (vis[x][y - 1] > vis[x][y]) {
          tail++;
          zhanx[tail] = x;
          zhany[tail] = y - 1;
          vis[x][y - 1] = vis[x][y];
        }
      }
      if (book[x][y + 1] == 1 && y <= n) {
        if (vis[x][y + 1] > vis[x][y]) {
          tail++;
          zhanx[tail] = x;
          zhany[tail] = y + 1;
          vis[x][y + 1] = vis[x][y];
        }
      }
      head++;
    }
    head = now1;
    now2 = tail;
    while (head <= now2) {
      x = zhanx[head];
      y = zhany[head];
      if (book[x - 1][y] == 0 && x >= 1) {
        if (vis[x - 1][y] > vis[x][y] + 1) {
          tail++;
          zhanx[tail] = x - 1;
          zhany[tail] = y;
          vis[x - 1][y] = vis[x][y] + 1;
        }
      }
      if (book[x + 1][y] == 0 && x <= n) {
        if (vis[x + 1][y] > vis[x][y] + 1) {
          tail++;
          zhanx[tail] = x + 1;
          zhany[tail] = y;
          vis[x + 1][y] = vis[x][y] + 1;
        }
      }
      if (book[x][y - 1] == 0 && y >= 1) {
        if (vis[x][y - 1] > vis[x][y] + 1) {
          tail++;
          zhanx[tail] = x;
          zhany[tail] = y - 1;
          vis[x][y - 1] = vis[x][y] + 1;
        }
      }
      if (book[x][y + 1] == 0 && y <= n) {
        if (vis[x][y + 1] > vis[x][y] + 1) {
          tail++;
          zhanx[tail] = x;
          zhany[tail] = y + 1;
          vis[x][y + 1] = vis[x][y] + 1;
        }
      }
      head++;
    }
  }
}
int main() {
  // freopen("domino.in","r",stdin);
  // freopen("domino.out","w",stdout);
  n = read();
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      vis[i][j] = min(min(i, n - i + 1), min(j, n - j + 1));
  for (i = 1; i <= n * n; i++) {
    p = read();
    BFS((p - 1) / n + 1, (p - 1) % n + 1);
  }
  pus(ans, 2);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mo 100000007
using namespace std;
long long read() {
  long long xx = 0, flagg = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-') {
    flagg = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * flagg;
}
void pus(long long xx, long long flagg) {
  if (xx < 0) {
    putchar('-');
    xx = -xx;
  }
  if (xx >= 10)
    pus(xx / 10, 0);
  putchar(xx % 10 + '0');
  if (flagg == 1)
    putchar(' ');
  if (flagg == 2)
    putchar('\n');
  return;
}
int n, m, i, j, k, p, vis[505][505], head, tail, zhanx[250005], zhany[250005],
    x, y;
int book[505][505], now1, now2, ans;
void BFS(int u, int v) {
  vis[u][v]--;
  ans += vis[u][v];
  book[u][v] = 1;
  head = 1;
  tail = 1;
  zhanx[1] = u;
  zhany[1] = v;
  now2 = 0;
  while (tail != now2) {
    now1 = head;
    while (head <= tail) {
      x = zhanx[head];
      y = zhany[head];
      if (book[x - 1][y] == 1 && x >= 1) {
        if (vis[x - 1][y] > vis[x][y]) {
          tail++;
          zhanx[tail] = x - 1;
          zhany[tail] = y;
          vis[x - 1][y] = vis[x][y];
        }
      }
      if (book[x + 1][y] == 1 && x <= n) {
        if (vis[x + 1][y] > vis[x][y]) {
          tail++;
          zhanx[tail] = x + 1;
          zhany[tail] = y;
          vis[x + 1][y] = vis[x][y];
        }
      }
      if (book[x][y - 1] == 1 && y >= 1) {
        if (vis[x][y - 1] > vis[x][y]) {
          tail++;
          zhanx[tail] = x;
          zhany[tail] = y - 1;
          vis[x][y - 1] = vis[x][y];
        }
      }
      if (book[x][y + 1] == 1 && y <= n) {
        if (vis[x][y + 1] > vis[x][y]) {
          tail++;
          zhanx[tail] = x;
          zhany[tail] = y + 1;
          vis[x][y + 1] = vis[x][y];
        }
      }
      head++;
    }
    head = now1;
    now2 = tail;
    while (head <= now2) {
      x = zhanx[head];
      y = zhany[head];
      if (book[x - 1][y] == 0 && x >= 1) {
        if (vis[x - 1][y] > vis[x][y] + 1) {
          tail++;
          zhanx[tail] = x - 1;
          zhany[tail] = y;
          vis[x - 1][y] = vis[x][y] + 1;
        }
      }
      if (book[x + 1][y] == 0 && x <= n) {
        if (vis[x + 1][y] > vis[x][y] + 1) {
          tail++;
          zhanx[tail] = x + 1;
          zhany[tail] = y;
          vis[x + 1][y] = vis[x][y] + 1;
        }
      }
      if (book[x][y - 1] == 0 && y >= 1) {
        if (vis[x][y - 1] > vis[x][y] + 1) {
          tail++;
          zhanx[tail] = x;
          zhany[tail] = y - 1;
          vis[x][y - 1] = vis[x][y] + 1;
        }
      }
      if (book[x][y + 1] == 0 && y <= n) {
        if (vis[x][y + 1] > vis[x][y] + 1) {
          tail++;
          zhanx[tail] = x;
          zhany[tail] = y + 1;
          vis[x][y + 1] = vis[x][y] + 1;
        }
      }
      head++;
    }
  }
}
int main() {
  // freopen("domino.in","r",stdin);
  // freopen("domino.out","w",stdout);
  n = read();
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      vis[i][j] = min(min(i, n - i + 1), min(j, n - j + 1));
  for (i = 1; i <= n * n; i++) {
    p = read();
    BFS((p - 1) / n + 1, (p - 1) % n + 1);
  }
  pus(ans, 2);
  return 0;
}