Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  int i, j;
  for (i = 0; i < h; i++)
    cin >> s[i];
  int ans = 1 << 30;
  int tmp[h][w];
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      tmp[i][j] = 1 << 30;
    }
  }
  queue<pair<pair<int, int>, pair<int, char>>> que;
  if (s[0][0] == '#') {
    que.push(make_pair(make_pair(0, 0), make_pair(1, '#')));
  } else {
    que.push(make_pair(make_pair(0, 0), make_pair(0, '.')));
  }
  while (!que.empty()) {
    int change = que.front().second.first;
    char color = que.front().second.second;
    int y = que.front().first.first;
    int x = que.front().first.second;
    char opp;
    bool chk = true;
    if (tmp[y][x] < change)
      chk = false;
    else {
      tmp[y][x] = change;
    }
    if (color == '#')
      opp = '.';
    else
      opp = '#';
    que.pop();
    int aa = 0;
    if (color == '.')
      aa = 1;
    if (x == w - 1 && y == h - 1) {
      if (ans > change)
        ans = change;
    } else if (change < ans && chk) {
      if (x < w - 1) {
        if (s[y][x + 1] == color)
          que.push(make_pair(make_pair(y, x + 1), make_pair(change, color)));
        else
          que.push(make_pair(make_pair(y, x + 1), make_pair(change + aa, opp)));
      }
      if (y < h - 1) {
        if (s[y + 1][x] == color)
          que.push(make_pair(make_pair(y + 1, x), make_pair(change, color)));
        else
          que.push(make_pair(make_pair(y + 1, x), make_pair(change + aa, opp)));
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  int i, j;
  for (i = 0; i < h; i++)
    cin >> s[i];
  int ans = 1 << 30;
  int tmp[h][w];
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      tmp[i][j] = 1 << 30;
    }
  }
  queue<pair<pair<int, int>, pair<int, char>>> que;
  if (s[0][0] == '#') {
    que.push(make_pair(make_pair(0, 0), make_pair(1, '#')));
  } else {
    que.push(make_pair(make_pair(0, 0), make_pair(0, '.')));
  }
  while (!que.empty()) {
    int change = que.front().second.first;
    char color = que.front().second.second;
    int y = que.front().first.first;
    int x = que.front().first.second;
    char opp;
    bool chk = true;
    if (tmp[y][x] <= change)
      chk = false;
    else {
      tmp[y][x] = change;
    }
    if (color == '#')
      opp = '.';
    else
      opp = '#';
    que.pop();
    int aa = 0;
    if (color == '.')
      aa = 1;
    if (x == w - 1 && y == h - 1) {
      if (ans > change)
        ans = change;
    } else if (change < ans && chk) {
      if (x < w - 1) {
        if (s[y][x + 1] == color)
          que.push(make_pair(make_pair(y, x + 1), make_pair(change, color)));
        else
          que.push(make_pair(make_pair(y, x + 1), make_pair(change + aa, opp)));
      }
      if (y < h - 1) {
        if (s[y + 1][x] == color)
          que.push(make_pair(make_pair(y + 1, x), make_pair(change, color)));
        else
          que.push(make_pair(make_pair(y + 1, x), make_pair(change + aa, opp)));
      }
    }
  }
  cout << ans << endl;
}