Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vel;
typedef vector<int> vei;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<int>> ve_vei;
typedef vector<vector<char>> ve_vec;
typedef vector<vector<string>> ve_ves;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define fs first
#define sc second
#define pub push_back
#define pob pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) max_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)

char maze[105][105];
int N, M;
int sx, sy; // start
int gx, gy; // goal

int d[105][105]; // 各pointまでの最短距離

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

// startが大量にある場合
// https://atcoder.jp/contests/qupc2018/tasks/qupc2018_c

int bfs() {
  queue<i_i> que;
  rep(i, N) rep(j, M) d[i][j] = INF; // 初期化
  que.push(i_i(sx, sy));             // que->start
  d[sx][sy] = 0;                     // startは距離0

  // queが空になるまでloop
  while (que.size()) {
    // queの先頭を取りだす
    i_i p = que.front();
    que.pop();
    // getがgoalならend
    if (p.fs == gx and p.sc == gy)
      break;

    rep(i, 2) {
      int nx = p.fs + dx[i];
      int ny = p.sc + dy[i];
      if (0 <= nx and nx < N and 0 <= ny and ny < M) {
        if (d[p.fs][p.sc] > d[nx][ny])
          continue;
        if (maze[p.fs][p.sc] == '#' and maze[nx][ny] == '.') {
          if (d[p.fs][p.sc] < d[nx][ny]) {
            d[nx][ny] = d[p.fs][p.sc];
            que.push(i_i(nx, ny));
          } else
            continue;
        } else if (maze[p.fs][p.sc] == '.' and maze[nx][ny] == '#') {
          if (d[p.fs][p.sc] + 1 < d[nx][ny]) {
            d[nx][ny] = d[p.fs][p.sc] + 1;
            que.push(i_i(nx, ny));
          } else
            continue;
        } else {
          d[nx][ny] = d[p.fs][p.sc];
          que.push(i_i(nx, ny));
        }
        // cout << nx << " " << ny << " " << d[nx][ny] << endl;
      }
    }
  }
  return d[gx][gy];
}

// AGC033A

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

  cin >> N >> M;
  rep(i, N) rep(j, M) { cin >> maze[i][j]; }
  sx = 0;
  sy = 0;
  gx = N - 1;
  gy = M - 1;
  int ans = bfs();
  if (maze[0][0] == '#')
    ans++;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vel;
typedef vector<int> vei;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<int>> ve_vei;
typedef vector<vector<char>> ve_vec;
typedef vector<vector<string>> ve_ves;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define fs first
#define sc second
#define pub push_back
#define pob pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) max_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)

char maze[105][105];
int N, M;
int sx, sy; // start
int gx, gy; // goal

int d[105][105]; // 各pointまでの最短距離

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

// startが大量にある場合
// https://atcoder.jp/contests/qupc2018/tasks/qupc2018_c

int bfs() {
  queue<i_i> que;
  rep(i, N) rep(j, M) d[i][j] = INF; // 初期化
  que.push(i_i(sx, sy));             // que->start
  d[sx][sy] = 0;                     // startは距離0

  // queが空になるまでloop
  while (que.size()) {
    // queの先頭を取りだす
    i_i p = que.front();
    que.pop();
    // getがgoalならend
    if (p.fs == gx and p.sc == gy)
      break;

    rep(i, 2) {
      int nx = p.fs + dx[i];
      int ny = p.sc + dy[i];
      if (0 <= nx and nx < N and 0 <= ny and ny < M) {
        if (d[p.fs][p.sc] >= d[nx][ny])
          continue;
        if (maze[p.fs][p.sc] == '#' and maze[nx][ny] == '.') {
          if (d[p.fs][p.sc] < d[nx][ny]) {
            d[nx][ny] = d[p.fs][p.sc];
            que.push(i_i(nx, ny));
          } else
            continue;
        } else if (maze[p.fs][p.sc] == '.' and maze[nx][ny] == '#') {
          if (d[p.fs][p.sc] + 1 < d[nx][ny]) {
            d[nx][ny] = d[p.fs][p.sc] + 1;
            que.push(i_i(nx, ny));
          } else
            continue;
        } else {
          d[nx][ny] = d[p.fs][p.sc];
          que.push(i_i(nx, ny));
        }
        // cout << nx << " " << ny << " " << d[nx][ny] << endl;
      }
    }
  }
  return d[gx][gy];
}

// AGC033A

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

  cin >> N >> M;
  rep(i, N) rep(j, M) { cin >> maze[i][j]; }
  sx = 0;
  sy = 0;
  gx = N - 1;
  gy = M - 1;
  int ans = bfs();
  if (maze[0][0] == '#')
    ans++;
  cout << ans << endl;
  return 0;
}