prithivMLmods/Coder-Stat huggingface dataset:
Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define INF 10000
typedef long long ll;
int main() {
  int h, w;
  int d_cnt = 0;

  cin >> h >> w;
  int map[52][52] = {};
  typedef pair<int, int> P;
  queue<P> Q;
  int x[4] = {1, 0, -1, 0};
  int y[4] = {0, 1, 0, -1};
  char sub;

  rep(i, h) {
    rep(j, w) {
      cin >> sub;
      if (sub == '.') {
        d_cnt++;
        map[i][j] = -1;
      } else {
        map[i][j] = INF;
      }
    }
  }

  Q.push(make_pair(0, 0));
  map[0][0] = 1;
  while (!Q.empty()) {
    P a = Q.front();
    Q.pop();

    rep(i, 4) {
      int x_sub = a.first + x[i];
      int y_sub = a.second + y[i];
      if (-1 == map[x_sub][y_sub]) {
        Q.push(P(x_sub, y_sub));
        map[x_sub][y_sub] = map[a.first][a.second] + 1;
        if (x_sub == h - 1 && y_sub == w - 1)
          break;
      }
    }
  }
  /*
          rep(i,h){
                  rep(j,w){
                          cout << map[i][j] << " ";
                  }
                  cout << endl;
          }
  */
  int mahi = map[h - 1][w - 1];
  cout << (mahi == -1 ? -1 : (d_cnt - mahi)) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define INF 10000
typedef long long ll;
int main() {
  int h, w;
  int d_cnt = 0;

  cin >> h >> w;
  int map[52][52] = {};
  typedef pair<int, int> P;
  queue<P> Q;
  int x[4] = {1, 0, -1, 0};
  int y[4] = {0, 1, 0, -1};
  char sub;

  rep(i, h) {
    rep(j, w) {
      cin >> sub;
      if (sub == '.') {
        d_cnt++;
        map[i][j] = -1;
      } else {
        map[i][j] = INF;
      }
    }
  }

  Q.push(make_pair(0, 0));
  map[0][0] = 1;
  while (!Q.empty()) {
    P a = Q.front();
    Q.pop();

    rep(i, 4) {
      int x_sub = a.first + x[i];
      int y_sub = a.second + y[i];
      if (x_sub >= 0 && x_sub < h && y_sub >= 0 && y_sub < w) {
        if (-1 == map[x_sub][y_sub]) {
          Q.push(P(x_sub, y_sub));
          map[x_sub][y_sub] = map[a.first][a.second] + 1;
          if (x_sub == h - 1 && y_sub == w - 1)
            break;
        }
      }
    }
  }
  /*
          rep(i,h){
                  rep(j,w){
                          cout << map[i][j] << " ";
                  }
                  cout << endl;
          }
  */
  int mahi = map[h - 1][w - 1];
  cout << (mahi == -1 ? -1 : (d_cnt - mahi)) << endl;
  return 0;
}
Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int main() {
  int h = 0, w = 0;
  cin >> h >> w;
  vector<string> field(h);
  int num_white = 0;
  for (int i = 0; i < h; i++) {
    cin >> field.at(i);
    num_white += count(field.at(i).begin(), field.at(i).end(), '.');
  }
  queue<pair<int, int>> que;
  que.push(make_pair(0, 0));
  vector<vector<int>> dis(h, vector<int>(w, 1e8));
  dis.at(0).at(0) = 1;
  int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
  while (!que.empty()) {
    auto cur = que.front();
    que.pop();
    if (cur.first == h - 1 && cur.second == w - 1) {
      break;
    }
    int x = cur.first, y = cur.second;
    for (int i = 0; i < 4; i++) {
      int next_x = x + dx[i], next_y = y + dy[i];
      if (next_x < 0 || next_x >= h || next_y < 0 || next_y >= w)
        continue;
      if (field.at(next_x).at(next_y) == '#')
        continue;
      if (dis.at(next_x).at(next_y) < dis.at(x).at(y))
        continue;
      dis.at(next_x).at(next_y) = dis.at(x).at(y) + 1;
      que.push(make_pair(next_x, next_y));
    }
  }
  if (dis.at(h - 1).at(w - 1) == 1e8) {
    cout << -1 << endl;
  } else {
    cout << num_white - dis.at(h - 1).at(w - 1) << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int main() {
  int h = 0, w = 0;
  cin >> h >> w;
  vector<string> field(h);
  int num_white = 0;
  for (int i = 0; i < h; i++) {
    cin >> field.at(i);
    num_white += count(field.at(i).begin(), field.at(i).end(), '.');
  }
  queue<pair<int, int>> que;
  que.push(make_pair(0, 0));
  vector<vector<int>> dis(h, vector<int>(w, 1e8));
  dis.at(0).at(0) = 1;
  int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
  while (!que.empty()) {
    auto cur = que.front();
    que.pop();
    if (cur.first == h - 1 && cur.second == w - 1) {
      break;
    }
    int x = cur.first, y = cur.second;
    for (int i = 0; i < 4; i++) {
      int next_x = x + dx[i], next_y = y + dy[i];
      if (next_x < 0 || next_x >= h || next_y < 0 || next_y >= w)
        continue;
      if (field.at(next_x).at(next_y) == '#')
        continue;
      if (dis.at(next_x).at(next_y) <= dis.at(x).at(y) + 1)
        continue;
      dis.at(next_x).at(next_y) = dis.at(x).at(y) + 1;
      que.push(make_pair(next_x, next_y));
    }
  }
  if (dis.at(h - 1).at(w - 1) == 1e8) {
    cout << -1 << endl;
  } else {
    cout << num_white - dis.at(h - 1).at(w - 1) << endl;
  }
  return 0;
}
Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <string>
using namespace std;

const int INF = 1e+9;

int main() {

  int H, W;
  string s[50];

  cin >> H >> W;
  for (int i = 0; i < H; i++)
    cin >> s[i];

  int white = 0; // 最初にwhiteな面の数
  int dx[] = {0, 1, 0, -1};
  int dy[] = {1, 0, -1, 0};
  int d[50][50];

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      d[i][j] = INF;
      if (s[i][j] == '.')
        white++;
    }
  }
  d[0][0] = 1;

  queue<pair<int, int>> q;
  q.push(make_pair(0, 0));

  while (!q.empty()) { // queueに何か存在する間は実行
    pair<int, int> p = q.front();
    q.pop();

    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];

      if (0 <= nx && nx < W && 0 <= ny && ny < H) {
        if (s[ny][nx] == '.' && d[ny][nx] > d[p.second][p.first]) {
          d[ny][nx] = d[p.second][p.first] + 1;
          q.push(make_pair(nx, ny));
        }
      }
    }
  }

  if (d[H - 1][W - 1] == INF)
    cout << -1 << endl;
  else
    cout << white - d[H - 1][W - 1] << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <string>
