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

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<vector<int>> c(3, vector<int>(3));
  vector<int> x(3);
  int i, j;
  for (i = 0; i < 3; ++i) {
    for (j = 0; j < 3; ++j) {
      cin >> c[i][j];
    }
    cout << endl;
  }
  for (i = 1; i < 3; ++i) {
    for (j = 0; j < 3; ++j) {
      x[j] = c[i][j] - c[0][j];
    }
    if (x[0] != x[1] || x[0] != x[2] || x[1] != x[2]) {
      cout << "No" << endl;
      exit(1);
    }
  }
  cout << "Yes" << endl;
  return 0;
}

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

int main() {
  vector<vector<int>> c(3, vector<int>(3));
  vector<int> x(3);
  int i, j;
  for (i = 0; i < 3; ++i) {
    for (j = 0; j < 3; ++j) {
      cin >> c[i][j];
    }
    cout << endl;
  }
  for (i = 1; i < 3; ++i) {
    for (j = 0; j < 3; ++j) {
      x[j] = c[i][j] - c[0][j];
    }
    if (x[0] != x[1] || x[0] != x[2] || x[1] != x[2]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  return 0;
}
Problem p03435 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#define inf 999999999
int main() {
  int h, w, cost[52][52], cnt;
  char s[52][52];
  scanf("%d%d", &h, &w);
  for (int i = 1; i <= h; i++)
    scanf("%s", s[i] + 1);
  for (int i = 0; i < 52; i++)
    for (int j = 0; j < 52; j++)
      cost[i][j] = inf;
  cost[1][1] = 1;
  for (int k = 1; cost[h][w] == inf; k++) {
    cnt = 0;
    for (int i = 1; i <= h; i++)
      for (int j = 1; j <= w; j++) {
        if (cost[i][j] == k) {
          cnt++;
          if (s[i - 1][j] == '.' && cost[i - 1][j] > k)
            cost[i - 1][j] = k + 1;
          if (s[i][j - 1] == '.' && cost[i][j - 1] > k)
            cost[i][j - 1] = k + 1;
          if (s[i + 1][j] == '.' && cost[i + 1][j] > k)
            cost[i + 1][j] = k + 1;
          if (s[i][j + 1] == '.' && cost[i][j + 1] > k)
            cost[i][j + 1] = k + 1;
        }
      }
    if (cnt == 0) {
      printf("-1\n");
      return 0;
    }
  }
  cnt = 0;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      if (s[i][j] == '.')
        cnt++;
  printf("%d\n", cnt - cost[h][w]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, l, r) for (ll i = (l); i < (r); i++)
int main(void) {
  int a = 1;
  int vec[3][3];
  rep(i, 0, 3) rep(j, 0, 3) cin >> vec[i][j];
  rep(i, 0, 3) rep(j, 0, 3) vec[i][j] -= vec[i][2];
  rep(i, 0, 3) rep(j, 0, 3) vec[j][i] -= vec[2][i];
  rep(i, 0, 3) rep(j, 0, 3) a *= !vec[i][j];
  cout << (a ? "Yes" : "No");
  return 0;
}
Problem p03435 (C++)

Buggy submission (status: Runtime Error):
// ConsoleApplication3.cpp : アプリケーションのエントリ ポイントを定義します。
//

#include <iostream>
#include <vector>

using namespace std;

int main() {
  bool kekka = true;
  vector<vector<int>> data;

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> data[i][j];
    }
  }

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {

      if (data[i][j] - data[i][(j + 1) % 3] !=
          data[(i + 1) % 3][j] - data[(i + 1) % 3][(j + 1) % 3]) {
        kekka = false;
      } else if (data[i][j] - data[(i + 1) % 3][j] !=
                 data[i][(j + 1) % 3] - data[(i + 1) % 3][(j + 1) % 3]) {
        kekka = false;
      }
    }
  }
  if (kekka) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}


Fixed submission:
// ConsoleApplication3.cpp : アプリケーションのエントリ ポイントを定義します。
//

#include <iostream>
#include <vector>

using namespace std;

int main() {
  bool kekka = true;
  vector<vector<int>> data{{1, 2, 3}, {4, 5, 6}, {4, 5, 6}};

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> data[i][j];
    }
  }

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {

      if (data[i][j] - data[i][(j + 1) % 3] !=
          data[(i + 1) % 3][j] - data[(i + 1) % 3][(j + 1) % 3]) {
        kekka = false;
      } else if (data[i][j] - data[(i + 1) % 3][j] !=
                 data[i][(j + 1) % 3] - data[(i + 1) % 3][(j + 1) % 3]) {
        kekka = false;
      }
    }
  }
  if (kekka) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p03435 (C++)

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

