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

#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int)(x).size())

typedef long long ll;
typedef pair<int, int> pii;

const int n = 505;
const int INF = 1e5;

int N;
bool occ[n][n];
int dp[n][n];
int x[4] = {0, 1, 0, -1};
int y[4] = {1, 0, -1, 0};

void printOcc() {
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++)
      cout << occ[i][j];
    cout << endl;
  }
}
void printDp() {
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++)
      cout << dp[i][j] << " ";
    cout << endl;
  }
}

void calc(int orig = -1) {
  deque<int> Q;
  if (orig == -1) {
    for (int i = 2; i < N; i++) {
      for (int j = 2; j < N; j++)
        dp[i][j] = INF;
    }
    for (int i = 1; i <= N; i++) {
      Q.push_back(i + N + 1);
      Q.push_back(i + N * (N + 1));
      Q.push_back(1 + i * (N + 1));
      Q.push_back(N + i * (N + 1));
    }
  } else {
    Q.push_back(orig);
  }
  while (!Q.empty()) {
    int r = Q.front() / (N + 1);
    int c = Q.front() % (N + 1);
    Q.pop_front();
    if (r > N || r < 1 || c < 1 || c > N)
      continue;
    for (int i = 0; i < 4; i++) {
      if (occ[r][c]) {
        if (dp[r + y[i]][c + x[i]] > dp[r][c] + occ[r][c]) {
          dp[r + y[i]][c + x[i]] = dp[r][c] + occ[r][c];
          Q.push_back((r + y[i]) * (N + 1) + c + x[i]);
        }
      } else {
        if (dp[r + y[i]][c + x[i]] > dp[r][c] + occ[r][c]) {
          dp[r + y[i]][c + x[i]] = dp[r][c] + occ[r][c];
          Q.push_front((r + y[i]) * (N + 1) + c + x[i]);
        }
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++)
      occ[i][j] = true;
  ll res = 0;
  calc();
  vector<int> P(N * N);
  for (int i = 0; i < N * N; i++) {
    // printOcc();
    // printDp();
    // cout << endl;
    cin >> P[i];
    P[i]--;
    int r = P[i] % N + 1, c = P[i] / N + 1;
    res += dp[r][c];
    occ[r][c] = false;
    calc(r * (N + 1) + c);
  }
  cout << res << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int)(x).size())

typedef long long ll;
typedef pair<int, int> pii;

const int n = 505;
const int INF = 1e5;

int N;
bool occ[n][n];
int dp[n][n];
int x[4] = {0, 1, 0, -1};
int y[4] = {1, 0, -1, 0};

void printOcc() {
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++)
      cout << occ[i][j];
    cout << endl;
  }
}
void printDp() {
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++)
      cout << dp[i][j] << " ";
    cout << endl;
  }
}

void calc() {
  deque<int> Q;
  for (int i = 2; i < N; i++) {
    for (int j = 2; j < N; j++)
      dp[i][j] = INF;
  }
  for (int i = 1; i <= N; i++) {
    Q.push_back(i + N + 1);
    Q.push_back(i + N * (N + 1));
    Q.push_back(1 + i * (N + 1));
    Q.push_back(N + i * (N + 1));
  }
  while (!Q.empty()) {
    int r = Q.front() / (N + 1);
    int c = Q.front() % (N + 1);
    Q.pop_front();
    if (r > N || r < 1 || c < 1 || c > N)
      continue;
    for (int i = 0; i < 4; i++) {
      if (occ[r][c]) {
        if (dp[r + y[i]][c + x[i]] > dp[r][c] + occ[r][c]) {
          dp[r + y[i]][c + x[i]] = dp[r][c] + occ[r][c];
          Q.push_back((r + y[i]) * (N + 1) + c + x[i]);
        }
      } else {
        if (dp[r + y[i]][c + x[i]] > dp[r][c] + occ[r][c]) {
          dp[r + y[i]][c + x[i]] = dp[r][c] + occ[r][c];
          Q.push_front((r + y[i]) * (N + 1) + c + x[i]);
        }
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++)
      occ[i][j] = true;
  ll res = 0;
  vector<int> P(N * N);
  for (int i = 0; i < N * N; i++) {
    calc();
    // printOcc();
    // printDp();
    // cout << endl;
    cin >> P[i];
    P[i]--;
    res += dp[P[i] % N + 1][P[i] / N + 1];
    occ[P[i] % N + 1][P[i] / N + 1] = false;
  }
  cout << res << endl;

  return 0;
}