Problem p02735 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define eps 1e-8
#define zero(x) (((x) > 0 ? (x) : -(x)) < eps)

#define pause cout << " press ansy key to continue...", cin >> chh
#define file_r(x) freopen(x, "r", stdin)
#define file_w(x) freopen(x, "w", stdout)
#define lowbit(x) ((x) & (-x))
#define repit(i, c)                                                            \
  for (__typeof__((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repe(i, u) for (int i = head[u]; i != -1; i = nxt[i])
#define repd(i, n) for (int i = (n - 1); i >= 0; i--)
#define FOR(i, n, m) for (int i = (n); i <= (m); i++)
#define FORD(i, n, m) for (int i = (n); i >= (m); i--)
#define pb push_back
#define X first
#define Y second
#define ins insert
#define rb rbegin
#define be begin
#define er erase
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define SZ(c) (c).size()
#define ALL(c) (c).begin(), (c).end()
#define sqr(r) ((LL)(r) * (r))
#define dis(x1, y1, x2, y2)                                                    \
  (((x1) - (x2)) * ((x1) - (x2)) + ((y1) - (y2)) * ((y1) - (y2)))
#define FASTIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)

#define sc(x) cout << #x " = " << x << endl, system("pause")
#define sc2(x, y)                                                              \
  cout << #x " = " << x << " " << #y " = " << y << endl, system("pause")
#define sc3(x, y, z)                                                           \
  cout << #x " = " << x << " " << #y " = " << y << " " << #z " = " << z        \
       << endl,                                                                \
      system("pause")
#define sc4(x, y, z, w)                                                        \
  cout << #x " = " << x << " " << #y " = " << y << " " << #z " = " << z << " " \
       << #w " = " << w << endl,                                               \
      system("pause")

#define in(n) scanf("%d", &n)
#define in2(n, m) scanf("%d %d", &n, &m)
#define in3(x, y, z) scanf("%d %d %d", &x, &y, &z)

using namespace std;
int chh;

typedef vector<int> vi;
typedef set<int> si;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<int, pii> pi3;
typedef vector<pair<int, int>> vpii;
typedef long long LL;

const int N = 105;

int n, m;
int dp[N][N], f[N][N];
char g[N][N];
int dx[2] = {0, 1};
int dy[2] = {1, 0};

void solve() {
  queue<pii> q;
  memset(dp, 127, sizeof(dp));
  memset(f, 0, sizeof(f));
  dp[0][0] = (g[0][0] != '.');
  q.push(mp(0, 0));
  while (!q.empty()) {
    int x = q.front().X, y = q.front().Y;
    q.pop(), f[x][y] = 0;
    rep(k, 4) {
      int u = x + dx[k], v = y + dy[k];
      if (u < 0 || u == n || v < 0 || v == m)
        continue;
      int w = dp[x][y] + (g[x][y] == '.' && g[u][v] == '#');
      if (dp[u][v] > w) {
        dp[u][v] = w;
        if (!f[u][v])
          f[u][v] = 1, q.push(mp(u, v));
      }
    }
  }
}

int main() {
  while (~in2(n, m)) {
    rep(i, n) scanf("%s", g[i]);
    solve();
    printf("%d\n", dp[n - 1][m - 1]);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define eps 1e-8
#define zero(x) (((x) > 0 ? (x) : -(x)) < eps)

#define pause cout << " press ansy key to continue...", cin >> chh
#define file_r(x) freopen(x, "r", stdin)
#define file_w(x) freopen(x, "w", stdout)
#define lowbit(x) ((x) & (-x))
#define repit(i, c)                                                            \
  for (__typeof__((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repe(i, u) for (int i = head[u]; i != -1; i = nxt[i])
#define repd(i, n) for (int i = (n - 1); i >= 0; i--)
#define FOR(i, n, m) for (int i = (n); i <= (m); i++)
#define FORD(i, n, m) for (int i = (n); i >= (m); i--)
#define pb push_back
#define X first
#define Y second
#define ins insert
#define rb rbegin
#define be begin
#define er erase
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define SZ(c) (c).size()
#define ALL(c) (c).begin(), (c).end()
#define sqr(r) ((LL)(r) * (r))
#define dis(x1, y1, x2, y2)                                                    \
  (((x1) - (x2)) * ((x1) - (x2)) + ((y1) - (y2)) * ((y1) - (y2)))
#define FASTIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)

#define sc(x) cout << #x " = " << x << endl, system("pause")
#define sc2(x, y)                                                              \
  cout << #x " = " << x << " " << #y " = " << y << endl, system("pause")
#define sc3(x, y, z)                                                           \
  cout << #x " = " << x << " " << #y " = " << y << " " << #z " = " << z        \
       << endl,                                                                \
      system("pause")
#define sc4(x, y, z, w)                                                        \
  cout << #x " = " << x << " " << #y " = " << y << " " << #z " = " << z << " " \
       << #w " = " << w << endl,                                               \
      system("pause")

#define in(n) scanf("%d", &n)
#define in2(n, m) scanf("%d %d", &n, &m)
#define in3(x, y, z) scanf("%d %d %d", &x, &y, &z)

using namespace std;
int chh;

typedef vector<int> vi;
typedef set<int> si;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<int, pii> pi3;
typedef vector<pair<int, int>> vpii;
typedef long long LL;

const int N = 105;

int n, m;
int dp[N][N], f[N][N];
char g[N][N];
int dx[2] = {0, 1};
int dy[2] = {1, 0};

void solve() {
  queue<pii> q;
  memset(dp, 127, sizeof(dp));
  memset(f, 0, sizeof(f));
  dp[0][0] = (g[0][0] != '.');
  q.push(mp(0, 0));
  while (!q.empty()) {
    int x = q.front().X, y = q.front().Y;
    q.pop(), f[x][y] = 0;
    rep(k, 2) {
      int u = x + dx[k], v = y + dy[k];
      if (u < 0 || u == n || v < 0 || v == m)
        continue;
      int w = dp[x][y] + (g[x][y] == '.' && g[u][v] == '#');
      if (dp[u][v] > w) {
        dp[u][v] = w;
        if (!f[u][v])
          f[u][v] = 1, q.push(mp(u, v));
      }
    }
  }
}

int main() {
  while (~in2(n, m)) {
    rep(i, n) scanf("%s", g[i]);
    solve();
    printf("%d\n", dp[n - 1][m - 1]);
  }
  return 0;
}