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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

void update_map(int row, int col, vector<vector<int>> &vv,
                vector<vector<int>> &m, int val) {
  int n = m.size();
  if (row < 0 || col < 0 || row >= n || col >= n)
    return;
  if (m[row][col] <= val)
    return;
  m[row][col] = val;
  update_map(row + 1, col, vv, m, m[row][col] + vv[row][col]);
  update_map(row - 1, col, vv, m, m[row][col] + vv[row][col]);
  update_map(row, col - 1, vv, m, m[row][col] + vv[row][col]);
  update_map(row, col + 1, vv, m, m[row][col] + vv[row][col]);
}

int main() {
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n * n; i++) {
    int t;
    cin >> t;
    v.push_back(t);
  }
  vector<vector<int>> vv(n, vector<int>(n, 1));
  vector<vector<int>> m(n, vector<int>(n, n * n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      m[i][j] = min(min(i, j), min(n - 1 - i, n - 1 - j));
    }
  }
  int res = 0;
  for (int i = 0; i < n * n; i++) {
    int row = (v[i] - 1) / n, col = (v[i] - 1) % n;
    res += m[row][col];
    if (row != 0)
      m[row][col] = min(m[row][col], m[row - 1][col] + vv[row - 1][col]);
    if (row != n - 1)
      m[row][col] = min(m[row][col], m[row + 1][col] + vv[row + 1][col]);
    if (col != 0)
      m[row][col] = min(m[row][col], m[row][col - 1] + vv[row][col - 1]);
    if (col != n - 1)
      m[row][col] = min(m[row][col], m[row][col + 1] + vv[row][col + 1]);
    vv[row][col] = 0;
    update_map(row - 1, col, vv, m, m[row][col]);
    update_map(row + 1, col, vv, m, m[row][col]);
    update_map(row, col - 1, vv, m, m[row][col]);
    update_map(row, col + 1, vv, m, m[row][col]);
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n * n; i++) {
    int t;
    cin >> t;
    v.push_back(t);
  }
  vector<pair<int, int>> dir = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
  vector<vector<int>> vv(n, vector<int>(n, 1));
  int res = 0;
  for (int i = 0; i < n * n; i++) {
    int r = n * n;
    vector<vector<int>> m(n, vector<int>(n, n * n));
    int row = (v[i] - 1) / n, col = (v[i] - 1) % n;
    vector<pair<int, int>> vp;
    vp.push_back(make_pair(v[i] - 1, 0));
    while (vp.size() > 0) {
      int pos = vp.back().first, hate = vp.back().second;
      if (pos / n == 0 || pos / n == n - 1 || pos % n == 0 || pos % n == n - 1)
        r = min(r, hate);
      vp.pop_back();
      row = pos / n, col = pos % n;
      for (auto d : dir) {
        int x = row + d.first, y = col + d.second;
        if (x < 0 || y < 0 || x >= n || y >= n)
          continue;
        int updated_hate = hate;
        if (vv[x][y] == 1)
          updated_hate++;
        if (updated_hate >= m[x][y])
          continue;
        m[x][y] = updated_hate;
        vp.push_back(make_pair(x * n + y, updated_hate));
      }
    }
    vv[(v[i] - 1) / n][(v[i] - 1) % n] = 0;
    res += r;
  }
  cout << res << endl;
}