Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

struct state {
  int y, x, step;
};
static const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main() {
  int h, w;
  scanf("%d %d", &h, &w);
  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  vector<vector<int>> used(h, vector<int>(w, -1));
  int sy = 0, sx = 0;
  queue<state> q;
  q.push({sy, sx, 0});
  used[sy][sx] = 0;
  while (!q.empty()) {
    state p = q.front();
    q.pop();
    for (int d = 0; d < 4; d++) {
      int xx = p.x + dx[d], yy = p.y + dy[d];
      if (xx < 0 || xx >= w || yy < 0 || yy >= h)
        continue;
      if (used[yy][xx] != -1 || s[yy][xx] == '#')
        continue;
      used[yy][xx] = p.step + 1;
      q.push({yy, xx, p.step + 1});
    }
  }
  /*
  for (int i = 0; i < h; i ++) {
          for (int j = 0; j < w; j ++) {
                  cerr << used[i][j] << " ";
          }
          cerr << endl;
  }
  */
  int cury = h - 1, curx = w - 1;
  vector<vector<bool>> pass(h, vector<bool>(w, false));
  while (true) {
    pass[cury][curx] = true;
    if (cury == 0 && curx == 0)
      break;
    for (int d = 0; d < 4; d++) {
      int xx = curx + dx[d], yy = cury + dy[d];
      if (xx < 0 || xx >= w || yy < 0 || yy >= h)
        continue;
      if (used[yy][xx] == used[cury][curx] - 1) {
        cury = yy, curx = xx;
        break;
      }
    }
  }
  /*
  for (int i = 0; i < h; i ++) {
          for (int j = 0; j < w; j ++) {
                  cerr << pass[i][j] << " ";
          }
          cerr << endl;
  }
  */
  int ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (!pass[i][j] && s[i][j] != '#') {
        ans++;
      }
    }
  }
  printf("%d", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

struct state {
  int y, x, step;
};
static const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main() {
  int h, w;
  scanf("%d %d", &h, &w);
  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  vector<vector<int>> used(h, vector<int>(w, -1));
  int sy = 0, sx = 0;
  queue<state> q;
  q.push({sy, sx, 0});
  used[sy][sx] = 0;
  while (!q.empty()) {
    state p = q.front();
    q.pop();
    for (int d = 0; d < 4; d++) {
      int xx = p.x + dx[d], yy = p.y + dy[d];
      if (xx < 0 || xx >= w || yy < 0 || yy >= h)
        continue;
      if (used[yy][xx] != -1 || s[yy][xx] == '#')
        continue;
      used[yy][xx] = p.step + 1;
      q.push({yy, xx, p.step + 1});
    }
  }
  if (used[h - 1][w - 1] == -1) {
    printf("%d\n", -1);
    return 0;
  }
  /*
  for (int i = 0; i < h; i ++) {
          for (int j = 0; j < w; j ++) {
                  cerr << used[i][j] << " ";
          }
          cerr << endl;
  }
  */
  int cury = h - 1, curx = w - 1;
  vector<vector<bool>> pass(h, vector<bool>(w, false));
  while (true) {
    pass[cury][curx] = true;
    if (cury == 0 && curx == 0)
      break;
    for (int d = 0; d < 4; d++) {
      int xx = curx + dx[d], yy = cury + dy[d];
      if (xx < 0 || xx >= w || yy < 0 || yy >= h)
        continue;
      if (used[yy][xx] == used[cury][curx] - 1) {
        cury = yy, curx = xx;
        break;
      }
    }
  }
  /*
  for (int i = 0; i < h; i ++) {
          for (int j = 0; j < w; j ++) {
                  cerr << pass[i][j] << " ";
          }
          cerr << endl;
  }
  */
  int ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (!pass[i][j] && s[i][j] != '#') {
        ans++;
      }
    }
  }
  printf("%d", ans);
  return 0;
}