prithivMLmods/Coder-Stat huggingface dataset:
Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define all(x) (x).begin(), (x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
constexpr auto INF = INT_MAX >> 1;
constexpr auto LINF = 5000000000000000;
constexpr auto MOD = 1000000007;

int main() {

  int h, w;
  cin >> h >> w;
  V<string> s(h);
  rep(i, h) cin >> s[i];

  deque<P> dq;
  dq.push_front(P(0, 0));
  vvi dist(h, vi(w, INF));
  dist[0][0] = s[0][0] == '#' ? 1 : 0;

  while (!dq.empty()) {
    int y = dq.front().first;
    int x = dq.front().second;
    dq.pop_front();
    vi dy = {0, 1};
    vi dx = {1, 0};
    rep(i, 2) {
      int ey = y + dy[i];
      int ex = x + dx[i];
      if (ey < 0 || h <= ey || ex < 0 || w <= ex)
        continue;
      int d = 0;
      if (s[y][x] == '.' && s[ey][ex] == '#')
        d = 1;
      if (dist[ey][ex] < dist[y][x] + d)
        continue;
      dist[ey][ex] = dist[y][x] + d;
      if (d)
        dq.push_back(P(ey, ex));
      else
        dq.push_front(P(ey, ex));
    }
  }

  cout << dist[h - 1][w - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define all(x) (x).begin(), (x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
constexpr auto INF = INT_MAX >> 1;
constexpr auto LINF = 5000000000000000;
constexpr auto MOD = 1000000007;

int main() {

  int h, w;
  cin >> h >> w;
  V<string> s(h);
  rep(i, h) cin >> s[i];

  deque<P> dq;
  dq.push_front(P(0, 0));
  vvi dist(h, vi(w, INF));
  dist[0][0] = s[0][0] == '#' ? 1 : 0;

  while (!dq.empty()) {
    int y = dq.front().first;
    int x = dq.front().second;
    dq.pop_front();
    vi dy = {0, 1};
    vi dx = {1, 0};
    rep(i, 2) {
      int ey = y + dy[i];
      int ex = x + dx[i];
      if (ey < 0 || h <= ey || ex < 0 || w <= ex)
        continue;
      int d = 0;
      if (s[y][x] == '.' && s[ey][ex] == '#')
        d = 1;
      if (dist[ey][ex] <= dist[y][x] + d)
        continue;
      dist[ey][ex] = dist[y][x] + d;
      if (d)
        dq.push_back(P(ey, ex));
      else
        dq.push_front(P(ey, ex));
    }
  }

  cout << dist[h - 1][w - 1] << endl;
}
Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const ll MOD = 1000000007;
const long long INF = 1LL << 60;
const double pi = acos(-1.0);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

vector<string> grid;
ll H, W;

bool movable(ll sh, ll sw) {
  if (sh < 0 || sh >= H || sw < 0 || sw >= W)
    return false;
  return true;
}

vector<vector<ll>> table;
ll dp(ll h, ll w) {
  // table.at(h).at(w)-=1;
  // 右移動
  if (movable(h, w - 1)) {
    if (grid.at(h).at(w - 1) == '.' && grid.at(h).at(w) == '#')
      chmin(table.at(h).at(w), dp(h, w - 1) + 1);
    else
      chmin(table.at(h).at(w), dp(h, w - 1));
  }
  // 下移動
  if (movable(h - 1, w)) {
    if (grid.at(h - 1).at(w) == '.' && grid.at(h).at(w) == '#')
      chmin(table.at(h).at(w), dp(h - 1, w) + 1);
    else
      chmin(table.at(h).at(w), dp(h - 1, w));
  }
  return table.at(h).at(w);
}
int main() {
  cin >> H >> W;
  grid = vector<string>(H);
  rep(i, H) { cin >> grid.at(i); }
  table = vector<vector<ll>>(H, vector<ll>(W, INF));
  if (grid.at(0).at(0) == '.')
    table.at(0).at(0) = 0;
  else
    table.at(0).at(0) = 1;
  cout << dp(H - 1, W - 1) << endl;
  return 0;
}


Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const ll MOD = 1000000007;
const long long INF = 1LL << 60;
const double pi = acos(-1.0);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

vector<string> grid;
ll H, W;

bool movable(ll sh, ll sw) {
  if (sh < 0 || sh >= H || sw < 0 || sw >= W)
    return false;
  return true;
}

vector<vector<ll>> table;
ll dp(ll h, ll w) {
  if (table.at(h).at(w) < INF)
    return table.at(h).at(w);
  table.at(h).at(w) -= 1;
  // 右移動
  if (movable(h, w - 1)) {
    if (grid.at(h).at(w - 1) == '.' && grid.at(h).at(w) == '#')
      chmin(table.at(h).at(w), dp(h, w - 1) + 1);
    else
      chmin(table.at(h).at(w), dp(h, w - 1));
  }
  // 下移動
  if (movable(h - 1, w)) {
    if (grid.at(h - 1).at(w) == '.' && grid.at(h).at(w) == '#')
      chmin(table.at(h).at(w), dp(h - 1, w) + 1);
    else
      chmin(table.at(h).at(w), dp(h - 1, w));
  }
  return table.at(h).at(w);
}
int main() {
  cin >> H >> W;
  grid = vector<string>(H);
  rep(i, H) { cin >> grid.at(i); }
  table = vector<vector<ll>>(H, vector<ll>(W, INF));
  if (grid.at(0).at(0) == '.')
    table.at(0).at(0) = 0;
  else
    table.at(0).at(0) = 1;
  cout << dp(H - 1, W - 1) << endl;
  return 0;
}
Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using maze = vector<string>;

const int NOT_VISITED = -1;

const char WALL = '#';
const char PATH = '.';

queue<pair<int, int>> q_path, q_wall;

int dh[] = {1, 0, -1, 0};
int dw[] = {0, 1, 0, -1};

void bfs(int &cnt, bool flip, maze &m, vector<vector<int>> &dist) {
  int h = m.size(), w = m.front().size();
  auto &q = !flip ? q_path : q_wall;
  auto &q_next = !flip ? q_wall : q_path;

  while (!q.empty()) {
    int ch = q.front().first, cw = q.front().second;
    q.pop();

    for (int k = 0; k < 2; ++k) {
      int nh = ch + dh[k], nw = cw + dw[k];
      if (nh < 0 || h <= nh || nw < 0 || w <= nw)
        continue;
      if ((!flip && m[nh][nw] == WALL) || (flip && m[nh][nw] == PATH)) {
        q_next.emplace(nh, nw);
        dist[nh][nw] = cnt + 1;
        continue;
      }

      if (dist[nh][nw] == NOT_VISITED) {
        q.emplace(nh, nw);
        dist[nh][nw] = cnt;
      }
    }
  }
  if (dist[h - 1][w - 1] == NOT_VISITED)
    ++cnt;
}

int main() {
  int h, w;
  cin >> h >> w;

  maze m(h);
  for (auto &x : m)
    cin >> x;

  int cnt = 0;
  bool flip = false;
  vector<vector<int>> dist(h, vector<int>(w, NOT_VISITED));

  if (m[0][0] == WALL) {
    cnt = 1;
    flip = true;
  }

  dist[0][0] = cnt;
  if (!flip)
    q_path.emplace(0, 0);
  else
    q_wall.emplace(0, 0);

  auto &goal = dist[h - 1][w - 1];

  while (goal == NOT_VISITED) {
    bfs(cnt, flip, m, dist);
    flip = !flip;
  }

  cout << (goal + 1) / 2 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using maze = vector<string>;

const int NOT_VISITED = -1;

const char WALL = '#';
const char PATH = '.';

queue<pair<int, int>> q_path, q_wall;

int dh[] = {1, 0, -1, 0};
int dw[] = {0, 1, 0, -1};

void bfs(int &cnt, bool flip, maze &m, vector<vector<int>> &dist) {
  int h = m.size(), w = m.front().size();
  auto &q = !flip ? q_path : q_wall;
  auto &q_next = !flip ? q_wall : q_path;

  while (!q.empty()) {
    int ch = q.front().first, cw = q.front().second;
    q.pop();

    for (int k = 0; k < 2; ++k) {
      int nh = ch + dh[k], nw = cw + dw[k];
      if (nh < 0 || h <= nh || nw < 0 || w <= nw)
        continue;
      if ((!flip && m[nh][nw] == WALL) || (flip && m[nh][nw] == PATH)) {
        if (dist[nh][nw] == NOT_VISITED) {
          q_next.emplace(nh, nw);
          dist[nh][nw] = cnt + 1;
        }
        continue;
      }

      if (dist[nh][nw] == NOT_VISITED) {
        q.emplace(nh, nw);
        dist[nh][nw] = cnt;
      }
    }
  }
  if (dist[h - 1][w - 1] == NOT_VISITED)
    ++cnt;
}

int main() {
  int h, w;
  cin >> h >> w;

  maze m(h);
  for (auto &x : m)
    cin >> x;

  int cnt = 0;
  bool flip = false;
  vector<vector<int>> dist(h, vector<int>(w, NOT_VISITED));

  if (m[0][0] == WALL) {
    cnt = 1;
    flip = true;
  }

  dist[0][0] = cnt;
  if (!flip)
    q_path.emplace(0, 0);
  else
    q_wall.emplace(0, 0);

  auto &goal = dist[h - 1][w - 1];

  while (goal == NOT_VISITED) {
    bfs(cnt, flip, m, dist);
    flip = !flip;
  }

  cout << (goal + 1) / 2 << endl;
}
Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
ID: espr1t
TASK:
KEYWORDS:
*/

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

using namespace std;
FILE *in = stdin;
FILE *out = stdout;

const int MAX = 101;
const int INF = 1000000001;

int n, m;
char a[MAX][MAX];
int dyn[MAX][MAX][2];

bool isOkay(int row, int col, int inv) {
  return inv ? a[row][col] == '#' : a[row][col] == '.';
}

int recurse(int row, int col, int inv) {
  if (row >= n)
    return INF;
  if (col >= m)
    return INF;
  if (row == n - 1 && col == m - 1)
    return isOkay(row, col, inv) ? 0 : !inv;

  int ans = INF, add = 0;
  int ninv = inv;
  if (!isOkay(row, col, inv))
    ninv = !inv, add = !inv;
  ans = min(ans, recurse(row + 1, col, ninv) + add);
  ans = min(ans, recurse(row, col + 1, ninv) + add);

  return dyn[row][col][inv] = ans;
}

int main(void) {
  // in = fopen("A.in", "rt");

  fscanf(in, "%d %d", &n, &m);
  for (int row = 0; row < n; row++)
    fscanf(in, "%s", a[row]);

  memset(dyn, -1, sizeof(dyn));
  fprintf(out, "%d\n", recurse(0, 0, 0));

  return 0;
}

Fixed submission:
/*
ID: espr1t
TASK:
KEYWORDS:
*/

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

using namespace std;
FILE *in = stdin;
FILE *out = stdout;

const int MAX = 101;
const int INF = 1000000001;

int n, m;
char a[MAX][MAX];
int dyn[MAX][MAX][2];

bool isOkay(int row, int col, int inv) {
  return inv ? a[row][col] == '#' : a[row][col] == '.';
}

int recurse(int row, int col, int inv) {
  if (row >= n)
    return INF;
  if (col >= m)
    return INF;
  if (row == n - 1 && col == m - 1)
    return isOkay(row, col, inv) ? 0 : !inv;
  if (dyn[row][col][inv] != -1)
    return dyn[row][col][inv];

  int ans = INF, add = 0;
  int ninv = inv;
  if (!isOkay(row, col, inv))
    ninv = !inv, add = !inv;
  ans = min(ans, recurse(row + 1, col, ninv) + add);
  ans = min(ans, recurse(row, col + 1, ninv) + add);

  return dyn[row][col][inv] = ans;
}

int main(void) {
  // in = fopen("A.in", "rt");

  fscanf(in, "%d %d", &n, &m);
  for (int row = 0; row < n; row++)
    fscanf(in, "%s", a[row]);

  memset(dyn, -1, sizeof(dyn));
  fprintf(out, "%d\n", recurse(0, 0, 0));

  return 0;
}
Problem p02735 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const int MD = (int)1e9 + 7;
typedef vector<ll> VL;
void dbg() { cerr << "\n"; }
template <class T, class... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}
template <class T, class T2> void amin(T &a, T2 b) {
  if (a > b)
    a = b;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int h, w;
  ;
  cin >> h >> w;
  vector<string> ba(h);
  rep(i, h) cin >> ba[i];
  vector<VI> dp(h + 2, VI(w + 2, MD));
  dp[0][0] = (ba[0][0] == '#');
  rep(i, h) rep(j, w) {
    amin(dp[i + 1][j], dp[i][j] + (ba[i][j] != ba[i + 1][j]));
    amin(dp[i][j + 1], dp[i][j] + (ba[i][j] != ba[i][j + 1]));
  }
  // for(auto xl: dp){for(auto x :xl) cerr<<x<<" ";dbg();}
  int ans = (dp[h - 1][w - 1] + 1) / 2;
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const int MD = (int)1e9 + 7;
typedef vector<ll> VL;
void dbg() { cerr << "\n"; }
template <class T, class... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}
template <class T, class T2> void amin(T &a, T2 b) {
  if (a > b)
    a = b;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int h, w;
  ;
  cin >> h >> w;
  vector<string> ba(h);
  rep(i, h) cin >> ba[i];
  vector<VI> dp(h + 2, VI(w + 2, MD));
  dp[0][0] = (ba[0][0] == '#');
  rep(i, h) rep(j, w) {
    if (i)
      amin(dp[i][j], dp[i - 1][j] + (ba[i][j] != ba[i - 1][j]));
    if (j)
      amin(dp[i][j], dp[i][j - 1] + (ba[i][j] != ba[i][j - 1]));
  }
  // for(auto xl: dp){for(auto x :xl) cerr<<x<<" ";dbg();}
  int ans = (dp[h - 1][w - 1] + 1) / 2;
  cout << ans << "\n";
  return 0;
}
Problem p02735 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, n, m) for (int i = m; i < (n); ++i)
using namespace std;
using ll = long long;

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char>> wh(h, vector<char>(w));
  vector<vector<int>> dp(h, vector<int>(w));
  rep(i, h) {
    rep(j, w) { cin >> wh.at(i).at(j); }
  }
  if (wh[0][0] == '.') {
    dp[0][0] = 0;
  } else {
    dp[0][0] = 1;
  }
  repp(k, w, 1) {
    if (wh[0][k] == '.') {
      dp[0][k] = dp[0][k - 1];
    } else if (wh[0][k] == '#' && wh[0][k - 1] == '.') {
      dp[0][k] = dp[0][k - 1] + 1;
    } else {
      dp[0][k] = dp[0][k - 1];
    }
  }
  repp(l, h, 1) {
    if (wh[l][0] == '.') {
      dp[l][0] = dp[l - 1][0];
    } else if (wh[l][0] == '#' && wh[l - 1][0] == '.') {
      dp[l][0] = dp[l - 1][0] + 1;
    } else {
      dp[l][0] = dp[l - 1][0];
    }
  }
  repp(a, w, 1) {
    repp(s, h, 1) {
      if (wh[a][s] == '.') {
        dp[a][s] = min(dp[a - 1][s], dp[a][s - 1]);
      } else {
        if (wh[a - 1][s] == '#' && wh[a][s - 1] == '#') {
          dp[a][s] = min(dp[a - 1][s], dp[a][s - 1]);
        } else if (wh[a - 1][s] == '#' && wh[a][s - 1] == '.') {
          dp[a][s] = min(dp[a - 1][s], dp[a][s - 1] + 1);
        } else if (wh[a - 1][s] == '.' && wh[a][s - 1] == '#') {
          dp[a][s] = min(dp[a - 1][s] + 1, dp[a][s - 1]);
        } else {
          dp[a][s] = min(dp[a - 1][s] + 1, dp[a][s - 1] + 1);
        }
      }
    }
  }
  cout << dp[h - 1][w - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, n, m) for (int i = m; i < (n); ++i)
using namespace std;
using ll = long long;

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char>> wh(h, vector<char>(w));
  vector<vector<int>> dp(h, vector<int>(w));
  rep(i, h) {
    rep(j, w) { cin >> wh.at(i).at(j); }
  }
  if (wh[0][0] == '.') {
    dp[0][0] = 0;
  } else {
    dp[0][0] = 1;
  }
  repp(k, w, 1) {
    if (wh[0][k] == '.') {
      dp[0][k] = dp[0][k - 1];
    } else if (wh[0][k] == '#' && wh[0][k - 1] == '.') {
      dp[0][k] = dp[0][k - 1] + 1;
    } else {
      dp[0][k] = dp[0][k - 1];
    }
  }
  repp(l, h, 1) {
    if (wh[l][0] == '.') {
      dp[l][0] = dp[l - 1][0];
    } else if (wh[l][0] == '#' && wh[l - 1][0] == '.') {
      dp[l][0] = dp[l - 1][0] + 1;
    } else {
      dp[l][0] = dp[l - 1][0];
    }
  }
  repp(a, h, 1) {
    repp(s, w, 1) {
      if (wh[a][s] == '.') {
        dp[a][s] = min(dp[a - 1][s], dp[a][s - 1]);
      } else {
        if (wh[a - 1][s] == '#' && wh[a][s - 1] == '#') {
          dp[a][s] = min(dp[a - 1][s], dp[a][s - 1]);
        } else if (wh[a - 1][s] == '#' && wh[a][s - 1] == '.') {
          dp[a][s] = min(dp[a - 1][s], dp[a][s - 1] + 1);
        } else if (wh[a - 1][s] == '.' && wh[a][s - 1] == '#') {
          dp[a][s] = min(dp[a - 1][s] + 1, dp[a][s - 1]);
        } else {
          dp[a][s] = min(dp[a - 1][s] + 1, dp[a][s - 1] + 1);
        }
      }
    }
  }
  cout << dp[h - 1][w - 1] << endl;
}
Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;
const ll dx[] = {1, 0};
const ll dy[] = {0, 1};
int main() {
  ll h, w;
  cin >> h >> w;
  vector<string> s(h);
  for (auto &&e : s) {
    cin >> e;
  }
  vector<vector<ll>> dp(h, vector<ll>(w, INF));
  dp[0][0] = s[0][0] == '#' ? 1 : 0;

  queue<pair<ll, ll>> nxt;
  nxt.push({0, 0});
  while (!nxt.empty()) {
    ll x = nxt.front().first;
    ll y = nxt.front().second;
    nxt.pop();
    for (int i = 0; i < 2; ++i) {
      ll nx = x + dx[i];
      ll ny = y + dy[i];
      if (nx >= h || ny >= w)
        continue;
      dp[nx][ny] = min(dp[nx][ny], dp[x][y] + ((s[nx][ny] == '#') ? 1 : 0));
      nxt.push({nx, ny});
    }
  }
  // for (int i = 0; i < h; ++i)
  // {
  //   for (int j = 0; j < w; ++j)
  //   {
  //     cout<<dp[i][j]<<" ";
  //   }
  //   cout<<endl;
  // }
  cout << dp[h - 1][w - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;
const ll dx[] = {1, 0};
const ll dy[] = {0, 1};
int main() {
  ll h, w;
  cin >> h >> w;
  vector<string> s(h);
  for (auto &&e : s) {
    cin >> e;
  }
  vector<vector<ll>> dp(h, vector<ll>(w, INF));
  dp[0][0] = s[0][0] == '#' ? 1 : 0;

  queue<pair<ll, ll>> nxt;
  nxt.push({0, 0});
  while (!nxt.empty()) {
    ll x = nxt.front().first;
    ll y = nxt.front().second;
    nxt.pop();
    for (int i = 0; i < 2; ++i) {
      ll nx = x + dx[i];
      ll ny = y + dy[i];
      if (nx >= h || ny >= w)
        continue;
      if (dp[nx][ny] >
          dp[x][y] + ((s[x][y] == '.' && s[nx][ny] == '#') ? 1 : 0)) {
        dp[nx][ny] = dp[x][y] + ((s[x][y] == '.' && s[nx][ny] == '#') ? 1 : 0);
        nxt.push({nx, ny});
      }
    }
  }
  // for (int i = 0; i < h; ++i)
  // {
  //   for (int j = 0; j < w; ++j)
  //   {
  //     cout<<dp[i][j]<<" ";
  //   }
  //   cout<<endl;
  // }
  cout << dp[h - 1][w - 1] << endl;
}
Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

bool CanGo(int y, int x, int R, int C) {
  return y >= 0 && y < R && x >= 0 && x < C;
}

int ToBlock(vector<vector<char>> &Map, int y1, int x1, int y2, int x2) {
  return Map[y1][x1] == '.' && Map[y2][x2] == '#' ? -1 : 0;
}

int TraceMaze(priority_queue<vector<int>> &Queue, int gy, int gx,
              vector<vector<char>> &Map) {
  vector<int> nowstate = Queue.top();
  Queue.pop();
  int R = Map.size();
  int C = Map[0].size();
  int step = nowstate[0];
  int ny = nowstate[1];
  int nx = nowstate[2];

  if (ny == gy && nx == gx)
    return -step;

  if (Map[ny][nx] == '#' || Map[ny][nx] == '.') {
    if (CanGo(ny + 1, nx, R, C))
      Queue.push({step + ToBlock(Map, ny, nx, ny + 1, nx), ny + 1, nx});
    if (CanGo(ny, nx + 1, R, C))
      Queue.push({step + ToBlock(Map, ny, nx, ny, nx + 1), ny, nx + 1});
  }
  return -1;
}

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> Map(H, vector<char>(W, 0));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> Map[i][j];
    }
  }
  int step = Map[0][0] == '#' ? -1 : 0;
  priority_queue<vector<int>> Queue;
  Queue.push({step, 0, 0});
  int ans = -1;
  while (!Queue.empty() && ans < 0)
    ans = TraceMaze(Queue, H - 1, W - 1, Map);
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

bool CanGo(int y, int x, int R, int C) {
  return y >= 0 && y < R && x >= 0 && x < C;
}

int ToBlock(vector<vector<char>> &Map, int y1, int x1, int y2, int x2) {
  return Map[y1][x1] == '.' && Map[y2][x2] == '#' ? -1 : 0;
}

int TraceMaze(priority_queue<vector<int>> &Queue, int gy, int gx,
              vector<vector<char>> &Map) {
  vector<int> nowstate = Queue.top();
  Queue.pop();
  int R = Map.size();
  int C = Map[0].size();
  int step = nowstate[0];
  int ny = nowstate[1];
  int nx = nowstate[2];

  if (ny == gy && nx == gx)
    return -step;

  if (Map[ny][nx] == '#' || Map[ny][nx] == '.') {
    if (CanGo(ny + 1, nx, R, C))
      Queue.push({step + ToBlock(Map, ny, nx, ny + 1, nx), ny + 1, nx});
    if (CanGo(ny, nx + 1, R, C))
      Queue.push({step + ToBlock(Map, ny, nx, ny, nx + 1), ny, nx + 1});
    Map[ny][nx] = ' ';
  }
  return -1;
}

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> Map(H, vector<char>(W, 0));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> Map[i][j];
    }
  }
  int step = Map[0][0] == '#' ? -1 : 0;
  priority_queue<vector<int>> Queue;
  Queue.push({step, 0, 0});
  int ans = -1;
  while (!Queue.empty() && ans < 0)
    ans = TraceMaze(Queue, H - 1, W - 1, Map);
  cout << ans << endl;
}
Problem p02735 (C++)