int main() {
  int c[3][3];
  for (int i = 0; i < 9; i++) {
    cin >> c[i / 3][i % 3];
  }
  int a[3];
  int b[3];
  bool res = false;
  for (int i = -100; i = 100; i++) {
    a[0] = i;
    b[0] = c[0][0] - a[0];
    a[1] = c[1][0] - b[0];
    a[2] = c[2][0] - b[0];
    b[1] = c[0][1] - a[0];
    b[2] = c[0][2] - a[0];
    if (c[1][1] == a[1] + b[1] && c[1][2] == a[1] + b[2] &&
        c[2][1] == a[2] + b[1] && c[2][2] == a[2] + b[2]) {
      res = true;
      break;
    }
  }
  if (res)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

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

int main() {
  int c[3][3];
  for (int i = 0; i < 9; i++) {
    cin >> c[i / 3][i % 3];
  }
  int a[3];
  int b[3];
  bool res = false;
  a[0] = 0;
  b[0] = c[0][0] - a[0];
  a[1] = c[1][0] - b[0];
  a[2] = c[2][0] - b[0];
  b[1] = c[0][1] - a[0];
  b[2] = c[0][2] - a[0];
  if (c[1][1] == a[1] + b[1] && c[1][2] == a[1] + b[2] &&
      c[2][1] == a[2] + b[1] && c[2][2] == a[2] + b[2]) {
    res = true;
  }
  if (res)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}
Problem p03435 (C++)

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define vi vector<int>
#define vivi vector<vi>
const int INF = 1 << 29;
typedef long long int ll;

int main() {
  int c[3][3];
  int N = 0;
  rep(i, 3) {
    rep(j, 3) {
      cin >> c[i][j];
      N += c[i][j];
    }
  }
  if (N % 3 != 0) {
    cout << "No";
    return 0;
  } else {
    N /= 3;
  }
  int a[3], b[3];
  for (a[0] = 0; a[0] <= N; ++a[0]) {
    for (a[1] = 0; a[1] <= N - a[0]; ++a[1]) {
      for (a[2] = 0; a[2] <= N - a[1] - a[0]; ++a[2])
        for (b[0] = 0; b[0] <= N - a[0] - a[1] - a[2]; ++b[0]) {
          for (b[1] = 0; b[1] <= N - b[0] - a[0] - a[1] - a[2]; ++b[1]) {
            for (b[2] = 0; b[2] <= N - b[0] - b[1] - a[0] - a[1] - a[2];
                 ++b[2]) {
              bool flg = true;
              rep(m, 3) rep(u, 3) if (c[m][u] != b[m] + a[u]) flg = false;
              if (flg) {
                cout << "Yes";
                return 0;
              }
            }
            // cout << b[2] << endl;
          }
        }
    }
  }
  cout << "No";
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define vi vector<int>
#define vivi vector<vi>
const int INF = 1 << 29;
typedef long long int ll;

int main() {
  int c[3][3];
  int N = 0;
  rep(i, 3) rep(j, 3) cin >> c[i][j];
  rep(i, 101) {
    int a[3], b[3];
    a[0] = i;
    b[0] = c[0][0] - a[0];
    b[1] = c[0][1] - a[0];
    b[2] = c[0][2] - a[0];
    a[1] = c[1][0] - b[0];
    a[2] = c[2][0] - b[0];
    bool flg = true;
    rep(s, 3) rep(t, 3) if (c[s][t] != a[s] + b[t]) flg = false;

    if (flg) {
      cout << "Yes";
      return 0;
    }
  }
  cout << "No";
}
Problem p03435 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  // cout << fixed << setprecision(15);
  int C[3][3];
  REP(i, 3) {
    REP(k, 3) { cin >> C[i][k]; }
  }
  vector<int> a(3, 0), b(3, 0);
  // if(C[0][0] == 0) a[0] = 0, b[0] = 0;
  REP(i, C[0][0] + 1) {
    a[0] = i, b[i] = C[0][0] - i;
    b[1] = C[0][1] - a[0];
    b[2] = C[0][2] - a[0];
    a[1] = C[1][0] - b[0];
    a[2] = C[2][0] - b[0];
    bool flag = true;
    for (int k = 1; k <= 2; k++) {
      for (int j = 1; j <= 2; j++) {
        if (a[k] + b[j] != C[k][j])
          flag = false;
      }
    }
    if (flag) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  // cout << fixed << setprecision(15);
  int C[3][3];
  REP(i, 3) {
    REP(k, 3) { cin >> C[i][k]; }
  }
  vector<int> a(3, 0), b(3, 0);
  // if(C[0][0] == 0) a[0] = 0, b[0] = 0;
  REP(i, C[0][0] + 1) {
    a[0] = i, b[0] = C[0][0] - i;
    b[1] = C[0][1] - a[0];
    b[2] = C[0][2] - a[0];
    a[1] = C[1][0] - b[0];
    a[2] = C[2][0] - b[0];
    bool flag = true;
    for (int k = 1; k <= 2; k++) {
      for (int j = 1; j <= 2; j++) {
        if (a[k] + b[j] != C[k][j])
          flag = false;
      }
    }
    if (flag) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;
  return 0;
}
Problem p03435 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

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

int main() {
  std::vector<int> v;
  int c[3][3];
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> c[i][j];
    }
  }
  int a[3], b[3];
  for (int a0 = -INF; a0 < c[0][0]; a0++) {
    for (int j = 0; j < 3; j++) {
      b[j] = c[0][j] - a0;
    }
    if (c[1][0] - b[0] == c[1][1] - b[1] && c[1][1] - b[1] == c[1][2] - b[2] &&
        c[2][0] - b[0] == c[2][1] - b[1] && c[2][1] - b[1] == c[2][2] - b[2]) {
      cout << "Yes"
           << "\n";
      return 0;
    }
  }
  cout << "No"
       << "\n";
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

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

int main() {
  std::vector<int> v;
  int c[3][3];
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> c[i][j];
    }
  }
  int a[3], b[3];
  for (int a0 = -100000; a0 < c[0][0]; a0++) {
    for (int j = 0; j < 3; j++) {
      b[j] = c[0][j] - a0;
    }
    if (c[1][0] - b[0] == c[1][1] - b[1] && c[1][1] - b[1] == c[1][2] - b[2] &&
        c[2][0] - b[0] == c[2][1] - b[1] && c[2][1] - b[1] == c[2][2] - b[2]) {
      cout << "Yes"
           << "\n";
      return 0;
    }
  }
  cout << "No"
       << "\n";
}
Problem p03435 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

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

  vector<vector<int>> vvi;
  vvi = vector<vector<int>>(3, vector<int>(3, 0));
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> vvi[i][j];
    }
  }
  for (int a = 0; a <= 100; ++a) {
    for (int b = 0; b <= 100; ++b) {
      for (int c = 0; c <= 100; ++c) {
        if (((vvi[0][0] - a) == (vvi[0][1] - b)) &&
            ((vvi[0][1] - b) == (vvi[0][2] - c))) {
          if (((vvi[1][0] - a) == (vvi[1][1] - b)) &&
              ((vvi[1][1] - b) == (vvi[1][2] - c))) {
            if (((vvi[2][0] - a) == (vvi[2][1] - b)) &&
                ((vvi[2][1] - b) == (vvi[2][2] - c))) {
              cout << "Yes" << endl;
              return 1;
            }
          }
        }
      }
    }
  }
  cout << "No" << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

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

  vector<vector<int>> vvi;
  vvi = vector<vector<int>>(3, vector<int>(3, 0));
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> vvi[i][j];
    }
  }
  for (int a = 0; a <= 100; ++a) {
    for (int b = 0; b <= 100; ++b) {
      for (int c = 0; c <= 100; ++c) {
        if (((vvi[0][0] - a) == (vvi[0][1] - b)) &&
            ((vvi[0][1] - b) == (vvi[0][2] - c))) {
          if (((vvi[1][0] - a) == (vvi[1][1] - b)) &&
              ((vvi[1][1] - b) == (vvi[1][2] - c))) {
            if (((vvi[2][0] - a) == (vvi[2][1] - b)) &&
                ((vvi[2][1] - b) == (vvi[2][2] - c))) {
              cout << "Yes" << endl;
              return 0;
            }
          }
        }
      }
    }
  }
  cout << "No" << endl;

  return 0;
}
Problem p03435 (C++)

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