using namespace std;

const int INF = 1e+9;

int main() {

  int H, W;
  string s[50];

  cin >> H >> W;
  for (int i = 0; i < H; i++)
    cin >> s[i];

  int white = 0; // 最初にwhiteな面の数
  int dx[] = {0, 1, 0, -1};
  int dy[] = {1, 0, -1, 0};
  int d[50][50];

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      d[i][j] = INF;
      if (s[i][j] == '.')
        white++;
    }
  }
  d[0][0] = 1;

  queue<pair<int, int>> q;
  q.push(make_pair(0, 0));

  while (!q.empty()) { // queueに何か存在する間は実行
    pair<int, int> p = q.front();
    q.pop();

    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];

      if (0 <= nx && nx < W && 0 <= ny && ny < H) {
        if (s[ny][nx] == '.' && d[ny][nx] > d[p.second][p.first] + 1) {
          d[ny][nx] = d[p.second][p.first] + 1;
          q.push(make_pair(nx, ny));
        }
      }
    }
  }

  if (d[H - 1][W - 1] == INF)
    cout << -1 << endl;
  else
    cout << white - d[H - 1][W - 1] << endl;

  return 0;
}
Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int h, w;
string arr[50];

int main() {
  cin >> h >> w;
  // memset(marked,0,sizeof(marked));
  for (int i = 0; i < h; i++)
    cin >> arr[i];
  vector<int> dist[h * w];
  int d[h * w];
  memset(d, 0, sizeof(d));
  int marked[h * w];
  memset(marked, 0, sizeof(marked));
  int c = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (arr[i][j] == '.') {
        c++;
        if (j < w - 1 && arr[i][j + 1] == '.') {
          dist[i * w + j].push_back(i * w + j + 1);
        }
        if (j > 0 && arr[i][j - 1] == '.') {
          dist[i * w + j].push_back(i * w + j - 1);
        }
        if (i > 0 && arr[i - 1][j] == '.') {
          dist[i * w + j].push_back((i - 1) * w + j);
        }
        if (i < h - 1 && arr[i + 1][j] == '.') {
          dist[i * w + j].push_back((i + 1) * w + j);
        }
      }
    }
  }
  queue<int> bfs;
  bfs.push(0);
  // int pl=0;
  while (!bfs.empty()) {
    int x = bfs.front();
    bfs.pop();
    marked[x] = 1;

    // pl++;
    for (auto z : dist[x]) {
      if (marked[z] == 0) {
        d[z] = d[x] + 1;
        bfs.push(z);
      }
    }
  }
  if (d[(h - 1) * w + (w - 1)] != 0)
    cout << c - d[(h - 1) * w + (w - 1)] - 1 << "\n";
  else
    cout << -1 << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int h, w;
string arr[50];

int main() {
  cin >> h >> w;
  // memset(marked,0,sizeof(marked));
  for (int i = 0; i < h; i++)
    cin >> arr[i];
  vector<int> dist[h * w];
  int d[h * w];
  memset(d, 0, sizeof(d));
  int marked[h * w];
  memset(marked, 0, sizeof(marked));
  int c = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (arr[i][j] == '.') {
        c++;
        if (j < w - 1 && arr[i][j + 1] == '.') {
          dist[i * w + j].push_back(i * w + j + 1);
        }
        if (j > 0 && arr[i][j - 1] == '.') {
          dist[i * w + j].push_back(i * w + j - 1);
        }
        if (i > 0 && arr[i - 1][j] == '.') {
          dist[i * w + j].push_back((i - 1) * w + j);
        }
        if (i < h - 1 && arr[i + 1][j] == '.') {
          dist[i * w + j].push_back((i + 1) * w + j);
        }
      }
    }
  }
  queue<int> bfs;
  bfs.push(0);
  // int pl=0;
  while (!bfs.empty()) {
    int x = bfs.front();
    bfs.pop();
    marked[x] = 1;

    // pl++;
    for (auto z : dist[x]) {
      if (marked[z] == 0) {
        marked[z] = 1;
        d[z] = d[x] + 1;
        bfs.push(z);
      }
    }
  }
  if (d[(h - 1) * w + (w - 1)] != 0)
    cout << c - d[(h - 1) * w + (w - 1)] - 1 << "\n";
  else
    cout << -1 << "\n";
}
Problem p03436 (C++)

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

using namespace std;

#define mp make_pair

char grid[55][55];
int red, stup;
int crni = 0;

int bfs() {
  bool bio[55][55];
  memset(bio, 0, sizeof(bio));
  queue<pair<int, int>> q;
  queue<int> qh;
  q.push(mp(0, 0));
  qh.push(1);
  while (!q.empty()) {
    pair<int, int> node = q.front();
    int cnt = qh.front();
    q.pop();
    qh.pop();
    int x = node.first;
    int y = node.second;
    if (x == red - 1 && y == stup - 1)
      return cnt;
    for (int i = 0; i < 4; i++) {
      int dx[] = {1, 0, -1, 0};
      int dy[] = {0, 1, 0, -1};
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (grid[nx][ny] == '#' || bio[nx][ny] || nx < 0 || ny < 0 || nx >= red ||
          ny >= stup)
        continue;
      q.push(mp(nx, ny));
      qh.push(cnt + 1);
    }
  }
  return -1;
}