Buggy submission (status: Runtime Error):
// LightOj Pass : qavajxyq

#include <bits/stdc++.h>
#define deb(x) cerr << (#x) << " : " << x << "\n"
#define ll long long int
#define ld long double
using namespace std;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

ll powmod(ll a, ll b, ll mod = MOD) {
  ll res = 1ll;
  b %= (mod - 1ll), a %= mod;
  while (b) {
    if (b % 2 == 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("D:\\V S Code\\cpp\\competitiveProgramming\\Input.txt", "r", stdin);
  freopen("D:\\V S Code\\cpp\\competitiveProgramming\\OPT.txt", "w", stdout);
#endif

  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;
  ll dp[n + 1][m + 1];
  string arr[n];

  for (int i = 0; i < n; i++)
    cin >> arr[i];
  memset(dp, -1, sizeof(dp));

  dp[0][0] = arr[0][0] == '#';
  int cnt;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!i and !j)
        continue;
      dp[i][j] = INF;
      if (j) {
        if (arr[i][j - 1] == '.' and arr[i][j] == '#')
          cnt = 1;
        else
          cnt = 0;

        dp[i][j] = min(dp[i][j], dp[i][j - 1] + cnt);
      }
      if (i) {
        if (arr[i - 1][j] == '.' and arr[i][j] == '#')
          cnt = 1;
        else
          cnt = 0;

        dp[i][j] = min(dp[i][j], dp[i - 1][j] + cnt);
      }
    }
  }

  cout << dp[n - 1][m - 1];
}

