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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define pqueue priority_queue
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  int N;
  cin >> N;
  int P[N * N];
  for (int i = 0; i < N * N; ++i) {
    cin >> P[i];
    P[i]--;
  }
  int DP[N][N];
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      DP[i][j] = min(i, min(j, min(N - 1 - i, N - 1 - j)));
    }
  }
  bool oru[N][N];
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      oru[i][j] = true;
    }
  }
  int ans = 0;
  queue<pair<int, int>> que;
  int x, y, cost;
  pair<int, int> state;
  for (int i = 0; i < N * N; ++i) {
    ans += DP[P[i] / N][P[i] % N];
    oru[P[i] / N][P[i] % N] = false;
    que.push(mp(P[i], DP[P[i] / N][P[i] % N]));
    while (!que.empty()) {
      state = que.front();
      que.pop();
      x = state.fi / N;
      y = state.fi % N;
      cost = state.se;
      if (oru[x][y])
        cost++;
      if (x > 0 && DP[x - 1][y] > cost) {
        DP[x - 1][y] = cost;
        que.push(mp((x - 1) * N + y, cost));
      }
      if (y > 0 && DP[x][y - 1] > cost) {
        DP[x][y - 1] = cost;
        que.push(mp(x * N + (y - 1), cost));
      }
      if (x + 1 < N && DP[x + 1][y] > cost) {
        DP[x + 1][y] = cost;
        que.push(mp((x + 1) * N + y, cost));
      }
      if (y + 1 < N && DP[x][y + 1] > cost) {
        DP[x][y + 1] = cost;
        que.push(mp(x * N + (y + 1), cost));
      }
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define pqueue priority_queue
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  int N;
  cin >> N;
  int P[N * N];
  for (int i = 0; i < N * N; ++i) {
    cin >> P[i];
    P[i]--;
  }
  int DP[N][N];
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      DP[i][j] = min(i, min(j, min(N - 1 - i, N - 1 - j)));
    }
  }
  bool oru[N][N];
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      oru[i][j] = true;
    }
  }
  int ans = 0;
  queue<pair<int, int>> que;
  int x, y, cost;
  pair<int, int> state;
  for (int i = 0; i < N * N; ++i) {
    ans += DP[P[i] / N][P[i] % N];
    oru[P[i] / N][P[i] % N] = false;
    que.push(mp(P[i], DP[P[i] / N][P[i] % N]));
    while (!que.empty()) {
      state = que.front();
      que.pop();
      x = state.fi / N;
      y = state.fi % N;
      cost = state.se;
      if (DP[x][y] < cost)
        continue;
      DP[x][y] = cost;
      if (oru[x][y])
        cost++;
      if (x > 0 && DP[x - 1][y] > cost)
        que.push(mp((x - 1) * N + y, cost));
      if (y > 0 && DP[x][y - 1] > cost)
        que.push(mp(x * N + (y - 1), cost));
      if (x + 1 < N && DP[x + 1][y] > cost)
        que.push(mp((x + 1) * N + y, cost));
      if (y + 1 < N && DP[x][y + 1] > cost)
        que.push(mp(x * N + (y + 1), cost));
    }
  }
  cout << ans << endl;
}