using namespace std;
#define N 3

int main(void) {
  vector<vector<int>> c(3, vector<int>(3, 0));

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      int tmp;
      cin >> tmp;
      c[i][j] = tmp;
    }
  }
  int total = 0;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      total += c[i][j];
    }
  }

  if (total % 3 == 0) {
    total /= 3;
    if (total == (c[1][1] + c[2][2] + c[3][3])) {
      cout << "Yes" << endl;
    }
  } else {
    cout << "No" << endl;
  }

  return 0;
}

Fixed submission:
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;
#define N 3

int main(void) {
  vector<vector<int>> c(3, vector<int>(3, 0));

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      int tmp;
      cin >> tmp;
      c[i][j] = tmp;
    }
  }
  int total = 0;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      total += c[i][j];
    }
  }

  if ((total % 3 == 0) && (total == 3 * (c[0][0] + c[1][1] + c[2][2]))) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}
Problem p03435 (C++)

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

int main() {
  vector<vector<int>> c(3, vector<int>(3));
  vector<int> an(3);
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> c.at(i).at(j);
    }
  }
  an.at(0) = c[0][0] + c[1][1] + c[2][2];
  an.at(1) = c[1][2] + c[2][3] + c[3][1];
  an.at(2) = c[1][3] + c[2][1] + c[3][2];
  if (an.at(0) == an.at(1) && an.at(1) == an.at(2)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

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

int main() {
  vector<vector<int>> c(3, vector<int>(3));
  vector<int> an(3);
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> c.at(i).at(j);
    }
  }
  an.at(0) = c[0][0] + c[1][1] + c[2][2];
  an.at(1) = c[0][1] + c[1][2] + c[2][0];
  an.at(2) = c[0][2] + c[1][0] + c[2][1];
  if (an.at(0) == an.at(1) && an.at(1) == an.at(2)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}
Problem p03435 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<vector<int>> table(3, vector<int>(3));
  for (int k = 0; k < 9; k++) {
    int i, j;
    i = k / 3;
    j = k & 3;
    cin >> table.at(i).at(j);
  }
  vector<int> yoko(3);
  vector<int> tate(3);
  for (int i = 0; i < 3; i++) {
    yoko.at(i) = table.at(i).at(0) + table.at(i).at(1) + table.at(i).at(2);
    tate.at(i) = table.at(0).at(i) + table.at(1).at(i) + table.at(2).at(i);
  }
  bool yokosum = true;
  bool tatesum = true;
  for (int i = 0; i < 2; i++) {
    if ((yoko.at(i + 1) - yoko.at(i)) % 3 != 0)
      yokosum = false;
    if ((tate.at(i + 1) - tate.at(i)) % 3 != 0)
      tatesum = false;
  }
  if (yokosum == true && tatesum == true)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<vector<int>> table(3, vector<int>(3));
  for (int k = 0; k < 9; k++) {
    int i, j;
    i = k / 3;
    j = k % 3;
    cin >> table.at(i).at(j);
  }
  vector<int> yoko(3);
  vector<int> tate(3);
  for (int i = 0; i < 3; i++) {
    yoko.at(i) = table.at(i).at(0) + table.at(i).at(1) + table.at(i).at(2);
    tate.at(i) = table.at(0).at(i) + table.at(1).at(i) + table.at(2).at(i);
  }
  bool yokosum = true;
  bool tatesum = true;
  for (int i = 0; i < 2; i++) {
    if ((yoko.at(i + 1) - yoko.at(i)) % 3 != 0)
      yokosum = false;
    if ((tate.at(i + 1) - tate.at(i)) % 3 != 0)
      tatesum = false;
  }
  if (yokosum == true && tatesum == true)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順
vector<string> M;
int H, W;