Fixed submission:
// LightOj Pass : qavajxyq

#include <bits/stdc++.h>
#define deb(x) cerr << (#x) << " : " << x << "\n"
#define ll long long int
#define ld long double
using namespace std;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

ll powmod(ll a, ll b, ll mod = MOD) {
  ll res = 1ll;
  b %= (mod - 1ll), a %= mod;
  while (b) {
    if (b % 2 == 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;
  ll dp[n + 1][m + 1];
  string arr[n];

  for (int i = 0; i < n; i++)
    cin >> arr[i];
  memset(dp, -1, sizeof(dp));

  dp[0][0] = arr[0][0] == '#';
  int cnt;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!i and !j)
        continue;
      dp[i][j] = INF;
      if (j) {
        if (arr[i][j - 1] == '.' and arr[i][j] == '#')
          cnt = 1;
        else
          cnt = 0;

        dp[i][j] = min(dp[i][j], dp[i][j - 1] + cnt);
      }
      if (i) {
        if (arr[i - 1][j] == '.' and arr[i][j] == '#')
          cnt = 1;
        else
          cnt = 0;

        dp[i][j] = min(dp[i][j], dp[i - 1][j] + cnt);
      }
    }
  }

  cout << dp[n - 1][m - 1];
}
Problem p02735 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  vector<vector<int>> dp(h, vector<int>(h, 1e5));
  if (s[0][0] == '#')
    dp[0][0] = 1;
  else
    dp[0][0] = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (i + 1 < h && s[i + 1][j] == '#' && s[i][j] == '.')
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1);
      else if (i + 1 < h)
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (j + 1 < w && s[i][j + 1] == '#' && s[i][j] == '.')
        dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + 1);
      else if (j + 1 < w)
        dp[i][j + 1] = min(dp[i][j + 1], dp[i][j]);
    }
  }
  cout << dp[h - 1][w - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  vector<vector<int>> dp(h, vector<int>(w, 1e5));
  if (s[0][0] == '#')
    dp[0][0] = 1;
  else
    dp[0][0] = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (i + 1 < h && s[i + 1][j] == '#' && s[i][j] == '.')
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1);
      else if (i + 1 < h)
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (j + 1 < w && s[i][j + 1] == '#' && s[i][j] == '.')
        dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + 1);
      else if (j + 1 < w)
        dp[i][j + 1] = min(dp[i][j + 1], dp[i][j]);
    }
  }
  cout << dp[h - 1][w - 1] << endl;
}
Problem p02735 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> s(H);
  for (int i = 0; i < H; i++)
    cin >> s[i];

  vector<vector<int>> dp(H, vector<int>(W));

  if (s[0][0] == '#')
    dp[0][0] = 1;
  else
    dp[0][0] = 0;

  int ans;
  for (int i = 0; i < W; i++) {
    for (int j = 0; j < H; j++) {
      ans = 20000;
      if (i != 0) {
        if (s[i - 1][j] == '.' && s[i][j] == '#')
          ans = dp[i - 1][j] + 1;
        else
          ans = dp[i - 1][j];
      }
      if (j != 0) {
        if (s[i][j - 1] == '.' && s[i][j] == '#')
          ans = min(dp[i][j - 1] + 1, ans);
        else
          ans = min(dp[i][j - 1], ans);
      }

      if (i != 0 || j != 0) {
        dp[i][j] = ans;
      }
    }
  }

  cout << dp[H - 1][W - 1] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> s(H);
  for (int i = 0; i < H; i++)
    cin >> s[i];

  vector<vector<int>> dp(H, vector<int>(W));

  if (s[0][0] == '#')
    dp[0][0] = 1;
  else
    dp[0][0] = 0;

  int ans;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      ans = 20000;
      if (i != 0) {
        if (s[i - 1][j] == '.' && s[i][j] == '#')
          ans = dp[i - 1][j] + 1;
        else
          ans = dp[i - 1][j];
      }
      if (j != 0) {
        if (s[i][j - 1] == '.' && s[i][j] == '#')
          ans = min(dp[i][j - 1] + 1, ans);
        else
          ans = min(dp[i][j - 1], ans);
      }

      if (i != 0 || j != 0) {
        dp[i][j] = ans;
      }
    }
  }

  cout << dp[H - 1][W - 1] << endl;

  return 0;
}
Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using P = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
typedef tuple<int, int, int> T;