int main() {
  cin >> red >> stup;
  for (int i = 0; i < red; i++) {
    for (int j = 0; j < stup; j++) {
      cin >> grid[i][j];
      if (grid[i][j] == '#')
        crni++;
    }
  }
  int put = bfs();
  if (put == -1) {
    cout << -1 << endl;
    return 0;
  } else {
    cout << red * stup - crni - put << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define mp make_pair

char grid[55][55];
int red, stup;
int crni = 0;

int bfs() {
  bool bio[55][55];
  memset(bio, 0, sizeof(bio));
  queue<pair<int, int>> q;
  queue<int> qh;
  q.push(mp(0, 0));
  qh.push(1);
  while (!q.empty()) {
    pair<int, int> node = q.front();
    int cnt = qh.front();
    q.pop();
    qh.pop();
    int x = node.first;
    int y = node.second;
    if (x == red - 1 && y == stup - 1)
      return cnt;
    for (int i = 0; i < 4; i++) {
      int dx[] = {1, 0, -1, 0};
      int dy[] = {0, 1, 0, -1};
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (grid[nx][ny] == '#' || bio[nx][ny] || nx < 0 || ny < 0 || nx >= red ||
          ny >= stup)
        continue;
      q.push(mp(nx, ny));
      qh.push(cnt + 1);
      bio[nx][ny] = 1;
    }
  }
  return -1;
}

int main() {
  cin >> red >> stup;
  for (int i = 0; i < red; i++) {
    for (int j = 0; j < stup; j++) {
      cin >> grid[i][j];
      if (grid[i][j] == '#')
        crni++;
    }
  }
  int put = bfs();
  if (put == -1) {
    cout << -1 << endl;
    return 0;
  } else {
    cout << red * stup - crni - put << endl;
  }
}
Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main(int argc, const char *argv[]) {
  int h, w;
  cin >> h >> w;

  vector<vector<char>> s(h, vector<char>(w));
  int cnt = 0;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> s[i][j];
      if (s[i][j] == '#') {
        cnt++;
      }
    }
  }

  vector<vector<int>> costs(h, vector<int>(w, -1));
  queue<array<int, 3>> q;
  q.push({0, 0, 0});
  while (!q.empty()) {
    auto [i, j, cost] = q.front();
    q.pop();
    if (costs[i][j] != -1 || s[i][j] == '#' || i < 0 || i > h - 1 || j < 0 ||
        j > w - 1) {
      continue;
    }

    costs[i][j] = cost;
    q.push({i - 1, j, cost + 1});
    q.push({i + 1, j, cost + 1});
    q.push({i, j - 1, cost + 1});
    q.push({i, j + 1, cost + 1});
  }

  int cost = costs[h - 1][w - 1];
  if (cost < 0) {
    cout << -1 << '\n';
  } else {
    cout << h * w - (cost + 1) - cnt << '\n';
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main(int argc, const char *argv[]) {
  int h, w;
  cin >> h >> w;

  vector<vector<char>> s(h, vector<char>(w));
  int cnt = 0;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> s[i][j];
      if (s[i][j] == '#') {
        cnt++;
      }
    }
  }

  vector<vector<int>> costs(h, vector<int>(w, -1));
  queue<array<int, 3>> q;
  q.push({0, 0, 0});
  while (!q.empty()) {
    auto [i, j, cost] = q.front();
    q.pop();
    if (i < 0 || i > h - 1 || j < 0 || j > w - 1 || costs[i][j] != -1 ||
        s[i][j] == '#') {
      continue;
    }

    costs[i][j] = cost;
    q.push({i - 1, j, cost + 1});
    q.push({i + 1, j, cost + 1});
    q.push({i, j - 1, cost + 1});
    q.push({i, j + 1, cost + 1});
  }

  int cost = costs[h - 1][w - 1];
  if (cost < 0) {
    cout << -1 << '\n';
  } else {
    cout << h * w - (cost + 1) - cnt << '\n';
  }

  return 0;
}
Problem p03436 (C++)

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

using namespace std;
char a[55][55];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool v[55][55];
struct s {
  int x, y, d;
};
int main() {
  int n, m;
  cin >> n >> m;
  int bl = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    for (int j = 0; j < m; j++) {
      if (a[i][j] == '.')
        bl++;
    }
  }
  queue
 q;
  s s1;
  s1.x = 0;
  s1.y = 0;
  s1.d = 1;
  q.push(s1);
  int w = 0;
  while (!q.empty()) {
    s t = q.front();
    q.pop();
    v[t.x][t.y] = 1;

    if (t.x == n - 1 && t.y == m - 1) {

      w = t.d;
      break;
    }
    for (int i = 0; i < 4; i++) {
      int nx = dx[i] + t.x;
      int ny = dy[i] + t.y;
      if (nx < 0 || nx >= n || ny < 0 || ny >= m || v[nx][ny] ||
          a[nx][ny] == '#')
        continue;
      s t1;
      t1.x = nx;
      t1.y = ny;
      t1.d = t.d + 1;
      q.push(t1);
    }
  }
  int ans = bl - w;
  if (w != 0)
    cout << ans;
  else
    cout << -1;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
char a[55][55];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool v[55][55];
struct s {
  int x, y, d;
};
int main() {
  int n, m;
  cin >> n >> m;
  int bl = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    for (int j = 0; j < m; j++) {
      if (a[i][j] == '.')
        bl++;
    }
  }
  queue
 q;
  s s1;
  s1.x = 0;
  s1.y = 0;
  s1.d = 1;
  q.push(s1);
  int w = 0;
  while (!q.empty()) {
    s t = q.front();
    q.pop();
    v[t.x][t.y] = 1;

    if (t.x == n - 1 && t.y == m - 1) {

      w = t.d;
      break;
    }
    for (int i = 0; i < 4; i++) {
      int nx = dx[i] + t.x;
      int ny = dy[i] + t.y;
      if (nx < 0 || nx >= n || ny < 0 || ny >= m || v[nx][ny] ||
          a[nx][ny] == '#')
        continue;
      s t1;
      v[nx][ny] = 1;
      t1.x = nx;
      t1.y = ny;
      t1.d = t.d + 1;
      q.push(t1);
    }
  }
  int ans = bl - w;
  if (w != 0)
    cout << ans;
  else
    cout << -1;
  return 0;
}
Problem p03436 (C++)

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

#define foreach(i, v)                                                          \
  for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)