int main() {
  cin >> H >> W;

  int white = 0;
  rep(i, H) {
    string S;
    cin >> S;
    rep(j, W) {
      if (S[j] == '.')
        white++;
    }
    M.push_back(S);
  }

  queue<pair<int, int>> Q;
  vector<vector<int>> D(H, vector<int>(W, -1));
  Q.emplace(0, 0);
  D[0][0] = 1;
  while (!Q.empty()) {
    int x = Q.front().first;
    int y = Q.front().second;
    int dis = D[y][x];

    Q.pop();
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};
    for (int i = 0; i < sizeof(dx); i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;
      if (D[ny][nx] != -1)
        continue;
      if (M[ny][nx] == '#')
        continue;
      D[ny][nx] = dis + 1;
      Q.emplace(nx, ny);
    }
  }

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

  cout << white - D[H - 1][W - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順
vector<string> M;
int H, W;

int main() {
  cin >> H >> W;

  int white = 0;
  rep(i, H) {
    string S;
    cin >> S;
    rep(j, W) {
      if (S[j] == '.')
        white++;
    }
    M.push_back(S);
  }

  queue<pair<int, int>> Q;
  vector<vector<int>> D(H, vector<int>(W, -1));
  Q.emplace(0, 0);
  D[0][0] = 1;
  while (!Q.empty()) {
    int x = Q.front().first;
    int y = Q.front().second;
    int dis = D[y][x];

    Q.pop();
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;
      if (D[ny][nx] != -1)
        continue;
      if (M[ny][nx] == '#')
        continue;
      D[ny][nx] = dis + 1;
      Q.emplace(nx, ny);
    }
  }

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

  cout << white - D[H - 1][W - 1] << endl;
}
Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int H, W;
vector<vector<char>> grid(50, vector<char>(50));
vector<vector<int>> from(50, vector<int>(50, -1));
queue<int> q;
vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};
vector<vector<bool>> use(50, vector<bool>(50, false));

void search() {
  int x = q.front() % W;
  int y = (q.front() - x) / W;
  q.pop();

  for (int i = 0; i < 4; ++i) {
    int X = x + dx[i];
    int Y = y + dy[i];
    if (0 <= X && X < W && 0 <= Y && Y < H) {
      if (grid[Y][X] == '.' && from[Y][X] == -1) {
        q.push(Y * W + X);
        from[Y][X] = y * W + x;
      }
    }
  }
  return;
}

void re(int x, int y) {
  if (x == 0 && y == 0)
    return;

  int X = from[y][x] % W;
  int Y = (from[y][x] - X) / W;
  use[Y][X] = true;
  re(X, Y);
  return;
}

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> grid[i][j];
    }
  }

  q.push(0);
  from[0][0] = 0;
  while (q.size() > 0)
    search();

  use[H - 1][W - 1] = true;
  re(W - 1, H - 1);

  int ans = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (!use[i][j] && grid[i][j] == '.')
        ++ans;
    }
  }

  cout << ans << endl;
  return 0;
}

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

int H, W;
vector<vector<char>> grid(50, vector<char>(50));
vector<vector<int>> from(50, vector<int>(50, -1));
queue<int> q;
vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};
vector<vector<bool>> use(50, vector<bool>(50, false));

void search() {
  int x = q.front() % W;
  int y = (q.front() - x) / W;
  q.pop();

  for (int i = 0; i < 4; ++i) {
    int X = x + dx[i];
    int Y = y + dy[i];
    if (0 <= X && X < W && 0 <= Y && Y < H) {
      if (grid[Y][X] == '.' && from[Y][X] == -1) {
        q.push(Y * W + X);
        from[Y][X] = y * W + x;
      }
    }
  }
  return;
}

void re(int x, int y) {
  if (x == 0 && y == 0)
    return;

  int X = from[y][x] % W;
  int Y = (from[y][x] - X) / W;
  use[Y][X] = true;
  re(X, Y);
  return;
}

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> grid[i][j];
    }
  }

  q.push(0);
  from[0][0] = 0;
  while (q.size() > 0)
    search();
  if (from[H - 1][W - 1] == -1) {
    cout << -1 << endl;
    return 0;
  }

  use[H - 1][W - 1] = true;
  re(W - 1, H - 1);

  int ans = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (!use[i][j] && grid[i][j] == '.')
        ++ans;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p03436 (C++)

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

using namespace std;

struct Point {
  int x;
  int y;
  int count;
};

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> map(h);
  for (int i = 0; i < h; i++) {
    cin >> map[i];
  }
  queue<Point> q;
  Point p = {0, 0, 1};
  q.push(p);
  int ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      ans += map[i][j] == '.';
    }
  }
  while (!q.empty()) {
    p = q.front();
    q.pop();
    int x = p.x, y = p.y, count = p.count++;
    map[y][x] = '#';
    if (x == w - 1 && y == h - 1) {
      cout << ans - count << endl;
      return 0;
    }
    if (x > 0 && map[y][x - 1] == '.') {
      p.x--;
      q.push(p);
      p.x++;
    }
    if (y > 0 && map[y - 1][x] == '.') {
      p.y--;
      q.push(p);
      p.y++;
    }
    if (x < w - 1 && map[y][x + 1] == '.') {
      p.x++;
      q.push(p);
      p.x--;
    }
    if (y < h - 1 && map[y + 1][x] == '.') {
      p.y++;
      q.push(p);
      p.y--;
    }
  }
  cout << -1 << endl;
}

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

using namespace std;

struct Point {
  int x;
  int y;
  int count;
};

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> map(h);
  for (int i = 0; i < h; i++) {
    cin >> map[i];
  }
  queue<Point> q;
  Point p = {0, 0, 1};
  q.push(p);
  int ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      ans += map[i][j] == '.';
    }
  }
  while (!q.empty()) {
    p = q.front();
    q.pop();
    int x = p.x, y = p.y, count = p.count++;
    if (map[y][x] == '#')
      continue;
    map[y][x] = '#';
    if (x == w - 1 && y == h - 1) {
      cout << ans - count << endl;
      return 0;
    }
    if (x > 0 && map[y][x - 1] == '.') {
      p.x--;
      q.push(p);
      p.x++;
    }
    if (y > 0 && map[y - 1][x] == '.') {
      p.y--;
      q.push(p);
      p.y++;
    }
    if (x < w - 1 && map[y][x + 1] == '.') {
      p.x++;
      q.push(p);
      p.x--;
    }
    if (y < h - 1 && map[y + 1][x] == '.') {
      p.y++;
      q.push(p);
      p.y--;
    }
  }
  cout << -1 << endl;
}
Problem p03436 (Python)