void solve() {
  int h, w;
  cin >> h >> w;
  vvi m(h, vi(w));
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) if (s[j] == '#') m[i][j] = 1;
  }
  dumpv(m, h, w);
  deque<T> q;
  vvi used(h, vi(w, -1));
  if (m[0][0]) {
    q.push_back(T(0, 0, 1));
  } else {
    q.push_back(T(0, 0, 0));
  }
  while (!q.empty()) {
    int i, j, c;
    tie(i, j, c) = q.front();
    q.pop_front();
    if (used[i][j] != -1 && used[i][j] < c)
      continue;
    used[i][j] = c;
    if (i < h - 1) {
      if (m[i + 1][j] == m[i][j]) {
        q.push_back(T(i + 1, j, c));
      } else {
        q.push_back(T(i + 1, j, c + 1));
      }
    }
    if (j < w - 1) {
      if (m[i][j + 1] == m[i][j]) {
        q.push_back(T(i, j + 1, c));
      } else {
        q.push_back(T(i, j + 1, c + 1));
      }
    }
    // dumpv(used, h, w);
  }
  cout << (used[h - 1][w - 1] + 1) / 2 << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using P = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
typedef tuple<int, int, int> T;

void solve() {
  int h, w;
  cin >> h >> w;
  vvi m(h, vi(w));
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) if (s[j] == '#') m[i][j] = 1;
  }
  dumpv(m, h, w);
  deque<T> q;
  vvi used(h, vi(w, -1));
  if (m[0][0]) {
    q.push_back(T(0, 0, 1));
  } else {
    q.push_back(T(0, 0, 0));
  }
  while (!q.empty()) {
    int i, j, c;
    tie(i, j, c) = q.front();
    q.pop_front();
    if (used[i][j] != -1 && used[i][j] <= c)
      continue;
    used[i][j] = c;
    if (i < h - 1) {
      if (m[i + 1][j] == m[i][j]) {
        q.push_back(T(i + 1, j, c));
      } else {
        q.push_back(T(i + 1, j, c + 1));
      }
    }
    if (j < w - 1) {
      if (m[i][j + 1] == m[i][j]) {
        q.push_back(T(i, j + 1, c));
      } else {
        q.push_back(T(i, j + 1, c + 1));
      }
    }
    // dumpv(used, h, w);
  }
  cout << (used[h - 1][w - 1] + 1) / 2 << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}
