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

#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <map>
#include <math.h>
#include <vector>
using namespace std;
map<int, int> ct;
map<int, int> ct2;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
#define INF 0x3f3f3f3f
#define MOD 998244353LL
#define EPS 0.00001
#define f first
#define s second
#define pb push_back
#define mp make_pair

#define FOR(i, a, b) for (int i = (a); i <= (signed)(b); i++)
#define AFOR(i, a) for (int i = 0; i < (signed)(a); i++)
#define RFOR(i, a, b) for (int i = (a); i >= b; i--)

#define MN 100005
int n, m, l;
int a, b, c;
string s[MN];
string str;
string ans;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

/*
int N;
cin >> N;
vi vect(N);
FOR(i,N) cin >> vec.at(i);*/
// sort(vec.begin(),vec.end());
// vvi data(N,vi(4));

/*	int bfs(int x, int y,vvi &A,vvi flag) {
                queue<pii> que;
                que.push(pii(x, y));
                while (que.size()) {
                        pii p = que.front(); que.pop();
                        AFOR(i, 4) {
                                int nx = p.first + dx[i]; int ny = p.second +
   dy[i]; if (0 <= nx && nx < N && 0 <= ny && ny < N) { if
   (A.at(p.first).at(p.second) + flag.at(p.first).at(p.second) <
   A.at(nx).at(ny)) { A.at(nx).at(ny) = A.at(p.first).at(p.second) +
   flag.at(p.first).at(p.second); que.push(pii(nx, ny));
                                        }
                                }
                        }
                }
                return 0;
        }
        */

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long sum = 0;
  int N;
  cin >> N;
  vi P(N * N);
  vvi A(N, vector<int>(N));
  vvi flag(N, vector<int>(N));

  AFOR(i, N) {
    AFOR(j, N) { flag.at(i).at(j) = 1; }
  }

  if (N % 2 == 0) {
    AFOR(i, N / 2) {
      AFOR(j, N / 2) {
        A.at(i).at(j) = min(i, j);
        A.at(N - i - 1).at(j) = min(i, j);
        A.at(N - i - 1).at(N - j - 1) = min(i, j);
        A.at(i).at(N - j - 1) = min(i, j);
      }
    }
  } else {
    AFOR(i, N / 2) {
      A.at(N / 2).at(i) = i;
      A.at(i).at(N / 2) = i;
      A.at(N - i - 1).at(N / 2) = i;
      A.at(N / 2).at(N - i - 1) = i;
      AFOR(j, N / 2) {
        A.at(i).at(j) = min(i, j);
        A.at(N - i - 1).at(j) = min(i, j);
        A.at(N - i - 1).at(N - j - 1) = min(i, j);
        A.at(i).at(N - j - 1) = min(i, j);
      }
    }
    A.at(N / 2).at(N / 2) = N / 2;
  }

  AFOR(i, N * N) {
    int x, y;
    cin >> P.at(i);
    x = P.at(i) / N;
    y = P.at(i) % N - 1;
    if (y == -1) {
      x = x - 1;
      y = y + N;
    }
    sum += A.at(x).at(y);
    flag.at(x).at(y) = 0;
    queue<pii> que;
    que.push(pii(x, y));
    while (que.size()) {
      pii p = que.front();
      que.pop();
      AFOR(i, 4) {
        int nx = p.first + dx[i];
        int ny = p.second + dy[i];
        if (0 <= nx && nx < N && 0 <= ny && ny < N) {
          if (A.at(p.first).at(p.second) + flag.at(p.first).at(p.second) <
              A.at(nx).at(ny)) {
            A.at(nx).at(ny) =
                A.at(p.first).at(p.second) + flag.at(p.first).at(p.second);
            que.push(pii(nx, ny));
          }
        }
      }
    }

    //	bfs(x, y, A,flag);
    //	cout << sum << endl;
  }

  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
map<int, int> ct;
map<int, int> ct2;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
#define INF 0x3f3f3f3f
#define MOD 998244353LL
#define EPS 0.00001
#define f first
#define s second
#define pb push_back
#define mp make_pair

#define FOR(i, a, b) for (int i = (a); i <= (signed)(b); i++)
#define AFOR(i, a) for (int i = 0; i < (signed)(a); i++)
#define RFOR(i, a, b) for (int i = (a); i >= b; i--)

#define MN 100005
int n, m, l;
int a, b, c;
string s[MN];
string str;
string ans;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

/*
int N;
cin >> N;
vi vect(N);
FOR(i,N) cin >> vec.at(i);*/
// sort(vec.begin(),vec.end());
// vvi data(N,vi(4));

int N;

int bfs(int x, int y, vvi &A, vvi flag) {
  queue<pii> que;
  que.push(pii(x, y));
  //	vvi history(N,vi (N));
  //	AFOR(i, N) {
  //	AFOR(j, N) {
  //	history.at(i).at(j) = 0;
  //	}
  //	}
  while (que.size()) {
    pii p = que.front();
    que.pop();
    //	history.at(p.first).at(p.second) = 1;
    AFOR(i, 4) {
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];
      //	if (history.at(nx).at(ny) == 1) continue;
      if (0 <= nx && nx < N && 0 <= ny && ny < N) {
        if (A.at(p.first).at(p.second) + flag.at(p.first).at(p.second) <
            A.at(nx).at(ny)) {
          A.at(nx).at(ny) =
              A.at(p.first).at(p.second) + flag.at(p.first).at(p.second);
          que.push(pii(nx, ny));
        }
      }
    }
  }
  return 0;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long sum = 0;
  cin >> N;
  vi P(N * N);
  vvi A(N, vector<int>(N));
  vvi flag(N, vector<int>(N));

  AFOR(i, N) {
    AFOR(j, N) { flag.at(i).at(j) = 1; }
  }

  if (N % 2 == 0) {
    AFOR(i, N / 2) {
      AFOR(j, N / 2) {
        A.at(i).at(j) = min(i, j);
        A.at(N - i - 1).at(j) = min(i, j);
        A.at(N - i - 1).at(N - j - 1) = min(i, j);
        A.at(i).at(N - j - 1) = min(i, j);
      }
    }
  } else {
    AFOR(i, N / 2) {
      A.at(N / 2).at(i) = i;
      A.at(i).at(N / 2) = i;
      A.at(N - i - 1).at(N / 2) = i;
      A.at(N / 2).at(N - i - 1) = i;
      AFOR(j, N / 2) {
        A.at(i).at(j) = min(i, j);
        A.at(N - i - 1).at(j) = min(i, j);
        A.at(N - i - 1).at(N - j - 1) = min(i, j);
        A.at(i).at(N - j - 1) = min(i, j);
      }
    }
    A.at(N / 2).at(N / 2) = N / 2;
  }

  AFOR(i, N * N) {
    int x, y;
    cin >> P.at(i);
    x = P.at(i) / N;
    y = P.at(i) % N - 1;
    if (y == -1) {
      x = x - 1;
      y = y + N;
    }
    sum += A.at(x).at(y);
    flag.at(x).at(y) = 0;

    bfs(x, y, A, flag);
    //	cout << sum << endl;
  }

  cout << sum << endl;
  return 0;
}