using namespace std;
typedef long long ll;

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

int dist[60][60];
int h, w;
int sy, sx;
std::vector<string> mp(60);

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  int h, w;
  cin >> h >> w;

  int white = 0;
  for (int y = 0; y < h; y++) {
    cin >> mp[y];
    white += count(mp[y].begin(), mp[y].end(), '.');
  }

  queue<int> q;
  q.push(0);
  q.push(0);
  dist[0][0] = 1;
  while (q.size()) {
    int y = q.front();
    q.pop();
    int x = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (!(0 <= ny && ny < h && 0 <= nx && nx < w))
        continue;
      if (mp[ny][nx] == '#')
        continue;
      if (dist[ny][nx] && dist[ny][nx] < dist[y][x] + 1)
        continue;
      q.push(ny);
      q.push(nx);
      dist[ny][nx] = dist[y][x] + 1;
    }
  }

  if (!dist[h - 1][w - 1])
    cout << -1 << endl;
  else
    cout << white - dist[h - 1][w - 1] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define foreach(i, v)                                                          \
  for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)

using namespace std;
typedef long long ll;

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

int dist[60][60];
int h, w;
int sy, sx;
std::vector<string> mp(60);

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  int h, w;
  cin >> h >> w;

  int white = 0;
  for (int y = 0; y < h; y++) {
    cin >> mp[y];
    white += count(mp[y].begin(), mp[y].end(), '.');
  }

  queue<int> q;
  q.push(0);
  q.push(0);
  dist[0][0] = 1;
  while (q.size()) {
    int y = q.front();
    q.pop();
    int x = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (!(0 <= ny && ny < h && 0 <= nx && nx < w))
        continue;
      if (mp[ny][nx] == '#')
        continue;
      if (dist[ny][nx] && dist[ny][nx] <= dist[y][x] + 1)
        continue;
      q.push(ny);
      q.push(nx);
      dist[ny][nx] = dist[y][x] + 1;
    }
  }

  if (!dist[h - 1][w - 1])
    cout << -1 << endl;
  else
    cout << white - dist[h - 1][w - 1] << endl;

  return 0;
}
Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <queue>

using namespace std;

int main(int argc, char const *argv[]) {
  int h, w;
  scanf("%d %d\n", &h, &w);
  char maze[h][w + 1];
  for (int i = 0; i < h; i++) {
    scanf("%s\n", maze[i]);
  }

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

  int d[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = -1;
    }
  }
  d[0][0] = 0;
  queue<pair<int, int>> que;
  que.push(make_pair(0, 0));

  int dxy[] = {0, 1, 0, -1, 0};
  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    int x = p.first;
    int y = p.second;
    for (int i = 0; i < 4; i++) {
      int nx = x + dxy[i];
      int ny = y + dxy[i + 1];
      if (nx < 0 || h <= nx || ny < 0 || w <= ny)
        continue;
      else if (maze[nx][ny] == '#')
        continue;
      else if (nx == h - 1 && ny == w - 1) {
        printf("%d\n", cnt - d[x][y] - 2);
        return 0;
      }
      d[nx][ny] = d[x][y] + 1;
      que.push(make_pair(nx, ny));
    }
  }
  printf("-1\n");

  return 0;
}


Fixed submission:
#include <cstdio>
#include <queue>

using namespace std;

int main(int argc, char const *argv[]) {
  int h, w;
  scanf("%d %d\n", &h, &w);
  char maze[h][w + 1];
  for (int i = 0; i < h; i++) {
    scanf("%s\n", maze[i]);
  }

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

  int d[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = -1;
    }
  }
  d[0][0] = 0;
  queue<pair<int, int>> que;
  que.push(make_pair(0, 0));

  int dxy[] = {0, 1, 0, -1, 0};
  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    int x = p.first;
    int y = p.second;
    for (int i = 0; i < 4; i++) {
      int nx = x + dxy[i];
      int ny = y + dxy[i + 1];
      if (nx < 0 || h <= nx || ny < 0 || w <= ny)
        continue;
      else if (maze[nx][ny] == '#')
        continue;
      else if (d[nx][ny] != -1)
        continue;
      else if (nx == h - 1 && ny == w - 1) {
        printf("%d\n", cnt - d[x][y] - 2);
        return 0;
      }
      d[nx][ny] = d[x][y] + 1;
      que.push(make_pair(nx, ny));
    }
  }
  printf("-1\n");

  return 0;
}
Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
string str[55];
int h, w, f[55][55], res;
const int INF = 10000;
vector<P> vec[55];
int x[4] = {0, 1, 0, -1}, y[4] = {1, 0, -1, 0};

int bfs(int k) {
  if (vec[k].empty())
    return res;
  while (!vec[k].empty()) {
    P p = vec[k][0];
    vec[k].erase(vec[k].begin());
    for (int i = 0; i < 4; i++) {
      int nx = p.first + x[i], ny = p.second + y[i];
      if (nx < 0 || nx >= h)
        continue;
      if (ny < 0 || ny >= w)
        continue;
      if (f[nx][ny] != INF)
        continue;
      if (str[nx][ny] == '#')
        continue;
      if (nx == h - 1 && ny == w - 1)
        return k + 1;
      f[nx][ny] = k + 1;
      vec[k + 1].push_back(P(nx, ny));
    }
  }
  return bfs(k + 1);
}

int main(void) {
  // Your code here!
  cin >> h >> w;
  for (int i = 0; i < h; i++)
    cin >> str[i];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (str[i][j] == '.')
        res++;
      f[i][j] = INF;
    }
  }
  f[0][0] = 0;
  vec[0].push_back(P(0, 0));
  cout << res - bfs(0) - 1 << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
string str[55];
int h, w, f[55][55], res;
const int INF = 10000;
vector<P> vec[10000];
int x[4] = {0, 1, 0, -1}, y[4] = {1, 0, -1, 0};