Problem p02735 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define endl '\n'

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int h, w;
  cin >> h >> w;

  vector<string> board(h);

  for (int i = 0; i < h; ++i)
    cin >> board[i];

  vector<vector<int>> dist(h, vector<int>(w, h * w + 1));
  dist[0][0] = 0;

  deque<pair<int, int>> q;
  q.push_back({0, 0});

  while (!q.empty()) {
    auto c = q.front();
    q.pop_front();

    int x = c.first, y = c.second, d = dist[x][y];

    for (int k = 0; k < 4; ++k) {
      int nx = x + dx[k], ny = y + dy[k];

      if (0 <= nx && nx < h && 0 <= ny && ny < w) {
        if (board[x][y] == board[nx][ny]) {
          if (dist[nx][ny] > d) {
            dist[nx][ny] = d;
            q.push_front({nx, ny});
          }
        } else {
          if (dist[nx][ny] > d + 1) {
            dist[nx][ny] = d + 1;
            q.push_back({nx, ny});
          }
        }
      }
    }
  }

  cout << (dist[h - 1][w - 1] + (board[0][0] == '#') +
           (board[h - 1][w - 1] == '#') + 1) /
              2
       << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define endl '\n'

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int h, w;
  cin >> h >> w;

  vector<string> board(h);

  for (int i = 0; i < h; ++i)
    cin >> board[i];

  vector<vector<int>> dist(h, vector<int>(w, h * w + 1));
  dist[0][0] = 0;

  deque<pair<int, int>> q;
  q.push_back({0, 0});

  while (!q.empty()) {
    auto c = q.front();
    q.pop_front();

    int x = c.first, y = c.second, d = dist[x][y];

    for (int k = 0; k < 2; ++k) {
      int nx = x + dx[k], ny = y + dy[k];

      if (0 <= nx && nx < h && 0 <= ny && ny < w) {
        if (board[x][y] == board[nx][ny]) {
          if (dist[nx][ny] > d) {
            dist[nx][ny] = d;
            q.push_front({nx, ny});
          }
        } else {
          if (dist[nx][ny] > d + 1) {
            dist[nx][ny] = d + 1;
            q.push_back({nx, ny});
          }
        }
      }
    }
  }

  cout << (dist[h - 1][w - 1] + (board[0][0] == '#') +
           (board[h - 1][w - 1] == '#') + 1) /
              2
       << endl;

  return 0;
}
Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

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

