Problem p02735 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \
  do {                                                                         \
  } while (0)
#define dump(x)                                                                \
  do {                                                                         \
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dx[] = {1, 0};
int dy[] = {0, 1};

void solve() {
  int h, w;
  cin >> h >> w;
  VS s(h);
  REP(i, 0, h) cin >> s[i];
  VVI d(h, VI(w, 1 << 30));
  d[0][0] = (s[0][0] == '#');
  deque<PII> q;
  q.emplace_back(0, 0);
  while (!q.empty()) {
    auto p = q.front();
    q.pop_front();
    int x = p.first, y = p.second;
    REP(i, 0, 4) {
      int rx = x + dx[i];
      int ry = y + dy[i];
      if (rx < 0 || rx >= h)
        continue;
      if (ry < 0 || ry >= w)
        continue;
      if (d[rx][ry] <= (s[x][y] == '.' && s[rx][ry] == '#') + d[x][y])
        continue;
      d[rx][ry] = (s[x][y] == '.' && s[rx][ry] == '#') + d[x][y];
      q.emplace_back(rx, ry);
    }
  }
  cout << d[h - 1][w - 1] << endl;
}

signed main() {
  solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \
  do {                                                                         \
  } while (0)
#define dump(x)                                                                \
  do {                                                                         \
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dx[] = {1, 0};
int dy[] = {0, 1};

void solve() {
  int h, w;
  cin >> h >> w;
  VS s(h);
  REP(i, 0, h) cin >> s[i];
  VVI d(h, VI(w, 1 << 30));
  d[0][0] = (s[0][0] == '#');
  deque<PII> q;
  q.emplace_back(0, 0);
  while (!q.empty()) {
    auto p = q.front();
    q.pop_front();
    int x = p.first, y = p.second;
    REP(i, 0, 2) {
      int rx = x + dx[i];
      int ry = y + dy[i];
      if (rx < 0 || rx >= h)
        continue;
      if (ry < 0 || ry >= w)
        continue;
      if (d[rx][ry] <= (s[x][y] == '.' && s[rx][ry] == '#') + d[x][y])
        continue;
      d[rx][ry] = (s[x][y] == '.' && s[rx][ry] == '#') + d[x][y];
      q.emplace_back(rx, ry);
    }
  }
  cout << d[h - 1][w - 1] << endl;
}

signed main() {
  solve();

  return 0;
}