Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define MOD (long long int)(1e9 + 7)
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define FOR(i, a, b) for (int i = a; i < (int)(b); i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset((a), 0, sizeof(a))
#define PB push_back
#define MP make_pair
#define SP << " " <<
const int INF = 1001001001;
const ll LINF = 100100100100100100;
const double EPS = 1e-10;
const long double PI = acos(-1.0L);
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))

__attribute__((constructor)) void initial() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

int n;
int maze[505][505];
int d[505][505];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void bfs(int sx, int sy) {
  queue<PII> q;
  d[sx][sy]--;
  q.push(MP(sx, sy));
  while (q.size()) {
    PII p = q.front();
    q.pop();
    rep(i, 4) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < n) {
        if (maze[nx][ny]) {
          if (d[nx][ny] > d[p.first][p.second] + 1) {
            d[nx][ny] = d[p.first][p.second] + 1;
            q.push(MP(nx, ny));
          }
        } else {
          if (d[nx][ny] > d[p.first][p.second]) {
            d[nx][ny] = d[p.first][p.second];
            q.push(MP(nx, ny));
          }
        }
      }
    }
  }
}

signed main() {
  cin >> n;
  int ans = 0;
  rep(i, n) rep(j, n) d[i][j] = min(min(i, n - 1 - i), min(j, n - j - 1)) + 1,
                      maze[i][j] = 1;
  rep(i, n * n) {
    int p;
    cin >> p;
    p--;
    int sx = p / n, sy = p % n;
    ans += d[sx][sy] - 1;
    maze[sx][sy]--;
    bfs(sx, sy);
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD (long long int)(1e9 + 7)
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define FOR(i, a, b) for (int i = a; i < (int)(b); i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset((a), 0, sizeof(a))
#define PB push_back
#define MP make_pair
#define SP << " " <<
const int INF = 1001001001;
const ll LINF = 100100100100100100;
const double EPS = 1e-10;
const long double PI = acos(-1.0L);
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))

__attribute__((constructor)) void initial() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

int n;
int maze[505][505];
int cl[505][505];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int bfs(int sx, int sy) {
  if (sx == 0 || sy == 0 || sy == n - 1 || sy == n - 1)
    return 0;
  priority_queue<pair<int, PII>, vector<pair<int, PII>>,
                 greater<pair<int, PII>>>
      q;
  int d[505][505];
  int ret = INF;
  rep(i, n) rep(j, n) d[i][j] = INF;
  q.push(MP(0, MP(sx, sy)));
  d[sx][sy] = 0;
  while (q.size()) {
    pair<int, PII> u = q.top();
    q.pop();
    if (u.first >= ret)
      return ret;
    PII p = u.second;
    if (p.first == n - 1 || p.first == 0 || p.second == n - 1 || p.second == 0)
      return u.first;
    rep(i, 4) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < n) {
        if (cl[nx][ny] && d[p.first][p.second] == 0)
          return 0;
        if (maze[nx][ny]) {
          if (d[nx][ny] > d[p.first][p.second] + 1) {
            d[nx][ny] = d[p.first][p.second] + 1;
            q.push(MP(d[nx][ny], MP(nx, ny)));
            // if(nx==0||ny==0||nx==n-1||ny==n-1) return d[nx][ny];
          }
          // cerr<<"BFS MP" SP nx SP ny<<endl;
        } else {
          if (d[nx][ny] > d[p.first][p.second]) {
            d[nx][ny] = d[p.first][p.second];
            q.push(MP(d[nx][ny], MP(nx, ny)));
            // if(nx==0||ny==0||nx==n-1||ny==n-1) return d[nx][ny];
            // cerr<<"BFS MP" SP nx SP ny<<endl;
          }
        }
      }
    }
  }

  rep(i, n) {
    chmin(ret, d[i][0]);
    chmin(ret, d[i][n - 1]);
    chmin(ret, d[0][i]);
    chmin(ret, d[n - 1][i]);
  }

  return ret;
}

signed main() {
  cin >> n;
  int ans = 0;
  rep(i, n) rep(j, n) maze[i][j] = 1, cl[i][j] = 0;
  rep(i, n * n) {
    int p;
    cin >> p;
    p--;
    int sx = p / n, sy = p % n;
    int cnt = bfs(sx, sy);
    ans += cnt;
    if (cnt == 0)
      cl[sx][sy] = 1;
    maze[sx][sy] = 0;
  }
  cout << ans << endl;

  return 0;
}