char s[101][101];
int d[101][101]; // d[i][j] := (i,j)に行くのにかかる最小のコスト

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  rep(i, 101) rep(j, 101) d[i][j] = 1e9 + 7;
  d[0][0] = 0;
  int h, w;
  cin >> h >> w;

  rep(i, h) rep(j, w) cin >> s[i][j];

  queue<pair<int, int>> q;
  q.push({0, 0});
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    // 取り出した点が黒だったら黒の間移動しまくる
    for (int i = 0; i < 2; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx >= h || ny >= w)
        continue;
      if (s[x][y] == '.') {
        if (s[nx][ny] == '#') {
          if (d[nx][ny] > d[x][y] + 1) {
            d[nx][ny] = d[x][y] + 1;
            q.push({nx, ny});
          }
        } else {
          if (d[nx][ny] < d[x][y])
            continue;
          d[nx][ny] = d[x][y];
          q.push({nx, ny});
        }
      } else {
        if (d[nx][ny] > d[x][y]) {
          d[nx][ny] = d[x][y];
          q.push({nx, ny});
        }
      }
    }
  }
  cout << d[h - 1][w - 1] + (s[0][0] == '#') << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

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

char s[101][101];
int d[101][101]; // d[i][j] := (i,j)に行くのにかかる最小のコスト

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  rep(i, 101) rep(j, 101) d[i][j] = 1e9 + 7;
  d[0][0] = 0;
  int h, w;
  cin >> h >> w;

  rep(i, h) rep(j, w) cin >> s[i][j];

  queue<pair<int, int>> q;
  q.push({0, 0});
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    // 取り出した点が黒だったら黒の間移動しまくる
    for (int i = 0; i < 2; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx >= h || ny >= w)
        continue;
      if (s[x][y] == '.') {
        if (s[nx][ny] == '#') {
          if (d[nx][ny] > d[x][y] + 1) {
            d[nx][ny] = d[x][y] + 1;
            q.push({nx, ny});
          }
        } else {
          if (d[nx][ny] > d[x][y]) {
            d[nx][ny] = d[x][y];
            q.push({nx, ny});
          }
        }
      } else {
        if (d[nx][ny] > d[x][y]) {
          d[nx][ny] = d[x][y];
          q.push({nx, ny});
        }
      }
    }
  }
  cout << d[h - 1][w - 1] + (s[0][0] == '#') << endl;
}
Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back

