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

#include <bits/stdc++.h>
using namespace std;

int64_t solve(int N, const vector<int> &perm) {
  using coord = int;
  constexpr char block = '#';
  constexpr char vacant = '.';
  const vector<pair<int, int>> diff = {make_pair(0, -1), make_pair(-1, 0),
                                       make_pair(1, 0),
                                       make_pair(0, 1)}; // 探索方向

  vector<string> board(N, string(N, block));
  vector<vector<int>> dist(N, vector<int>(N));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dist.at(i).at(j) = min(min(i, N - 1 - i), min(j, N - 1 - j));
    }
  }

  int64_t ans = 0;
  for (size_t i = 0; i < perm.size(); i++) {
    int s = perm.at(i);
    int s_x = s / N;
    int s_y = s % N;

    ans += dist.at(s_x).at(s_y);
    board.at(s_x).at(s_y) = vacant;

    deque<coord> bfs;
    bfs.push_front(s);
    while (!bfs.empty()) {
      coord p = bfs.front();
      bfs.pop_front();
      int x = p / N;
      int y = p % N;

      for (auto &[dx, dy] : diff) {
        if (x + dx <= -1 || x + dx >= N || y + dy <= -1 || y + dy >= N) {
          continue;
        } else if (board.at(x).at(y) != block) {
          if (dist.at(x + dx).at(y + dy) > dist.at(x).at(y)) {
            dist.at(x + dx).at(y + dy) = dist.at(x).at(y);
            bfs.push_front((x + dx) * N + y + dy);
          }
        } else if (board.at(x).at(y) == block) {
          if (dist.at(x + dx).at(y + dy) > dist.at(x).at(y) + 1) {
            dist.at(x + dx).at(y + dy) = dist.at(x).at(y) + 1;
            bfs.push_front((x + dx) * N + y + dy);
          }
        }
      }
    }
  }

  return ans;
}

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

  vector<int> perm(N * N);
  for (size_t i = 0; i < perm.size(); i++) {
    cin >> perm.at(i);
    perm.at(i)--; // 1-indexed -> 0-indexed
  }

  int64_t ans = solve(N, perm);

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int64_t solve(int N, const vector<int> &perm) {
  using coord = int;
  constexpr int TBC = INT_MAX;
  constexpr char block = '#';

  const vector<pair<int, int>> diff = {make_pair(0, -1), make_pair(-1, 0),
                                       make_pair(1, 0),
                                       make_pair(0, 1)}; // 探索方向

  vector<string> board(N, string(N, block));

  int64_t ans = 0;
  for (size_t i = 0; i < perm.size(); i++) {
    int s = perm.at(i);
    int s_x = s / N;
    int s_y = s % N;

    vector<vector<int>> checked(N, vector<int>(N, TBC));
    deque<coord> bfs;
    checked.at(s_x).at(s_y) = 0;
    bfs.push_front(s);
    while (!bfs.empty()) {
      coord p = bfs.front();
      bfs.pop_front();
      int x = p / N;
      int y = p % N;

      bool escaped = false;
      for (const auto &[dx, dy] : diff) {
        if (x + dx <= -1 || x + dx >= N || y + dy <= -1 || y + dy >= N) {
          escaped = true;
          ans += checked.at(x).at(y);
          board.at(s_x).at(s_y) = '.';
          break;
        } else if (board.at(x + dx).at(y + dy) != block &&
                   checked.at(x + dx).at(y + dy) == TBC) {
          checked.at(x + dx).at(y + dy) = checked.at(x).at(y);
          bfs.push_front((x + dx) * N + y + dy);
        } else if (board.at(x + dx).at(y + dy) == block &&
                   checked.at(x + dx).at(y + dy) == TBC) {
          checked.at(x + dx).at(y + dy) = checked.at(x).at(y) + 1;
          bfs.push_back((x + dx) * N + y + dy);
        }
      }
      if (escaped) {
        bfs.clear();
        break;
      }
    }
  }

  return ans;
}

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

  vector<int> perm(N * N);
  for (size_t i = 0; i < perm.size(); i++) {
    cin >> perm.at(i);
    perm.at(i)--; // 1-indexed -> 0-indexed
  }

  int64_t ans = solve(N, perm);

  cout << ans << endl;
}