int bfs(int k) {
  if (vec[k].empty())
    return res;
  while (!vec[k].empty()) {
    P p = vec[k][0];
    vec[k].erase(vec[k].begin());
    for (int i = 0; i < 4; i++) {
      int nx = p.first + x[i], ny = p.second + y[i];
      if (nx < 0 || nx >= h)
        continue;
      if (ny < 0 || ny >= w)
        continue;
      if (f[nx][ny] != INF)
        continue;
      if (str[nx][ny] == '#')
        continue;
      if (nx == h - 1 && ny == w - 1)
        return k + 1;
      f[nx][ny] = k + 1;
      vec[k + 1].push_back(P(nx, ny));
    }
  }
  return bfs(k + 1);
}

int main(void) {
  // Your code here!
  cin >> h >> w;
  for (int i = 0; i < h; i++)
    cin >> str[i];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (str[i][j] == '.')
        res++;
      f[i][j] = INF;
    }
  }
  f[0][0] = 0;
  vec[0].push_back(P(0, 0));
  cout << res - bfs(0) - 1 << endl;
}
Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>

using namespace std;

typedef long long ll;

struct Square {
  int i;
  int j;
};

int main() {
  int H, W;
  char s[51][51];
  int memo[51][51] = {};

  cin >> H >> W;

  int blackNum = 0;
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      cin >> s[i][j];
      if (s[i][j] == '#')
        ++blackNum;
    }
  }

  queue<struct Square> q;
  struct Square first = {1, 1};
  q.push(first);
  memo[1][1] = 1;
  while (!q.empty()) {
    struct Square cur = q.front();
    q.pop();

    int i = cur.i;
    int j = cur.j;

    if (i == H && j == W)
      break;

    if (i > 1 && memo[i - 1][j] == 0 && s[i - 1][j] == '.') {
      memo[i - 1][j] = memo[i][j] + 1;
      struct Square next = {i - 1, j};
      q.push(next);
    }
    if (i < H && memo[i + 1][j] == 0 && s[i + 1][j] == '.') {
      memo[i + 1][j] = memo[i][j] + 1;
      struct Square next = {i + 1, j};
      q.push(next);
    }
    if (j > 1 && memo[i][j - 1] == 0 && s[i][j - 1] == '.') {
      memo[i][j - 1] = memo[i][j] + 1;
      struct Square next = {i, j - 1};
      q.push(next);
    }
    if (j < W && memo[i][j + 1] == 0 && s[i][j + 1] == '.') {
      memo[i][j + 1] = memo[i][j] + 1;
      struct Square next = {i, j + 1};
      q.push(next);
    }
  }

  if (memo[H][W] == 0)
    return -1;

  cout << H * W - memo[H][W] - blackNum << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>

using namespace std;

typedef long long ll;

struct Square {
  int i;
  int j;
};

int main() {
  int H, W;
  char s[51][51];
  int memo[51][51] = {};

  cin >> H >> W;

  int blackNum = 0;
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      cin >> s[i][j];
      if (s[i][j] == '#')
        ++blackNum;
    }
  }

  queue<struct Square> q;
  struct Square first = {1, 1};
  q.push(first);
  memo[1][1] = 1;
  while (!q.empty()) {
    struct Square cur = q.front();
    q.pop();

    int i = cur.i;
    int j = cur.j;

    if (i == H && j == W)
      break;

    if (i > 1 && memo[i - 1][j] == 0 && s[i - 1][j] == '.') {
      memo[i - 1][j] = memo[i][j] + 1;
      struct Square next = {i - 1, j};
      q.push(next);
    }
    if (i < H && memo[i + 1][j] == 0 && s[i + 1][j] == '.') {
      memo[i + 1][j] = memo[i][j] + 1;
      struct Square next = {i + 1, j};
      q.push(next);
    }
    if (j > 1 && memo[i][j - 1] == 0 && s[i][j - 1] == '.') {
      memo[i][j - 1] = memo[i][j] + 1;
      struct Square next = {i, j - 1};
      q.push(next);
    }
    if (j < W && memo[i][j + 1] == 0 && s[i][j + 1] == '.') {
      memo[i][j + 1] = memo[i][j] + 1;
      struct Square next = {i, j + 1};
      q.push(next);
    }
  }

  if (memo[H][W] == 0) {
    cout << -1 << endl;
    return 0;
  }

  cout << H * W - memo[H][W] - blackNum << endl;

  return 0;
}
Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
// D - Grid Repainting
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using VC = vector<char>;
using VVC = vector<VC>;
using PII = pair<int, int>;
#define rp(i, s, e) for (int i = (s); i < (e); ++i)
#define rpz(i, e) rp(i, 0, e)

int bfs(int H, int W, VVC &S) {
  int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
  vvi dist(H + 1, vi(W + 1, -1));
  dist[1][1] = 0;
  queue<PII> q;
  q.push({1, 1});
  while (!q.empty()) {
    int i, j;
    tie(i, j) = q.front();
    q.pop();
    S[i][j] = '_';
    if (i == H && j == W)
      break;
    rpz(k, 4) {
      int y = i + dy[k], x = j + dx[k];
      if (S[y][x] != '.')
        continue;
      dist[y][x] = dist[i][j] + 1;
      q.push({y, x});
    }
  }
  return dist[H][W];
}

int main() {
  int H, W;
  cin >> H >> W;
  VVC S(H + 2, VC(W + 2, '#'));
  int block = 0;
  rpz(i, H) {
    string s;
    cin >> s;
    rpz(j, W) {
      if (s[j] == '#')
        block++;
      S[i + 1][j + 1] = s[j];
    }
  }
  int dist = bfs(H, W, S);
  cout << (dist < 0 ? -1 : H * W - block - dist - 1) << endl;
}

Fixed submission:
// D - Grid Repainting
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using VC = vector<char>;
using VVC = vector<VC>;
using PII = pair<int, int>;
#define rp(i, s, e) for (int i = (s); i < (e); ++i)
#define rpz(i, e) rp(i, 0, e)

int bfs(int H, int W, VVC &S) {
  int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
  vvi dist(H + 1, vi(W + 1, -1));
  dist[1][1] = 0;
  queue<PII> q;
  q.push({1, 1});
  while (!q.empty()) {
    int i, j;
    tie(i, j) = q.front();
    q.pop();
    if (S[i][j] == '_')
      continue;
    S[i][j] = '_';
    if (i == H && j == W)
      break;
    rpz(k, 4) {
      int y = i + dy[k], x = j + dx[k];
      if (S[y][x] != '.')
        continue;
      dist[y][x] = dist[i][j] + 1;
      q.push({y, x});
    }
  }
  return dist[H][W];
}

