Problem p02735 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FAST                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define FIXED cout << fixed << setprecision(12)
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define graph vector<vector<int>>
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define f first
#define s second
#define hashmap unordered_map
#define hashset unordered_set
#define eps 1e-9
#define mod 1000000007
#define inf 3000000000000000007ll
#define sz(a) signed(a.size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

#ifdef DEBUG
mt19937 gen(857204);
#else
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif

template <class T, class U> inline void checkmin(T &x, U y) {
  if (y < x)
    x = y;
}
template <class T, class U> inline void checkmax(T &x, U y) {
  if (y > x)
    x = y;
}
template <class T, class U> inline bool ifmax(T &x, U y) {
  if (y > x)
    return x = y, true;
  return false;
}
template <class T, class U> inline bool ifmin(T &x, U y) {
  if (y < x)
    return x = y, true;
  return false;
}
template <class T> inline void sort(T &a) { sort(all(a)); }
template <class T> inline void rsort(T &a) { sort(rall(a)); }
template <class T> inline void reverse(T &a) { reverse(all(a)); }
template <class T, class U>
inline istream &operator>>(istream &str, pair<T, U> &p) {
  return str >> p.f >> p.s;
}
template <class T> inline istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a)
    str >> i;
  return str;
}
template <class T> inline T sorted(T a) {
  sort(a);
  return a;
}

signed main() {
  FAST;
  FIXED;
  int n, m;
  cin >> n >> m;
  vector<string> a(n);
  cin >> a;
  vector<vector<vector<pair<int, pii>>>> go(n,
                                            vector<vector<pair<int, pii>>>(m));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      if (a[i][j] == '.') {
        if (i < n - 1) {
          if (a[i + 1][j] == '#') {
            go[i][j].pb({1, {i + 1, j}});
          } else {
            go[i][j].pb({0, {i + 1, j}});
          }
        }
        if (j < m - 1) {
          if (a[i][j + 1] == '#') {
            go[i][j].pb({1, {i, j + 1}});
          } else {
            go[i][j].pb({0, {i, j + 1}});
          }
        }
      } else {
        if (i < n - 1)
          go[i][j].pb({0, {i + 1, j}});
        if (j < m - 1)
          go[i][j].pb({0, {i, j + 1}});
      }
    }
  for (auto &i : go)
    for (auto &j : i)
      sort(j);
  deque<pii> que;
  que.pb({0, 0});
  vector<vector<int>> dist(n, vector<int>(m, mod));
  dist[0][0] = 0;
  while (sz(que)) {
    auto f = que.front();
    que.popf();
    for (auto i : go[f.f][f.s])
      if (ifmin(dist[i.s.f][i.s.s], dist[f.f][f.s] + i.f)) {
        if (i.f == 0)
          que.push_front(i.s);
        else
          que.push_back(i.s);
      }
  }
  cout << dist[n - 1][m - 1] + (a[0][0] == '#');
#ifdef DEBUG
  cerr << "Runtime is: " << clock() * 1.0 / CLOCKS_PER_SEC << endl;
#endif
  return 0;
}

Fixed submission:
#pragma GCC optimize("O3", "unroll-loops")

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FAST                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define FIXED cout << fixed << setprecision(12)
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define graph vector<vector<int>>
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define f first
#define s second
#define hashmap unordered_map
#define hashset unordered_set
#define eps 1e-9
#define mod 1000000007
#define inf 3000000000000000007ll
#define sz(a) signed(a.size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

#ifdef DEBUG
mt19937 gen(857204);
#else
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif

template <class T, class U> inline void checkmin(T &x, U y) {
  if (y < x)
    x = y;
}
template <class T, class U> inline void checkmax(T &x, U y) {
  if (y > x)
    x = y;
}
template <class T, class U> inline bool ifmax(T &x, U y) {
  if (y > x)
    return x = y, true;
  return false;
}
template <class T, class U> inline bool ifmin(T &x, U y) {
  if (y < x)
    return x = y, true;
  return false;
}
template <class T> inline void sort(T &a) { sort(all(a)); }
template <class T> inline void rsort(T &a) { sort(rall(a)); }
template <class T> inline void reverse(T &a) { reverse(all(a)); }
template <class T, class U>
inline istream &operator>>(istream &str, pair<T, U> &p) {
  return str >> p.f >> p.s;
}
template <class T> inline istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a)
    str >> i;
  return str;
}
template <class T> inline T sorted(T a) {
  sort(a);
  return a;
}

signed main() {
  FAST;
  FIXED;
  int n, m;
  cin >> n >> m;
  vector<string> a(n);
  cin >> a;
  vector<vector<vector<pair<int, pii>>>> go(n,
                                            vector<vector<pair<int, pii>>>(m));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      if (a[i][j] == '.') {
        if (i < n - 1) {
          if (a[i + 1][j] == '#') {
            go[i][j].pb({1, {i + 1, j}});
          } else {
            go[i][j].pb({0, {i + 1, j}});
          }
        }
        if (j < m - 1) {
          if (a[i][j + 1] == '#') {
            go[i][j].pb({1, {i, j + 1}});
          } else {
            go[i][j].pb({0, {i, j + 1}});
          }
        }
      } else {
        if (i < n - 1)
          go[i][j].pb({0, {i + 1, j}});
        if (j < m - 1)
          go[i][j].pb({0, {i, j + 1}});
      }
    }
  for (auto &i : go)
    for (auto &j : i)
      sort(j);
  deque<pii> que;
  que.pb({0, 0});
  vector<vector<int>> dist(n, vector<int>(m, mod));
  dist[0][0] = 0;
  while (sz(que)) {
    auto f = que.front();
    que.popf();
    for (auto i : go[f.f][f.s])
      if (ifmin(dist[i.s.f][i.s.s], dist[f.f][f.s] + i.f)) {
        if (i.f == 0)
          que.push_front(i.s);
        else
          que.push_back(i.s);
      }
  }
  cout << dist[n - 1][m - 1] + (a[0][0] == '#');
#ifdef DEBUG
  cerr << "Runtime is: " << clock() * 1.0 / CLOCKS_PER_SEC << endl;
#endif
  return 0;
}