int n, m;
char s[105][105];
int f[105][105][205];
bool vis[105][105][205];

int F(int x, int y, int i) {
  if (x >= n || y >= m)
    return 1e9;
  int &ans = f[x][y][i];
  if (vis[x][y][i])
    return ans;
  ans = 1e9;
  int cur = 0;
  if (s[x][y] ^ i) {
    cur = 1;
    i++;
  }
  if (x == n - 1 && y == m - 1) {
    ans = cur;
    return ans;
  }
  for (int ni = 0; ni <= i; ++ni) {
    ans = min(ans, F(x + 1, y, ni));
    ans = min(ans, F(x, y + 1, ni));
  }
  ans += cur;
  // printf("F(%d %d %d) = %d\n", x, y, i, ans);
  return ans;
}

int main() {
  ios ::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
    for (int j = 0; j < m; ++j) {
      if (s[i][j] == '#')
        s[i][j] = 1;
      else
        s[i][j] = 0;
    }
  }
  cout << F(0, 0, 0) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back

int n, m;
char s[105][105];
int f[105][105][205];
bool vis[105][105][205];

int F(int x, int y, int i) {
  if (x >= n || y >= m)
    return 1e9;
  int &ans = f[x][y][i];
  if (vis[x][y][i])
    return ans;
  vis[x][y][i] = 1;
  ans = 1e9;
  int cur = 0;
  if (s[x][y] ^ i) {
    cur = 1;
    i++;
  }
  if (x == n - 1 && y == m - 1) {
    ans = cur;
    return ans;
  }
  for (int ni = 0; ni <= i; ++ni) {
    ans = min(ans, F(x + 1, y, ni));
    ans = min(ans, F(x, y + 1, ni));
  }
  ans += cur;
  // printf("F(%d %d %d) = %d\n", x, y, i, ans);
  return ans;
}

int main() {
  ios ::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
    for (int j = 0; j < m; ++j) {
      if (s[i][j] == '#')
        s[i][j] = 1;
      else
        s[i][j] = 0;
    }
  }
  cout << F(0, 0, 0) << endl;
  return 0;
}
Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000000;
constexpr ll INF = 1e18;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int, int> P;

int H, W;
vector<vector<char>> vec(110, vector<char>(110));
vector<vector<int>> ret(110, vector<int>(110, -1));
vector<int> dy = {0, 1};
vector<int> dx = {1, 0};