int main() {
  int H, W;
  cin >> H >> W;
  VVC S(H + 2, VC(W + 2, '#'));
  int block = 0;
  rpz(i, H) {
    string s;
    cin >> s;
    rpz(j, W) {
      if (s[j] == '#')
        block++;
      S[i + 1][j + 1] = s[j];
    }
  }
  int dist = bfs(H, W, S);
  cout << (dist < 0 ? -1 : H * W - block - dist - 1) << endl;
}
Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

int main() {

  long res = 0;
  long H, W;
  cin >> H >> W;
  vector<vector<bool>> visited(H, vector<bool>(W));
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      char a;
      cin >> a;
      visited[i][j] = (a == '#');
      if (a == '.')
        res++;
    }

  queue<pair<long, long>> task;
  vector<vector<long>> dist(H, vector<long>(W, -1));
  task.push(make_pair(0, 0));
  dist[0][0] = 1;

  long dx[4] = {1, 0, -1, 0};
  long dy[4] = {0, 1, 0, -1};

  while (!task.empty()) {
    long x = task.front().first, y = task.front().second;
    task.pop();
    visited[x][y] = true;

    for (int i = 0; i < 4; i++) {
      long nx = x + dx[i], ny = y + dy[i];
      if (0 <= nx && nx < H && 0 <= ny && ny < W)
        if (!visited[nx][ny]) {
          task.push(make_pair(nx, ny));
          dist[nx][ny] = dist[x][y] + 1;
        }
    }
  }

  res -= dist[H - 1][W - 1];

  if (dist[H - 1][W - 1] == -1)
    cout << -1 << endl;
  else
    cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

int main() {

  long res = 0;
  long H, W;
  cin >> H >> W;
  vector<vector<bool>> visited(H, vector<bool>(W));
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      char a;
      cin >> a;
      visited[i][j] = (a == '#');
      if (a == '.')
        res++;
    }

  queue<pair<long, long>> task;
  vector<vector<long>> dist(H, vector<long>(W, -1));
  task.push(make_pair(0, 0));
  dist[0][0] = 1;

  long dx[4] = {1, 0, -1, 0};
  long dy[4] = {0, 1, 0, -1};

  while (!task.empty()) {
    long x = task.front().first, y = task.front().second;
    task.pop();
    visited[x][y] = true;

    for (int i = 0; i < 4; i++) {
      long nx = x + dx[i], ny = y + dy[i];
      if (0 <= nx && nx < H && 0 <= ny && ny < W)
        if (!visited[nx][ny]) {
          task.push(make_pair(nx, ny));
          visited[nx][ny] = true;
          dist[nx][ny] = dist[x][y] + 1;
        }
    }
  }

  res -= dist[H - 1][W - 1];

  if (dist[H - 1][W - 1] == -1)
    cout << -1 << endl;
  else
    cout << res << endl;
}
Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int H, W;
string s[50];
int dist[50][50] = {};

int dh[] = {1, 0, -1, 0}, dw[] = {0, 1, 0, -1};

void visit(int h, int w, int step) {
  if (h < 0 || h >= H || w < 0 || w >= W) {
    return;
  }
  if (s[h][w] == '#') {
    return;
  }
  if (step > 50 * 50 * 2) {
    return;
  }
  if (dist[h][w] < step) {
    return;
  }
  dist[h][w] = step;
  for (int i = 0; i < 4; i++) {
    int h_ = h + dh[i];
    int w_ = w + dw[i];
    visit(h_, w_, step + 1);
  }
}

int main() {
  cin >> H >> W;
  for (int h = 0; h < H; h++) {
    cin >> s[h];
  }
  int black_count = 0;
  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      if (s[h][w] == '#') {
        black_count++;
      }
    }
  }
  memset(dist, 1000000, sizeof(dist));
  visit(0, 0, 0);
  if (dist[H - 1][W - 1] < 50 * 50 * 10) {
    cout << (H * W) - black_count - (dist[H - 1][W - 1] + 1) << endl;
  } else {
    cout << -1 << endl;
  }
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int H, W;
string s[50];
int dist[50][50] = {};

int dh[] = {1, 0, -1, 0}, dw[] = {0, 1, 0, -1};

void visit(int h, int w, int step) {
  if (h < 0 || h >= H || w < 0 || w >= W) {
    return;
  }
  if (s[h][w] == '#') {
    return;
  }
  if (step > 50 * 50 * 2) {
    return;
  }
  if (dist[h][w] <= step) {
    return;
  }
  dist[h][w] = step;
  for (int i = 0; i < 4; i++) {
    int h_ = h + dh[i];
    int w_ = w + dw[i];
    visit(h_, w_, step + 1);
  }
}

int main() {
  cin >> H >> W;
  for (int h = 0; h < H; h++) {
    cin >> s[h];
  }
  int black_count = 0;
  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      if (s[h][w] == '#') {
        black_count++;
      }
    }
  }
  memset(dist, 1000000, sizeof(dist));
  visit(0, 0, 0);
  if (dist[H - 1][W - 1] < 50 * 50 * 10) {
    cout << (H * W) - black_count - (dist[H - 1][W - 1] + 1) << endl;
  } else {
    cout << -1 << endl;
  }
}
Problem p03436 (C++)

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

int main() {
  int h, w;
  cin >> h >> w;
  int count = 0;
  char b[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> b[i][j];
      if (b[i][j] == '.')
        count++;
    }
  }
  int dh[4] = {-1, 1, 0, 0};
  int dw[4] = {0, 0, 1, -1};
  queue<pair<int, int>> q;
  q.push(make_pair(0, 0));
  vector<vector<bool>> visited(h, vector<bool>(w, 0));
  int ans[h][w];
  ans[0][0] = 1;
  ans[h - 1][w - 1] = -1;
  while (!q.empty()) {
    pair<int, int> cr = q.front();
    q.pop();
    if (cr.first == h - 1 && cr.second == w - 1)
      break;
    for (int i = 0; i < 4; i++) {
      int nh = cr.first + dh[i];
      int nw = cr.second + dw[i];
      if (nh > h - 1 || nh < 0 || nw > w - 1 || nw < 0 || b[nh][nw] == '#' ||
          visited[nh][nw] == 1)
        continue;
      else {
        q.push(make_pair(nh, nw));
        visited[cr.first][cr.second] = 1;
        ans[nh][nw] = ans[cr.first][cr.second] + 1;
      }
    }
  }
  if (ans[h - 1][w - 1] == -1)
    cout << -1 << endl;
  else
    cout << count - ans[h - 1][w - 1] << endl;
}

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

