Problem p02735 (C++)

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

typedef long long ll;
typedef pair<ll, pair<ll, ll>> ipair;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define ABS(x) ((x) < 0 ? -(x) : (x))
#define f(i, n) for (int i = 0; i < n; i++)
#define F(i, a, b) for (int i = a; i <= b; i++)

//-------------------------CODE BEGINS HERE-------------------------------//
// clock_t tStart = clock();

ll mod = 998244353;

ll po(ll b, ll p) {
  if (p == 0)
    return 1LL;
  if (p == 1)
    return b;
  ll t = po(b, p / 2);
  t = (t * t) % mod;
  return p % 2 ? (t * b) % mod : t;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);

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

  char mat[h][w];

  f(i, h) {
    f(j, w) { cin >> mat[i][j]; }
  }

  int dp[h][w];

  dp[0][0] = 0;

  F(i, 1, h - 1) {
    if (mat[i][0] != mat[i - 1][0])
      dp[i][0] = dp[i - 1][0] + 1;
    else
      dp[i][0] = dp[i - 1][0];
  }

  F(i, 1, w - 1) {
    if (mat[0][i] != mat[0][i - 1])
      dp[0][i] = dp[0][i - 1] + 1;
    else
      dp[0][i] = dp[0][i - 1];
  }

  F(i, 1, h - 1) {
    F(j, 1, w - 1) {
      int p = INT_MAX;
      if (mat[i][j - 1] == mat[i][j]) {
        p = MIN(p, dp[i][j - 1]);
      } else {
        p = MIN(p, dp[i][j - 1] + 1);
      }
      if (mat[i - 1][j] == mat[i][j]) {
        p = MIN(p, dp[i - 1][j]);
      } else {
        p = MIN(p, dp[i - 1][j] + 1);
      }
      dp[i][j] = p;
    }
  }

  // f(i,h){
  // 	f(j,w){
  // 		cout<<dp[i][j]<<" ";
  // 	}
  // 	cout<<endl;
  // }

  if (mat[0][0] == '.') {
    cout << (dp[h - 1][w - 1] + 1) / 2 << endl;
  } else {
    cout << (dp[h - 1][w - 1]) / 2 + 1 << endl;
  }

  // printf("Time taken: %.2fs\n",(double)(clock()-tStart)/CLOCKS_PER_SEC);

  return 0;
}

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

typedef long long ll;
typedef pair<ll, pair<ll, ll>> ipair;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define ABS(x) ((x) < 0 ? -(x) : (x))
#define f(i, n) for (int i = 0; i < n; i++)
#define F(i, a, b) for (int i = a; i <= b; i++)

//-------------------------CODE BEGINS HERE-------------------------------//
// clock_t tStart = clock();

ll mod = 998244353;

ll po(ll b, ll p) {
  if (p == 0)
    return 1LL;
  if (p == 1)
    return b;
  ll t = po(b, p / 2);
  t = (t * t) % mod;
  return p % 2 ? (t * b) % mod : t;
}

int main() {

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

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

  char mat[h][w];

  f(i, h) {
    f(j, w) { cin >> mat[i][j]; }
  }

  int dp[h][w];

  dp[0][0] = 0;

  F(i, 1, h - 1) {
    if (mat[i][0] != mat[i - 1][0])
      dp[i][0] = dp[i - 1][0] + 1;
    else
      dp[i][0] = dp[i - 1][0];
  }

  F(i, 1, w - 1) {
    if (mat[0][i] != mat[0][i - 1])
      dp[0][i] = dp[0][i - 1] + 1;
    else
      dp[0][i] = dp[0][i - 1];
  }

  F(i, 1, h - 1) {
    F(j, 1, w - 1) {
      int p = INT_MAX;
      if (mat[i][j - 1] == mat[i][j]) {
        p = MIN(p, dp[i][j - 1]);
      } else {
        p = MIN(p, dp[i][j - 1] + 1);
      }
      if (mat[i - 1][j] == mat[i][j]) {
        p = MIN(p, dp[i - 1][j]);
      } else {
        p = MIN(p, dp[i - 1][j] + 1);
      }
      dp[i][j] = p;
    }
  }

  // f(i,h){
  // 	f(j,w){
  // 		cout<<dp[i][j]<<" ";
  // 	}
  // 	cout<<endl;
  // }

  if (mat[0][0] == '.') {
    cout << (dp[h - 1][w - 1] + 1) / 2 << endl;
  } else {
    cout << (dp[h - 1][w - 1]) / 2 + 1 << endl;
  }

  // printf("Time taken: %.2fs\n",(double)(clock()-tStart)/CLOCKS_PER_SEC);

  return 0;
}