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

#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

#define inf 250000
using namespace std;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int min(int a, int b, int c, int d) { return min(min(a, b), min(c, d)); }

int main() {
  ios;
  int n;
  cin >> n;

  vector<vector<bool>> oc(n, vector<bool>(n, 1));
  vector<vector<int>> dist(n, vector<int>(n));
  forn(i, n) forn(j, n) { dist[i][j] = min(i, j, n - i - 1, n - j - 1); }

  long long ans = 0;

  forn(tt, n * n) {
    int x;
    cin >> x;
    x--;
    int i = x / n, j = x % n;
    oc[i][j] = 0;
    ans += dist[i][j];

    queue<pair<int, int>> q;
    forn(d, 4) {
      int x = i + dx[d], y = j + dy[d];
      if (x < 0 || x >= n || y < 0 || y >= n)
        continue;
      if (dist[x][y] > dist[i][j]) {
        dist[x][y] = dist[i][j];
        q.push({x, y});
      }
    }

    while (!q.empty()) {
      i = q.front().first;
      j = q.front().second;
      q.pop();

      forn(d, 4) {
        int x = i + dx[d], y = j + dy[d];
        if (x < 0 || x >= n || y < 0 || y >= n)
          continue;

        if (dist[x][y] > dist[i][j] + oc[i][j]) {
          dist[x][y] = dist[i][j] + oc[i][j];
          q.push({x, y});
        }
      }
    }
  }

  cout << ans << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

#define inf 250000
using namespace std;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int main() {
  int n;
  cin >> n;

  vector<vector<bool>> oc(n, vector<bool>(n, 1));
  long long ans = 0;

  forn(tt, n * n) {
    int x;
    cin >> x;
    x--;
    int i = x / n, j = x % n;
    oc[i][j] = 0;

    vector<vector<int>> dist(n, vector<int>(n, inf));
    vector<vector<bool>> done(n, vector<bool>(n));
    deque<pair<int, int>> q;
    q.push_back({i, j});
    dist[i][j] = 0;

    while (!q.empty()) {
      i = q.front().first;
      j = q.front().second;
      q.pop_front();
      if (done[i][j])
        continue;
      done[i][j] = 1;

      forn(d, 4) {
        int x = i + dx[d], y = j + dy[d];
        if (0 <= x && x < n && 0 <= y && y < n)
          if (oc[x][y] && dist[x][y] > dist[i][j] + 1) {
            dist[x][y] = dist[i][j] + 1;
            q.push_back({x, y});
          } else if (!oc[x][y] && dist[x][y] > dist[i][j]) {
            dist[x][y] = dist[i][j];
            q.push_front({x, y});
          }
      }
    }

    int cur = inf;
    forn(i, n) {
      cur = min(cur, dist[0][i]);
      cur = min(cur, dist[i][0]);
      cur = min(cur, dist[n - 1][i]);
      cur = min(cur, dist[i][n - 1]);
    }
    ans += cur;
  }

  cout << ans << "\\n";
}