int main() {
  int h, w;
  cin >> h >> w;
  int count = 0;
  char b[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> b[i][j];
      if (b[i][j] == '.')
        count++;
    }
  }
  int dh[4] = {-1, 1, 0, 0};
  int dw[4] = {0, 0, 1, -1};
  queue<pair<int, int>> q;
  q.push(make_pair(0, 0));
  vector<vector<bool>> visited(h, vector<bool>(w, 0));
  int ans[h][w];
  ans[0][0] = 1;
  ans[h - 1][w - 1] = -1;
  while (!q.empty()) {
    pair<int, int> cr = q.front();
    q.pop();
    if (cr.first == h - 1 && cr.second == w - 1)
      break;
    for (int i = 0; i < 4; i++) {
      int nh = cr.first + dh[i];
      int nw = cr.second + dw[i];
      if (nh > h - 1 || nh < 0 || nw > w - 1 || nw < 0 || b[nh][nw] == '#' ||
          visited[nh][nw] == 1)
        continue;
      else {
        q.push(make_pair(nh, nw));
        visited[nh][nw] = 1;
        ans[nh][nw] = ans[cr.first][cr.second] + 1;
      }
    }
  }
  if (ans[h - 1][w - 1] == -1)
    cout << -1 << endl;
  else
    cout << count - ans[h - 1][w - 1] << endl;
}
Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
const ll mod = 1e9 + 7;
typedef pair<int, int> P;

const int MAX = 10000;

int main() {
  vector<vector<char>> maze(50, vector<char>(50));
  vector<vector<int>> d(50, vector<int>(50));
  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};
  int ans = 0;
  int h, w;
  cin >> h >> w;
  REPS(i, h) {
    REPS(j, w) {
      cin >> maze.at(i).at(j);
      d.at(i).at(j) = MAX;
      if (maze.at(i).at(j) == '.')
        ans++;
    }
  }
  queue<P> que;
  que.push(P(1, 1));
  d.at(1).at(1) = 0;

  while (que.size()) {

    P p = que.front();
    que.pop();

    REP(i, 4) {
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];
      if (nx > 0 && nx <= w && ny > 0 && ny <= h && maze.at(ny).at(nx) == '.' &&
          d.at(ny).at(nx) == MAX) {
        que.push(P(nx, ny));
        d.at(ny).at(nx) = d.at(p.second).at(p.first) + 1;
      }
    }
  }
  if (d.at(h).at(w) == MAX)
    cout << -1 << endl;
  else {
    ans = ans - d.at(h).at(w) - 1;
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
const ll mod = 1e9 + 7;
typedef pair<int, int> P;

const int MAX = 10000;

int main() {
  vector<vector<char>> maze(51, vector<char>(51));
  vector<vector<int>> d(51, vector<int>(51));
  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};
  int ans = 0;
  int h, w;
  cin >> h >> w;
  REPS(i, h) {
    REPS(j, w) {
      cin >> maze.at(i).at(j);
      d.at(i).at(j) = MAX;
      if (maze.at(i).at(j) == '.')
        ans++;
    }
  }
  queue<P> que;
  que.push(P(1, 1));
  d.at(1).at(1) = 0;

  while (que.size()) {

    P p = que.front();
    que.pop();

    REP(i, 4) {
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];
      if (nx > 0 && nx <= w && ny > 0 && ny <= h && maze.at(ny).at(nx) == '.' &&
          d.at(ny).at(nx) == MAX) {
        que.push(P(nx, ny));
        d.at(ny).at(nx) = d.at(p.second).at(p.first) + 1;
      }
    }
  }
  if (d.at(h).at(w) == MAX)
    cout << -1 << endl;
  else {
    ans = ans - d.at(h).at(w) - 1;
    cout << ans << endl;
  }

  return 0;
}
Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
const int INF = 100000000;
int h, w, n, ans;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int dp[50][50];
string m[50];

void init() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dp[i][j] = INF;
    }
  }
}

void bfs(P s, P g) {
  init();
  queue<P> que;
  que.push(s);
  dp[s.first][s.second] = 0;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (p == g) {
      ans += dp[p.first][p.second];
      return;
    } else {
      for (int i = 0; i < 4; i++) {
        if (p.first + dx[i] >= h || p.second + dy[i] >= w)
          continue;
        if (p.first + dx[i] < 0 || p.second + dy[i] < 0)
          continue;
        if (m[p.first + dx[i]][p.second + dy[i]] == '#')
          continue;
        if (dp[p.first + dx[i]][p.second + dy[i]] > dp[p.first][p.second]) {
          dp[p.first + dx[i]][p.second + dy[i]] = dp[p.first][p.second] + 1;
          que.push(P(p.first + dx[i], p.second + dy[i]));
        }
      }
    }
  }
  ans = -1;
  return;
}

int main() {
  cin >> h >> w;
  int cnt = 0;
  for (int i = 0; i < h; i++) {
    cin >> m[i];
    for (int j = 0; j < w; j++) {
      if (m[i][j] == '#') {
        cnt++;
      }
    }
  }
  bfs(P(0, 0), P(h - 1, w - 1));
  if (ans == -1) {
    cout << ans << endl;
  } else {
    cout << h * w - ans - cnt - 1 << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
const int INF = 100000000;
int h, w, n, ans;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int dp[50][50];
string m[50];

void init() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dp[i][j] = INF;
    }
  }
}