Buggy submission (status: Runtime Error):
import heapq as hp
import numpy as np

h, w = map(int, input().split())
s = np.array([list(input()) for _ in range(h)])
s = np.pad(s, 1, "constant")
visited = np.zeros_like(s, dtype=np.bool)

judge = np.inf

q = []
hp.heappush(q, (0, 1, 1))

while h:
    cnt, y, x = hp.heappop(q)
    if y == h and x == w:
        judge = cnt
        break
    for dy, dx in [[0, 1], [1, 0], [-1, 0], [0, -1]]:
        ny, nx = y + dy, x + dx
        if s[ny, nx] == "." and not visited[ny, nx]:
            visited[ny, nx] = True
            hp.heappush(q, (cnt + 1, ny, nx))

if judge == np.inf:
    print(-1)
else:
    print((s == ".").sum() - judge - 1)


Fixed submission:
import heapq as hp
import numpy as np

h, w = map(int, input().split())
s = np.array([list(input()) for _ in range(h)])
s = np.pad(s, 1, "constant")
visited = np.zeros_like(s, dtype=np.bool)

judge = np.inf

q = []
hp.heappush(q, (0, 1, 1))

while q:
    cnt, y, x = hp.heappop(q)
    if y == h and x == w:
        judge = cnt
        break
    for dy, dx in [[0, 1], [1, 0], [-1, 0], [0, -1]]:
        ny, nx = y + dy, x + dx
        if s[ny, nx] == "." and not visited[ny, nx]:
            visited[ny, nx] = True
            hp.heappush(q, (cnt + 1, ny, nx))

if judge == np.inf:
    print(-1)
else:
    print((s == ".").sum() - judge - 1)
Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define ll long long int
#define MOD 1000000007LL
using namespace std;

int main() {
  char c[60][60] = {};
  int h, w;
  cin >> h >> w;
  int d = 0;
  for (int i = 1; i <= h; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= w; j++) {
      c[j][i] = s[j - 1];
      if (s[j - 1] == '.') {
        d++;
      }
    }
  }
  int ans[60][60] = {};
  queue<int> xq;
  queue<int> yq;
  xq.push(1);
  yq.push(1);
  int vx[4] = {1, 0, -1, 0};
  int vy[4] = {0, 1, 0, -1};
  int co = 0;
  while (1) {
    int x = xq.front();
    int y = yq.front();
    for (int i = 0; i < 4; i++) {
      int nx = x + vx[i];
      int ny = y + vy[i];
      if ((nx < 1) || (ny < 1)) {
        continue;
      }
      if (c[nx][ny] == '.') {
        if (ans[nx][ny] == 0) {
          ans[nx][ny] = ans[x][y] + 1;
          xq.push(nx);
          yq.push(ny);
        }
        if ((nx == w) && (ny == h)) {
          cout << d - 1 - ans[nx][ny] << endl;
          return 0;
        }
      }
    }
    xq.pop();
    yq.pop();
    co++;
    if (co > 3600) {
      cout << -1 << endl;
      return 0;
    }
  }
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define ll long long int
#define MOD 1000000007LL
using namespace std;

int main() {
  char c[60][60] = {};
  int h, w;
  cin >> h >> w;
  int d = 0;
  for (int i = 1; i <= h; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= w; j++) {
      c[j][i] = s[j - 1];
      if (s[j - 1] == '.') {
        d++;
      }
    }
  }
  int ans[60][60] = {};
  queue<int> xq;
  queue<int> yq;
  xq.push(1);
  yq.push(1);
  int vx[4] = {1, 0, -1, 0};
  int vy[4] = {0, 1, 0, -1};
  int co = 0;
  while (1) {
    int x = xq.front();
    int y = yq.front();
    for (int i = 0; i < 4; i++) {
      int nx = x + vx[i];
      int ny = y + vy[i];
      if ((nx < 1) || (ny < 1)) {
        continue;
      }
      if (c[nx][ny] == '.') {
        if (ans[nx][ny] == 0) {
          ans[nx][ny] = ans[x][y] + 1;
          xq.push(nx);
          yq.push(ny);
        }
        if ((nx == w) && (ny == h)) {
          cout << d - 1 - ans[nx][ny] << endl;
          return 0;
        }
      }
    }
    xq.pop();
    yq.pop();
    if (xq.empty()) {
      cout << -1 << endl;
      return 0;
    }
  }
}
Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int vx[4] = {0, 1, 0, -1};
int vy[4] = {1, 0, -1, 0};
vector<vector<char>> s(50, vector<char>(50));
vector<vector<int>> counter(50, vector<int>(50, -1));
int main() {
  int H, W;
  int white = 0;
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> s.at(i).at(j);
      if (s.at(i).at(j) == '.')
        white++;
    }
  }

  queue<vector<int>> q;
  q.push({0, 0});
  counter.at(0).at(0) = 0;
  while (true) {
    vector<int> now = q.front();
    q.pop();

    if (now[0] == H - 1 && now[1] == W - 1)
      break;

    for (int i = 0; i < 4; i++) {
      int next_y = now[0] + vy[i];
      int next_x = now[1] + vx[i];

      if (next_y >= 0 && next_y < H && next_x >= 0 && next_x < W) {
        if (s.at(next_y).at(next_x) == '.' &&
            counter.at(next_y).at(next_x) == -1) {
          q.push({next_y, next_x});
          counter.at(next_y).at(next_x) = counter.at(now[0]).at(now[1]) + 1;
        }
      }
    }
  }

  if (counter.at(H - 1).at(W - 1) != -1) {
    cout << white - counter.at(H - 1).at(W - 1) - 1 << endl;
  } else {
    cout << counter.at(H - 1).at(W - 1) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int vx[4] = {0, 1, 0, -1};
int vy[4] = {1, 0, -1, 0};
vector<vector<char>> s(50, vector<char>(50));
vector<vector<int>> counter(50, vector<int>(50, -1));
int main() {
  int H, W;
  int white = 0;
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> s.at(i).at(j);
      if (s.at(i).at(j) == '.')
        white++;
    }
  }

  queue<vector<int>> q;
  q.push({0, 0});
  counter.at(0).at(0) = 0;
  while (!q.empty()) {
    vector<int> now = q.front();
    q.pop();

    if (now[0] == H - 1 && now[1] == W - 1)
      break;

    for (int i = 0; i < 4; i++) {
      int next_y = now[0] + vy[i];
      int next_x = now[1] + vx[i];

      if (next_y >= 0 && next_y < H && next_x >= 0 && next_x < W) {
        if (s.at(next_y).at(next_x) == '.' &&
            counter.at(next_y).at(next_x) == -1) {
          q.push({next_y, next_x});
          counter.at(next_y).at(next_x) = counter.at(now[0]).at(now[1]) + 1;
        }
      }
    }
  }

  if (counter.at(H - 1).at(W - 1) != -1) {
    cout << white - counter.at(H - 1).at(W - 1) - 1 << endl;
  } else {
    cout << counter.at(H - 1).at(W - 1) << endl;
  }
}
Problem p03436 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define dep(i, a, n) for (int i = (a); i >= (n); i--)
#define mod (ll)(1e9 + 7)

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

