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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

int main() {
  int n;
  vector<int> p;
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    int j;
    cin >> j;
    p.push_back(j);
  }
  int mp[n + 2][n + 2];
  int done[n + 2][n + 2];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      mp[i][j] = min(n - i, n - j);
      mp[i][j] = min(mp[i][j], i - 1);
      mp[i][j] = min(mp[i][j], j - 1);
      done[i][j] = 1;
    }
  }
  for (int i = 0; i <= n + 1; i++) {
    mp[i][0] = 0;
    mp[i][n + 1] = 0;
    mp[0][i] = 0;
    mp[n + 1][i] = 0;
  }

  ll ans = 0;
  int dir1[] = {1, 0, -1, 0};
  int dir2[] = {0, 1, 0, -1};
  for (int i = 0; i < n * n; i++) {
    int h = p[i] / n + 1;
    int w = p[i] % n;
    if (w == 0) {
      w = n;
      h--;
    }
    ans += mp[h][w];
    queue<pair<int, int>> bq;
    bq.push(make_pair(h, w));
    done[h][w] = 0;
    while (bq.size() > 0) {
      auto pt = bq.front();
      bq.pop();
      int c = mp[pt.first][pt.second] + done[pt.first][pt.second];
      for (int j = 0; j < 4; j++) {
        auto next = make_pair(pt.first + dir1[j], pt.second + dir2[j]);
        if (mp[next.first][next.second] > c) {
          mp[next.first][next.second] = c;
          bq.push(next);
        }
      }
    }
  }

  std::cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

int main() {
  int n;
  vector<int> p;
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    int j;
    cin >> j;
    p.push_back(j);
  }
  int mp[n + 2][n + 2];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      mp[i][j] = min(n - i, n - j);
      mp[i][j] = min(mp[i][j], i - 1);
      mp[i][j] = min(mp[i][j], j - 1);
    }
  }
  for (int i = 0; i <= n + 1; i++) {
    mp[i][0] = 0;
    mp[i][n + 1] = 0;
    mp[0][i] = 0;
    mp[n + 1][i] = 0;
  }

  ll ans = 0;
  int dir1[] = {1, 0, -1, 0};
  int dir2[] = {0, 1, 0, -1};
  map<pair<int, int>, int> done;
  for (int i = 0; i < n * n; i++) {
    int h = p[i] / n + 1;
    int w = p[i] % n;
    if (w == 0) {
      w = n;
      h--;
    }
    ans += mp[h][w];
    queue<pair<int, int>> bq;
    bq.push(make_pair(h, w));
    done[make_pair(h, w)] = -1;
    while (bq.size() > 0) {
      auto pt = bq.front();
      bq.pop();
      int c = mp[pt.first][pt.second];
      if (done.count(pt) == 0)
        c++;
      for (int j = 0; j < 4; j++) {
        auto next = make_pair(pt.first + dir1[j], pt.second + dir2[j]);
        if (mp[next.first][next.second] > c) {
          mp[next.first][next.second] = c;
          bq.push(next);
        }
      }
    }
  }

  std::cout << ans << endl;
  return 0;
}