Problem p02670 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;

struct pnt {
  int y, x, cost;
};

vector<vector<int>> mv = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int N;
void bfs(int y, int x, vector<vector<int>> &grid, vector<vector<int>> &attend) {
  queue<pnt> que;
  attend[y][x] = 0;
  que.push({y, x, grid[y][x] - 1});
  while ((int)que.size() > 0) {
    pnt now = que.front();
    que.pop();
    // cout << -2 << endl;
    // cout << now.y << ' ' << now.x << ' ' << now.cost << ' ' <<
    // grid[now.y][now.x] << endl;

    if (grid[now.y][now.x] > now.cost) {
      grid[now.y][now.x] = now.cost;
    } else {
      continue;
    }
    // cout << -5 << endl;

    for (auto &m : mv) {
      int ny, nx;
      ny = now.y + m[0];
      nx = now.x + m[1];
      // cout << -3 << endl;

      if (ny < 0 || nx < 0 || ny > N - 1 || nx > N - 1) {
        continue;
      } else {
        // cout << ny << ' ' << nx << endl;
        que.push({ny, nx, now.cost + attend[ny][nx]});
      }
    }
  }
}

template <typename T> void cout_vec(vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    if (i != 0) {
      cout << ' ';
    }
    cout << vec[i];
  }
  cout << endl;
}

template <typename T> void cout_mat(vector<vector<T>> &mat) {
  for (int i = 0; i < mat.size(); i++) {
    vector<T> vec = mat[i];
    cout_vec(mat[i]);
  }
}

int main() {
  cin >> N;

  vector<vector<int>> grid(N, vector<int>(N, 0));
  vector<vector<int>> attend(N, vector<int>(N, 1));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      grid[i][j] = min(min(i, N - 1 - i), min(j, N - 1 - j));
    }
  }
  // cout_mat(grid);
  // cout_mat(attend);
  // cout << endl;

  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    int p;
    cin >> p;
    p--;

    int y, x;
    y = p / N;
    x = p % N;
    // cout << -4 << endl;
    ans += grid[y][x];
    bfs(y, x, grid, attend);
    // cout_mat(grid);
    // cout_mat(attend);
    // cout<<endl;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;

struct pnt {
  int y, x, cost;
};

vector<vector<int>> mv = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int N;
int bfs(int y, int x, vector<vector<int>> &grid, vector<vector<int>> &attend) {
  queue<pnt> que;
  attend[y][x] = 0;
  que.push({y, x, grid[y][x] - 1});
  while ((int)que.size() > 0) {
    pnt now = que.front();
    que.pop();

    if (grid[now.y][now.x] > now.cost) {
      grid[now.y][now.x] = now.cost;
    } else {
      continue;
    }

    for (auto &m : mv) {
      int ny, nx;
      ny = now.y + m[0];
      nx = now.x + m[1];

      if (ny < 0 || nx < 0 || ny > N - 1 || nx > N - 1) {
        continue;
      } else {
        que.push({ny, nx, now.cost + attend[ny][nx]});
      }
    }
  }
}

template <typename T> void cout_vec(vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    if (i != 0) {
      cout << ' ';
    }
    cout << vec[i];
  }
  cout << endl;
}

template <typename T> void cout_mat(vector<vector<T>> &mat) {
  for (int i = 0; i < mat.size(); i++) {
    vector<T> vec = mat[i];
    cout_vec(mat[i]);
  }
}

int main() {
  cin >> N;

  vector<vector<int>> grid(N, vector<int>(N, 0));
  vector<vector<int>> attend(N, vector<int>(N, 1));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      grid[i][j] = min(min(i, N - 1 - i), min(j, N - 1 - j));
    }
  }
  // cout_mat(grid);
  // cout << endl;

  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    int p;
    cin >> p;
    p--;

    int y, x;
    y = p / N;
    x = p % N;
    ans += grid[y][x];
    // cout << y << ' ' << x << endl;
    bfs(y, x, grid, attend);
    // cout_mat(grid);
    // cout_mat(attend);
    // cout<<endl;
  }
  cout << ans << endl;
}