struct T {
  int x, y, c;
};

int s[101][101];
char a[101][101];

int main() {
  int h, w;
  cin >> h >> w;
  int ss = 0;
  rep(i, 0, h) rep(j, 0, w) {
    cin >> a[i][j];
    if (a[i][j] == '.')
      ss++;
    s[i][j] = 1145141919;
  }

  queue<T> q;
  q.push({0, 0, 0});

  while (!q.empty()) {
    T t = q.front();
    q.pop();
    if (t.x == h - 1 && t.y == w - 1) {
      cout << ss - (t.c + 1) << endl;
      return 0;
    }
    if (s[t.x][t.y] < t.c)
      continue;
    s[t.x][t.y] = t.c;
    if (t.x > 0 && a[t.x - 1][t.y] == '.' && s[t.x - 1][t.y] > t.c + 1)
      q.push({t.x - 1, t.y, t.c + 1});
    if (t.x < h - 1 && a[t.x + 1][t.y] == '.' && s[t.x + 1][t.y] > t.c + 1)
      q.push({t.x + 1, t.y, t.c + 1});
    if (t.y > 0 && a[t.x][t.y - 1] == '.' && s[t.x][t.y - 1] > t.c + 1)
      q.push({t.x, t.y - 1, t.c + 1});
    if (t.y < w - 1 && a[t.x][t.y + 1] == '.' && s[t.x][t.y + 1] > t.c + 1)
      q.push({t.x, t.y + 1, t.c + 1});
  }
  cout << -1 << endl;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define dep(i, a, n) for (int i = (a); i >= (n); i--)
#define mod (ll)(1e9 + 7)

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

struct T {
  int x, y, c;
};

int s[101][101];
char a[101][101];

int main() {
  int h, w;
  cin >> h >> w;
  int ss = 0;
  rep(i, 0, h) rep(j, 0, w) {
    cin >> a[i][j];
    if (a[i][j] == '.')
      ss++;
    s[i][j] = 1145141919;
  }

  queue<T> q;
  q.push({0, 0, 0});

  while (!q.empty()) {
    T t = q.front();
    q.pop();
    if (t.x == h - 1 && t.y == w - 1) {
      cout << ss - (t.c + 1) << endl;
      return 0;
    }
    if (s[t.x][t.y] <= t.c)
      continue;
    s[t.x][t.y] = t.c;
    if (t.x > 0 && a[t.x - 1][t.y] == '.' && s[t.x - 1][t.y] > t.c + 1)
      q.push({t.x - 1, t.y, t.c + 1});
    if (t.x < h - 1 && a[t.x + 1][t.y] == '.' && s[t.x + 1][t.y] > t.c + 1)
      q.push({t.x + 1, t.y, t.c + 1});
    if (t.y > 0 && a[t.x][t.y - 1] == '.' && s[t.x][t.y - 1] > t.c + 1)
      q.push({t.x, t.y - 1, t.c + 1});
    if (t.y < w - 1 && a[t.x][t.y + 1] == '.' && s[t.x][t.y + 1] > t.c + 1)
      q.push({t.x, t.y + 1, t.c + 1});
  }
  cout << -1 << endl;
}
Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;

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

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }
  int res = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      res += S[i][j] != '#';
    }
  }
  int si = 0, sj = 0;
  queue<P> q;
  vector<vector<int>> d(H, vector<int>(W, -1));
  q.emplace(si, sj);
  d[si][sj] = 1;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int tx = p.first + dx[i], ty = p.second + dy[i];
      if (S[tx][ty] == '.' && 0 <= tx && tx < H && 0 <= ty && ty < W &&
          d[tx][ty] == -1) {
        d[tx][ty] = d[p.first][p.second] + 1;
        q.emplace(tx, ty);
      }
    }
  }
  cout << (d[H - 1][W - 1] == -1 ? -1 : res - d[H - 1][W - 1]) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;

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

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }
  int res = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      res += S[i][j] != '#';
    }
  }
  int si = 0, sj = 0;
  queue<P> q;
  vector<vector<int>> d(H, vector<int>(W, -1));
  q.emplace(si, sj);
  d[si][sj] = 1;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int tx = p.first + dx[i], ty = p.second + dy[i];
      if (0 <= tx && tx < H && 0 <= ty && ty < W && S[tx][ty] == '.' &&
          d[tx][ty] == -1) {
        d[tx][ty] = d[p.first][p.second] + 1;
        q.emplace(tx, ty);
      }
    }
  }
  cout << (d[H - 1][W - 1] == -1 ? -1 : res - d[H - 1][W - 1]) << endl;
  return 0;
}
Problem p03436 (Python)

