Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
using namespace std;
struct masu {
  int y, x;
};

int h, w, check[50][50];
int X[] = {-1, 0, 1, 0};
int Y[] = {0, -1, 0, 1};
char s[50][50];

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s[i][j];
    }
  }

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

  // cout << "a" << endl;
  deque<masu> que;
  que.push_back(masu{0, 0});
  while (!que.empty()) {
    masu m = que.front();
    que.pop_front();
    // cout << "y:" << m.y << "x:" << m.x << endl;
    for (int i = 0; i < 4; i++) {
      int y = m.y + Y[i];
      int x = m.x + X[i];
      // cout << "y:" << y << " x:" << x << endl;
      if (0 <= y && y < h && 0 <= x && x < w && check[y][x] == INT32_MAX &&
          s[y][x] == '.') {
        check[y][x] = check[m.y][m.x] + 1;
        que.push_back(masu{y, x});
      }
    }
  }

  // cout << "b" << endl;
  int y = h - 1, x = w - 1;
  while (true) {
    s[y][x] = '#';
    if (y == 0 && x == 0)
      break;

    for (int i = 0; i < 4; i++) {
      int y2 = y + Y[i];
      int x2 = x + X[i];
      if (0 <= y2 && y2 < h && 0 <= x2 && x2 < w &&
          check[y2][x2] == check[y][x] - 1) {
        y = y2;
        x = x2;
        break;
      }
    }
  }
  // cout << "c" << endl;

  int ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '.')
        ans++;
    }
  }

  cout << (s[0][0] == '.' ? -1 : ans) << endl;
}

Fixed submission:
#include <iostream>
#include <queue>
using namespace std;
struct masu {
  int y, x;
};

int h, w, check[50][50];
int X[] = {-1, 0, 1, 0};
int Y[] = {0, -1, 0, 1};
char s[50][50];

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s[i][j];
    }
  }

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

  // cout << "a" << endl;
  deque<masu> que;
  que.push_back(masu{0, 0});
  while (!que.empty()) {
    masu m = que.front();
    que.pop_front();
    // cout << "y:" << m.y << "x:" << m.x << endl;
    for (int i = 0; i < 4; i++) {
      int y = m.y + Y[i];
      int x = m.x + X[i];
      // cout << "y:" << y << " x:" << x << endl;
      if (0 <= y && y < h && 0 <= x && x < w && check[y][x] == INT32_MAX &&
          s[y][x] == '.') {
        check[y][x] = check[m.y][m.x] + 1;
        que.push_back(masu{y, x});
      }
    }
  }

  // cout << "b" << endl;
  int y = h - 1, x = w - 1;
  while (true) {
    s[y][x] = '#';
    if (y == 0 && x == 0 || check[y][x] == INT32_MAX)
      break;

    for (int i = 0; i < 4; i++) {
      int y2 = y + Y[i];
      int x2 = x + X[i];
      if (0 <= y2 && y2 < h && 0 <= x2 && x2 < w &&
          check[y2][x2] == check[y][x] - 1) {
        y = y2;
        x = x2;
        break;
      }
    }
  }
  // cout << "c" << endl;

  int ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '.')
        ans++;
    }
  }

  cout << (s[0][0] == '.' ? -1 : ans) << endl;
}