void dfs() {
  if (vec.at(0).at(0) == '#') {
    ret.at(0).at(0) = 1;
  } else {
    ret.at(0).at(0) = 0;
  }
  queue<P> que;
  que.push(P(0, 0));
  while (!(que.empty())) {
    P p = que.front();
    que.pop();
    for (int i = 0; i < 2; i++) {
      int ny = p.first + dy[i];
      int nx = p.second + dx[i];
      if (ny < H && nx < W) {
        int cnt = ret[p.first][p.second];
        if (vec[p.first][p.second] == '.' && vec[ny][nx] == '#') {
          cnt++;
        }
        if (cnt <= ret[ny][nx] || ret[ny][nx] == -1) {
          que.push(P(ny, nx));
          ret[ny][nx] = cnt;
        }
      }
    }
  }
}

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> vec.at(i).at(j);
    }
  }
  dfs();
  cout << ret[H - 1][W - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000000;
constexpr ll INF = 1e18;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int, int> P;

int H, W;
vector<vector<char>> vec(110, vector<char>(110));
vector<vector<int>> ret(110, vector<int>(110, -1));
vector<int> dy = {0, 1};
vector<int> dx = {1, 0};

void dfs() {
  if (vec.at(0).at(0) == '#') {
    ret.at(0).at(0) = 1;
  } else {
    ret.at(0).at(0) = 0;
  }
  queue<P> que;
  que.push(P(0, 0));
  while (!(que.empty())) {
    P p = que.front();
    que.pop();
    for (int i = 0; i < 2; i++) {
      int ny = p.first + dy[i];
      int nx = p.second + dx[i];
      if (ny < H && nx < W) {
        int cnt = ret[p.first][p.second];
        if (vec[p.first][p.second] == '.' && vec[ny][nx] == '#') {
          cnt++;
        }
        if (cnt < ret[ny][nx] || ret[ny][nx] == -1) {
          que.push(P(ny, nx));
          ret[ny][nx] = cnt;
        }
      }
    }
  }
}

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> vec.at(i).at(j);
    }
  }
  dfs();
  cout << ret[H - 1][W - 1] << endl;
}
Problem p02735 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  int H, W;
  int i, j;

  cin >> H >> W;

  vector<vector<int>> dp(H, vector<int>(W, 0));

  vector<string> s(H);

  for (auto &a : s)
    cin >> a;

  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      if (i == 0 && j == 0) {
        dp[i][j] = (s[i][j] == '#');
      } else if (i == 0) {
        dp[i][j] = dp[i][j - 1] + (s[i][j] == '#' && s[i][j - 1] != '#');
      } else if (j == 0) {
        dp[i][j] = dp[i - 1][j] + (s[i][j] == '#' && s[i - 1][j] != '#');
      } else {
        dp[i][j] = min(dp[i][j - 1] + (s[i][j] == '#' && s[i][j - 1] != '#'),
                       dp[i - 1][j] + (s[i][j] == '#' && s[i - 1][j] != '#'));
      }
    }
  }

  cout << dp[H][W] << endl;

  return 0;
}

Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  int H, W;
  int i, j;

  cin >> H >> W;

  vector<vector<int>> dp(H, vector<int>(W, 0));

  vector<string> s(H);

  for (auto &a : s)
    cin >> a;

  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      if (i == 0 && j == 0) {
        dp[i][j] = (s[i][j] == '#');
      } else if (i == 0) {
        dp[i][j] = dp[i][j - 1] + (s[i][j] == '#' && s[i][j - 1] != '#');
      } else if (j == 0) {
        dp[i][j] = dp[i - 1][j] + (s[i][j] == '#' && s[i - 1][j] != '#');
      } else {
        dp[i][j] = min(dp[i][j - 1] + (s[i][j] == '#' && s[i][j - 1] != '#'),
                       dp[i - 1][j] + (s[i][j] == '#' && s[i - 1][j] != '#'));
      }
    }
  }

  cout << dp[H - 1][W - 1] << endl;

  return 0;
}
Problem p02735 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

char a[105][105];

int d[105][105];

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

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }

  for (int i = 0; i < 105; i++) {
    for (int j = 0; j < 105; j++) {
      d[i][j] = numeric_limits<int>::max();
    }
  }

  deque<pair<int, int>> q;
  q.push_back({1, 1});
  d[1][1] = 0;
  while (!q.empty()) {
    int y, x;
    y = q.front().first;
    x = q.front().second;
    // auto [y, x] = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int yy = y + dy[i];
      int xx = x + dx[i];
      if (yy < 1 || xx < 1 || yy > n || xx > m)
        continue;
      int dist = d[y][x] + (a[y][x] == a[yy][xx] ? 0 : 1);
      if (dist < d[yy][xx]) {
        if (a[yy][xx] == a[y][x])
          q.push_front({yy, xx});
        else
          q.push_back({yy, xx});
        d[yy][xx] = dist;
      }
    }
  }
  cout << d[n][m] / 2 + (a[1][1] == '#' || a[n][m] == '#') << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

char a[105][105];

int d[105][105];

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

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }

  for (int i = 0; i < 105; i++) {
    for (int j = 0; j < 105; j++) {
      d[i][j] = numeric_limits<int>::max();
    }
  }

  deque<pair<int, int>> q;
  q.push_back({1, 1});
  d[1][1] = 0;
  while (!q.empty()) {
    int y, x;
    y = q.front().first;
    x = q.front().second;
    // auto [y, x] = q.front();
    q.pop_front();
    for (int i = 0; i < 2; i++) {
      int yy = y + dy[i];
      int xx = x + dx[i];
      if (yy < 1 || xx < 1 || yy > n || xx > m)
        continue;
      int dist = d[y][x] + (a[y][x] == a[yy][xx] ? 0 : 1);
      if (dist < d[yy][xx]) {
        if (a[yy][xx] == a[y][x])
          q.push_front({yy, xx});
        else
          q.push_back({yy, xx});
        d[yy][xx] = dist;
      }
    }
  }
  cout << d[n][m] / 2 + (a[1][1] == '#' || a[n][m] == '#') << endl;
}
Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const string ln = "\n";
constexpr int INF = 1001001001;
constexpr int MOD = 1000000007;

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int h, w;
  cin >> h >> w;
  vector<string> g(h);
  for (int i = 0; i < h; i++) {
    cin >> g[i];
  }

  queue<P> q;
  q.push(P(0, 0));

  int d[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = INF;
    }
  }

  d[0][0] = g[0][0] == '.' ? 0 : 1;

  while (!q.empty()) {
    P v = q.front();
    q.pop();

    int y = v.first;
    int x = v.second;

    for (int i = 0; i < 2; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];

      if (nx < h && ny < w) {
        int next = d[y][x] + (g[ny][nx] == '#' && g[y][x] == '.' ? 1 : 0);
        d[ny][nx] = min(next, d[ny][nx]);
        q.push(P(ny, nx));
      }
    }
  }

  cout << d[h - 1][w - 1] << ln;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const string ln = "\n";
constexpr int INF = 1001001001;
constexpr int MOD = 1000000007;

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int h, w;
  cin >> h >> w;
  vector<string> g(h);
  for (int i = 0; i < h; i++) {
    cin >> g[i];
  }

  queue<P> q;
  q.push(P(0, 0));

  int d[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = INF;
    }
  }

  d[0][0] = g[0][0] == '.' ? 0 : 1;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      for (int i = 0; i < 2; i++) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if (ny < h && nx < w) {
          int next = d[y][x] + (g[ny][nx] == '#' && g[y][x] == '.' ? 1 : 0);
          d[ny][nx] = min(next, d[ny][nx]);
        }
      }
    }
  }

  cout << d[h - 1][w - 1] << ln;

  return 0;
}