Buggy submission (status: Runtime Error):
# 最短でゴールして残った黒をぜんぶ白に変えれば最大のポイントをもらえそう
# ってなふうに雑に考えてたけど、探索・最短経路復元・解答それぞれで1度ずつBFSをしなきゃならんのか？？？？

h, w = map(int, input().split())
table = [[] for _ in range(h)]
for y in range(h):
    table[y] = list(input())
# ステップ更新用
my_table = [[-1 for _ in range(w)] for _ in range(h)]
my_table[0][0] = 0
yo = [(0, 0)]
steps = [0, -1, 0, 1, 0]
while len(yo) > 0:
    p = yo[0]
    yo = yo[1:]
    for i in range(4):
        next_x, next_y = steps[i + 1] + p[1], steps[i] + p[0]
        if next_x < 0 or w <= next_x or next_y < 0 or h <= next_y:
            continue
        if table[next_y][next_x] == "#":
            continue
        if my_table[next_y][next_x] >= 0:
            continue
        my_table[next_y][next_x] = my_table[p[0]][p[1]] + 1
        yo.append((next_y, next_x))

# 経路を逆からたどって最短経路復元
restore_table = [["o" for _ in range(w)] for _ in range(h)]
restore_table[h - 1][w - 1] = "x"
now = my_table[h - 1][w - 1]
cur = (h - 1, w - 1)
while now != 0:
    for i in range(4):
        next_y, next_x = steps[i] + cur[0], steps[i + 1] + cur[1]
        if next_x < 0 or w <= next_x or next_y < 0 or h < next_y:
            continue
        if my_table[next_y][next_x] == now - 1:
            restore_table[next_y][next_x] = "x"
            cur = (next_y, next_x)
            now -= 1
            break

# やっと答え
ans = 0
for y in range(h):
    for x in range(w):
        if restore_table[y][x] == "o" and table[y][x] == ".":
            ans += 1
print(ans)


Fixed submission:
# 最短でゴールして残った黒をぜんぶ白に変えれば最大のポイントをもらえそう
# ってなふうに雑に考えてたけど、探索・最短経路復元・解答それぞれで1度ずつBFSをしなきゃならんのか？？？？

h, w = map(int, input().split())
table = [[] for _ in range(h)]
for y in range(h):
    table[y] = list(input())
# ステップ更新用
my_table = [[-1 for _ in range(w)] for _ in range(h)]
my_table[0][0] = 0
yo = [(0, 0)]
steps = [0, -1, 0, 1, 0]
while len(yo) > 0:
    p = yo[0]
    yo = yo[1:]
    for i in range(4):
        next_x, next_y = steps[i + 1] + p[1], steps[i] + p[0]
        if next_x < 0 or w <= next_x or next_y < 0 or h <= next_y:
            continue
        if table[next_y][next_x] == "#":
            continue
        if my_table[next_y][next_x] >= 0:
            continue
        my_table[next_y][next_x] = my_table[p[0]][p[1]] + 1
        yo.append((next_y, next_x))
if my_table[h - 1][w - 1] == -1:
    print(-1)
    exit()

# 経路を逆からたどって最短経路復元
restore_table = [["o" for _ in range(w)] for _ in range(h)]
restore_table[h - 1][w - 1] = "x"
now = my_table[h - 1][w - 1]
cur = (h - 1, w - 1)
while now != 0:
    for i in range(4):
        next_y, next_x = steps[i] + cur[0], steps[i + 1] + cur[1]
        if next_x < 0 or w <= next_x or next_y < 0 or h < next_y:
            continue
        if my_table[next_y][next_x] == now - 1:
            restore_table[next_y][next_x] = "x"
            cur = (next_y, next_x)
            now -= 1
            break

# やっと答え
ans = 0
for y in range(h):
    for x in range(w):
        if restore_table[y][x] == "o" and table[y][x] == ".":
            ans += 1
print(ans)
Problem p03436 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)

h, w = map(int, readline().split())
s = [input() for i in range(h)]
q = [(0, 0)]
g = (h - 1, w - 1)
test = [(0, 1), (0, -1), (1, 0), (-1, 0)]
inf = float("inf")
dist = [[inf] * w for _ in range(h)]
dist[0][0] = 0
cnt = 0
while 1:
    qq = []
    cnt += 1
    for y, x in q:
        for dy, dx in test:
            yy, xx = y + dy, x + dx
            if xx < 0 or yy < 0 or xx > w - 1 or yy > h - 1:
                continue
            if s[yy][xx] == "#" or dist[yy][xx] != inf:
                continue
            dist[yy][xx] = cnt
            qq.append((yy, xx))
    q = qq
    if g in q:
        break
    if q:
        continue
    else:
        print(-1)
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            cnt += 1
print(h * w - cnt - 1)


Fixed submission:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)

h, w = map(int, readline().split())
s = [input() for i in range(h)]
q = [(0, 0)]
g = (h - 1, w - 1)
test = [(0, 1), (0, -1), (1, 0), (-1, 0)]
inf = float("inf")
dist = [[inf] * w for _ in range(h)]
dist[0][0] = 0
cnt = 0
while 1:
    qq = []
    cnt += 1
    for y, x in q:
        for dy, dx in test:
            yy, xx = y + dy, x + dx
            if xx < 0 or yy < 0 or xx > w - 1 or yy > h - 1:
                continue
            if s[yy][xx] == "#" or dist[yy][xx] != inf:
                continue
            dist[yy][xx] = cnt
            qq.append((yy, xx))
    q = qq
    if g in q:
        break
    if q:
        continue
    else:
        print(-1)
        exit()
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            cnt += 1
print(h * w - cnt - 1)
Problem p03436 (Python)

