Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

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

  int H, W;
  char tmp;

  scanf("%d %d", &H, &W);
  scanf("%c", &tmp);

  vector<vector<int>> S(H, vector<int>(W));
  vector<vector<pair<int, bool>>> d(
      H, vector<pair<int, bool>>(W, make_pair(H + W + 100, false)));

  int count = 0;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf("%c", &tmp);
      if (tmp == '.') {
        S[i][j] = 1;
        // d[i][j].second = false;
        count++;
      } else {
        S[i][j] = 0;
        d[i][j].second = true; // check不要
      }
    }
    scanf("%c", &tmp);
  }
  // printf("count = %d\n", count);

  pair<int, int> check_pos = make_pair(0, 0);
  d[0][0].first = 1;
  int d_pl;
  while (true) {
    // printf("check (%d,%d)\n", check_pos.first, check_pos.second);
    d[check_pos.first][check_pos.second].second = true;
    if (check_pos.first == H - 1 && check_pos.second == W - 1) {
      break;
    }
    d_pl = d[check_pos.first][check_pos.second].first + 1;
    for (auto i : {make_pair(check_pos.first, check_pos.second - 1),
                   make_pair(check_pos.first, check_pos.second + 1),
                   make_pair(check_pos.first - 1, check_pos.second),
                   make_pair(check_pos.first + 1, check_pos.second)}) {
      // printf("(%d, %d) check\n", i.first, i.second);
      if (i.first < 0 || i.first >= H || i.second < 0 || i.second >= W) {
        // printf("out of range\n");
        continue;
      }
      if (d[i.first][i.second].second == true) {
        // printf("already check\n");
        continue;
      }
      // printf("min(%d,%d)\n", d[i.first][i.second].first, d_pl);
      d[i.first][i.second].first = min(d[i.first][i.second].first, d_pl);
    }

    int min_d = H + W + 100;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        // printf("(%d, %d)\n", i, j);
        if (d[i][j].second == true) {
          // printf("(%d, %d) true\n", i, j);
          continue;
        }
        if (d[i][j].first < min_d) {
          min_d = d[i][j].first;
          check_pos = make_pair(i, j);
          // printf("change (%d,%d)\n", check_pos.first,
          //    check_pos.second);
        }
      }
    }
  }

  // printf("retult = %d\n", d[H - 1][W - 1].first);

  printf("%d", count - d[H - 1][W - 1].first);

  return 0;
}

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

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

  int H, W;
  char tmp;

  scanf("%d %d", &H, &W);
  scanf("%c", &tmp);

  vector<vector<int>> S(H, vector<int>(W));
  vector<vector<pair<int, bool>>> d(
      H, vector<pair<int, bool>>(W, make_pair(H + W + 100, false)));

  int count = 0;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf("%c", &tmp);
      if (tmp == '.') {
        S[i][j] = 1;
        // d[i][j].second = false;
        count++;
      } else {
        S[i][j] = 0;
        d[i][j].second = true; // check不要
      }
    }
    scanf("%c", &tmp);
  }
  // printf("count = %d\n", count);

  pair<int, int> check_pos = make_pair(0, 0);
  d[0][0].first = 1;
  int d_pl;
  while (true) {
    // printf("check (%d,%d)\n", check_pos.first, check_pos.second);
    d[check_pos.first][check_pos.second].second = true;
    if (check_pos.first == H - 1 && check_pos.second == W - 1) {
      break;
    }
    d_pl = d[check_pos.first][check_pos.second].first + 1;
    for (auto i : {make_pair(check_pos.first, check_pos.second - 1),
                   make_pair(check_pos.first, check_pos.second + 1),
                   make_pair(check_pos.first - 1, check_pos.second),
                   make_pair(check_pos.first + 1, check_pos.second)}) {
      // printf("(%d, %d) check\n", i.first, i.second);
      if (i.first < 0 || i.first >= H || i.second < 0 || i.second >= W) {
        // printf("out of range\n");
        continue;
      }
      if (d[i.first][i.second].second == true) {
        // printf("already check\n");
        continue;
      }
      // printf("min(%d,%d)\n", d[i.first][i.second].first, d_pl);
      d[i.first][i.second].first = min(d[i.first][i.second].first, d_pl);
    }

    int min_d = H + W + 100;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        // printf("(%d, %d)\n", i, j);
        if (d[i][j].second == true) {
          // printf("(%d, %d) true\n", i, j);
          continue;
        }
        if (d[i][j].first < min_d) {
          min_d = d[i][j].first;
          check_pos = make_pair(i, j);
          // printf("change (%d,%d)\n", check_pos.first,
          //    check_pos.second);
        }
      }
    }
    if (min_d == H + W + 100) {
      printf("-1");
      return 0;
    }
  }

  // printf("retult = %d\n", d[H - 1][W - 1].first);

  printf("%d", count - d[H - 1][W - 1].first);

  return 0;
}