void bfs(P s, P g) {
  init();
  queue<P> que;
  que.push(s);
  dp[s.first][s.second] = 0;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (p == g) {
      ans += dp[p.first][p.second];
      return;
    } else {
      for (int i = 0; i < 4; i++) {
        if (p.first + dx[i] >= h || p.second + dy[i] >= w)
          continue;
        if (p.first + dx[i] < 0 || p.second + dy[i] < 0)
          continue;
        if (m[p.first + dx[i]][p.second + dy[i]] == '#')
          continue;
        if (dp[p.first + dx[i]][p.second + dy[i]] == INF) {
          dp[p.first + dx[i]][p.second + dy[i]] = dp[p.first][p.second] + 1;
          que.push(P(p.first + dx[i], p.second + dy[i]));
        }
      }
    }
  }
  ans = -1;
  return;
}

int main() {
  cin >> h >> w;
  int cnt = 0;
  for (int i = 0; i < h; i++) {
    cin >> m[i];
    for (int j = 0; j < w; j++) {
      if (m[i][j] == '#') {
        cnt++;
      }
    }
  }
  bfs(P(0, 0), P(h - 1, w - 1));
  if (ans == -1) {
    cout << ans << endl;
  } else {
    cout << h * w - ans - cnt - 1 << endl;
  }
  return 0;
}
Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include "algorithm"
#include "array"
#include "iostream"
#include "string"
#include "vector"
#include <queue>

using namespace std;
#define rep(p, q) for (int i = p; i < q; i++)

// 4

int main(void) {
  vector<string> board = vector<string>();
  int w, h = 0;
  cin >> h >> w;
  int panel_num = -1; // start 分
  rep(0, w) {
    string str = "";
    cin >> str;
    board.push_back(str);
    for (int j = 0; j < str.size(); j++) {
      if (str[j] == '.')
        panel_num++;
    }
  }
  const int INF = 1000000;
  typedef pair<int, int> P;
  int sx = 0, sy = 0;
  int gx = w - 1, gy = h - 1;
  int d[50][50];
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  for (int i = 0; i < 50; i++) {
    for (int j = 0; j < 50; j++) {
      d[i][j] = INF;
    }
  }

  queue<P> que;
  que.push(P(sx, sy));
  que.push(P(sx, sy));
  d[sx][sy] = 0;

  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (p.first == gx && p.second == gy)
      break;

    rep(0, 4) {
      int nx = p.first + dx[i], ny = p.second + dy[i];

      if (0 <= nx && nx < w && 0 <= ny && ny < h && board[ny][nx] != '#' &&
          d[nx][ny] == INF) {
        que.push(P(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  if (d[gx][gy] == INF)
    cout << -1 << endl;
  else
    cout << panel_num - d[gx][gy] << endl;
}

Fixed submission:
#include "algorithm"
#include "array"
#include "iostream"
#include "string"
#include "vector"
#include <queue>

using namespace std;
#define rep(p, q) for (int i = p; i < q; i++)

// 4

int main(void) {
  vector<string> board = vector<string>();
  int w, h = 0;
  cin >> h >> w;
  int panel_num = -1; // start 分
  rep(0, h) {
    string str = "";
    cin >> str;
    board.push_back(str);
    for (int j = 0; j < str.size(); j++) {
      if (str[j] == '.')
        panel_num++;
    }
  }
  const int INF = 1000000;
  typedef pair<int, int> P;
  int sx = 0, sy = 0;
  int gx = w - 1, gy = h - 1;
  int d[50][50];
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  for (int i = 0; i < 50; i++) {
    for (int j = 0; j < 50; j++) {
      d[i][j] = INF;
    }
  }

  queue<P> que;
  que.push(P(sx, sy));
  que.push(P(sx, sy));
  d[sx][sy] = 0;

  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (p.first == gx && p.second == gy)
      break;

    rep(0, 4) {
      int nx = p.first + dx[i], ny = p.second + dy[i];

      if (0 <= nx && nx < w && 0 <= ny && ny < h && board[ny][nx] != '#' &&
          d[nx][ny] == INF) {
        que.push(P(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  if (d[gx][gy] == INF)
    cout << -1 << endl;
  else
    cout << panel_num - d[gx][gy] << endl;
}
Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<int, int>

char grid[55][55];
int dist[55][55];
const int INF = INT_MAX;
int di[] = {0, 1, 0, -1};
int dj[] = {1, 0, -1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  int black = 0;
  rep(i, h) rep(j, w) {
    cin >> grid[i][j];
    dist[i][j] = INF;
    if (grid[i][j] == '#')
      black++;
  }
  dist[0][0] = 0;
  queue<P> q;
  q.push(make_pair(0, 0));
  while (!q.empty()) {
    int pi, pj;
    tie(pi, pj) = q.front();
    q.pop();
    int past = dist[pi][pj];
    rep(tmp, 4) {
      int now = past + 1;
      int ni = pi + di[tmp], nj = pj + dj[tmp];
      if (grid[ni][nj] == '#')
        continue;
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (dist[ni][nj] < now)
        continue;
      dist[ni][nj] = now;
      q.push(make_pair(ni, nj));
    }
  }
  if (dist[h - 1][w - 1] != INF)
    cout << h * w - black - dist[h - 1][w - 1] - 1 << endl;
  else
    cout << -1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<int, int>

char grid[55][55];
int dist[55][55];
const int INF = INT_MAX;
int di[] = {0, 1, 0, -1};
int dj[] = {1, 0, -1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  int black = 0;
  rep(i, h) rep(j, w) {
    cin >> grid[i][j];
    dist[i][j] = INF;
    if (grid[i][j] == '#')
      black++;
  }
  dist[0][0] = 0;
  queue<P> q;
  q.push(make_pair(0, 0));
  while (!q.empty()) {
    int pi, pj;
    tie(pi, pj) = q.front();
    q.pop();
    int past = dist[pi][pj];
    rep(tmp, 4) {
      int now = past + 1;
      int ni = pi + di[tmp], nj = pj + dj[tmp];
      if (grid[ni][nj] == '#')
        continue;
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (dist[ni][nj] <= now)
        continue;
      dist[ni][nj] = now;
      q.push(make_pair(ni, nj));
    }
  }
  if (dist[h - 1][w - 1] != INF)
    cout << h * w - black - dist[h - 1][w - 1] - 1 << endl;
  else
    cout << -1 << endl;
}