Buggy submission (status: Runtime Error):
from collections import deque

H, W = map(int, input().split())
grid = [list(input()) for _ in range(H)]
dot_cnt = 0
for g in grid:
    dot_cnt += g.count(".")
sy = 0
sx = 0
gy = H - 1
gx = W - 1

deq = deque()
deq.append((0, 0))
grid[0][0] = 0

y_list = [-1, 1, 0, 0]
x_list = [0, 0, -1, 1]

while deq:
    y, x = deq.popleft()
    if y == gy and x == gx:
        break
    for i in range(len(y_list)):
        dy = y + y_list[i]
        dx = x + x_list[i]
        if dy < 0 or H <= dy or dx < 0 or W <= dx:
            continue
        if grid[dy][dx] == ".":
            deq.append((dy, dx))
            grid[dy][dx] = grid[y][x] + 1
print(dot_cnt - (grid[gy][gx] + 1))


Fixed submission:
from collections import deque

H, W = map(int, input().split())
grid = [list(input()) for _ in range(H)]
dot_cnt = 0
for g in grid:
    dot_cnt += g.count(".")
sy = 0
sx = 0
gy = H - 1
gx = W - 1

deq = deque()
deq.append((0, 0))
grid[0][0] = 0

y_list = [-1, 1, 0, 0]
x_list = [0, 0, -1, 1]

while deq:
    y, x = deq.popleft()
    if y == gy and x == gx:
        break
    for i in range(len(y_list)):
        dy = y + y_list[i]
        dx = x + x_list[i]
        if dy < 0 or H <= dy or dx < 0 or W <= dx:
            continue
        if grid[dy][dx] == ".":
            deq.append((dy, dx))
            grid[dy][dx] = grid[y][x] + 1
if grid[gy][gx] == ".":
    print(-1)
else:
    print(dot_cnt - (grid[gy][gx] + 1))
Problem p03436 (Python)

Buggy submission (status: Time Limit Exceeded):
from collections import deque


def bfs(start):
    q = deque([[start]])
    visited = set()
    while q:
        path = q.popleft()
        i, j = path[-1]
        if (i, j) == (H - 1, W - 1):
            return path
        for di, dj in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            ni, nj = i + di, j + dj
            if (
                0 <= ni < H
                and 0 <= nj < W
                and field[ni][nj] != "#"
                and (ni, nj) not in visited
            ):
                new_path = list(path) + [(ni, nj)]
                q.append(new_path)
                visited.add((i, j))


H, W = map(int, input().split())
field = [input() for _ in range(H)]
shortest_path = bfs((0, 0))
if shortest_path is None:
    print(-1)
    exit()
ans = 0
for i in range(H):
    for j in range(W):
        if field[i][j] == "." and (i, j) not in shortest_path:
            ans += 1
print(ans)


Fixed submission:
from collections import deque


def bfs(start):
    q = deque([[start]])
    visited = set()
    while q:
        path = q.popleft()
        i, j = path[-1]
        if (i, j) == (H - 1, W - 1):
            return path
        for di, dj in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            ni, nj = i + di, j + dj
            if (
                0 <= ni < H
                and 0 <= nj < W
                and field[ni][nj] != "#"
                and (ni, nj) not in visited
            ):
                q.append(path + [(ni, nj)])
                visited.add((ni, nj))


H, W = map(int, input().split())
field = [input() for _ in range(H)]
shortest_path = bfs((0, 0))
if shortest_path is None:
    print(-1)
    exit()
ans = 0
for i in range(H):
    for j in range(W):
        if field[i][j] == "." and (i, j) not in shortest_path:
            ans += 1
print(ans)
Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int H, W, N;
char mp[60][60];
int deep[60][60];
vector<pair<int, int>> xy = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void search(int x, int y) {
  if (x < 0 || x >= W || y < 0 || y >= H || mp[x][y] == '#')
    return;
  for (int i = 0; i < 4; i++) {
    if (deep[x][y] < deep[x + xy.at(i).first][y + xy.at(i).second]) {
      deep[x + xy.at(i).first][y + xy.at(i).second] = deep[x][y] + 1;
      search(x + xy.at(i).first, y + xy.at(i).second);
    }
  }
}

int main(void) {
  // Your code here!
  cin >> H >> W;
  int cnt = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> mp[j][i];
      if (mp[j][i] == '.')
        cnt++;
    }
  }
  for (int i = 0; i < 60; i++) {
    for (int j = 0; j < 60; j++) {
      deep[i][j] = 10000;
    }
  }
  deep[0][0] = 1;
  search(0, 0);
  int ans = cnt - deep[W - 1][H - 1];
  if (ans < 0) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int H, W, N;
char mp[60][60];
int deep[60][60];
vector<pair<int, int>> xy = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void search(int x, int y) {
  if (x < 0 || x >= W || y < 0 || y >= H || mp[x][y] == '#')
    return;
  for (int i = 0; i < 4; i++) {
    if (mp[x + xy.at(i).first][y + xy.at(i).second] == '.' &&
        deep[x][y] + 1 < deep[x + xy.at(i).first][y + xy.at(i).second]) {
      deep[x + xy.at(i).first][y + xy.at(i).second] = deep[x][y] + 1;
      search(x + xy.at(i).first, y + xy.at(i).second);
    }
  }
}

int main(void) {
  // Your code here!
  cin >> H >> W;
  int cnt = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> mp[j][i];
      if (mp[j][i] == '.')
        cnt++;
    }
  }
  for (int i = 0; i < 60; i++) {
    for (int j = 0; j < 60; j++) {
      deep[i][j] = 10000;
    }
  }
  deep[0][0] = 1;
  search(0, 0);
  int ans = cnt - deep[W - 1][H - 1];
  if (ans < 0) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}