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

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
const int mod = 1000000007, MAX = 405, INF = 1 << 30;
int dp[MAX][MAX][MAX];

int main() {

  std::ifstream in("text.txt");
  std::cin.rdbuf(in.rdbuf());
  cin.tie(0);
  ios::sync_with_stdio(false);

  int H, W;
  cin >> H >> W;
  vector<vector<int>> S(H, vector<int>(W));

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

  for (int s = 2; s <= H + W; s++) {
    for (int a = 1; a <= H; a++) {
      if (1 <= s - a && s - a <= W) {
        for (int b = 1; b <= H; b++) {
          if (1 <= s - b && s - b <= W) {
            if (a == b) {
              for (int bit = 0; bit < 4; bit++) {
                dp[s][a][b] =
                    max(dp[s][a][b], dp[s - 1][a - bit / 2][b - bit % 2] +
                                         S[a - 1][s - a - 1]);
              }
            } else {
              for (int bit = 0; bit < 4; bit++) {
                dp[s][a][b] = max(
                    dp[s][a][b], dp[s - 1][a - bit / 2][b - bit % 2] +
                                     S[a - 1][s - a - 1] + S[b - 1][s - b - 1]);
              }
            }
          }
        }
      }
    }
  }

  cout << dp[H + W][H][H] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
const int mod = 1000000007, MAX = 205, INF = 1 << 30;
int dp[2 * MAX][MAX][MAX];

int main() {

  std::ifstream in("text.txt");
  std::cin.rdbuf(in.rdbuf());
  cin.tie(0);
  ios::sync_with_stdio(false);

  int H, W;
  cin >> H >> W;
  vector<vector<int>> S(H, vector<int>(W));

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

  for (int s = 2; s <= H + W; s++) {
    for (int a = 1; a <= H; a++) {
      if (1 <= s - a && s - a <= W) {
        for (int b = 1; b <= H; b++) {
          if (1 <= s - b && s - b <= W) {
            if (a == b) {
              for (int bit = 0; bit < 4; bit++) {
                dp[s][a][b] =
                    max(dp[s][a][b], dp[s - 1][a - bit / 2][b - bit % 2] +
                                         S[a - 1][s - a - 1]);
              }
            } else {
              for (int bit = 0; bit < 4; bit++) {
                dp[s][a][b] = max(
                    dp[s][a][b], dp[s - 1][a - bit / 2][b - bit % 2] +
                                     S[a - 1][s - a - 1] + S[b - 1][s - b - 1]);
              }
            }
          }
        }
      }
    }
  }

  cout << dp[H + W][H][H] << endl;
}
Problem p03932 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

const int MN = 220;
int h, w;
ll g[MN][MN];

bool bc(int x, int y) { return (0 <= x && x < w && 0 <= y && y < h); }

ll dp[MN][MN][MN];
bool used[MN][MN][MN] = {};

ll solve(int d, int ax, int bx) {
  int ay = d - ax, by = d - bx;
  if (!bc(ax, ay) || !bc(bx, by))
    return -TEN(15);
  if (d == h + w - 2)
    return g[ay][ax];
  if (used[d][ax][bx])
    return dp[d][ax][bx];
  used[d][ax][bx] = true;
  ll &ans = dp[d][ax][bx];
  ans = -TEN(15);
  ans = max(ans, solve(d + 1, ax, bx));
  ans = max(ans, solve(d + 1, ax + 1, bx));
  ans = max(ans, solve(d + 1, ax, bx + 1));
  ans = max(ans, solve(d + 1, ax + 1, bx + 1));
  ans += g[ay][ax] + g[by][bx];
  if (ax == bx) {
    ans -= g[ay][ax];
  }
  return ans;
}

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> g[i][j];
    }
  }
  cout << solve(0, 0, 0) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

const int MN = 220;
int h, w;
ll g[MN][MN];

bool bc(int x, int y) { return (0 <= x && x < w && 0 <= y && y < h); }

ll dp[2 * MN][MN][MN];
bool used[2 * MN][MN][MN] = {};

ll solve(int d, int ax, int bx) {
  int ay = d - ax, by = d - bx;
  if (!bc(ax, ay) || !bc(bx, by))
    return -TEN(15);
  if (d == h + w - 2)
    return g[ay][ax];
  if (used[d][ax][bx])
    return dp[d][ax][bx];
  used[d][ax][bx] = true;
  ll &ans = dp[d][ax][bx];
  ans = -TEN(15);
  ans = max(ans, solve(d + 1, ax, bx));
  ans = max(ans, solve(d + 1, ax + 1, bx));
  ans = max(ans, solve(d + 1, ax, bx + 1));
  ans = max(ans, solve(d + 1, ax + 1, bx + 1));
  ans += g[ay][ax] + g[by][bx];
  if (ax == bx) {
    ans -= g[ay][ax];
  }
  return ans;
}

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> g[i][j];
    }
  }
  cout << solve(0, 0, 0) << endl;
  return 0;
}
Problem p03932 (C++)

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

int h, w, a1[410][410], a2[410][410], dp[410][410][410];

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

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      a2[i + j][i - j + w] = a1[i][j];
    }
  }

  dp[0][w][w] = a1[0][0];

  for (int i = 1; i <= h + w - 2; i++) {
    for (int j = 1; j <= h + w - 1; j++) {
      for (int k = 1; k <= h + w - 1; k++) {
        dp[i][j][k] = max({dp[i - 1][j - 1][k - 1], dp[i - 1][j - 1][k + 1],
                           dp[i - 1][j + 1][k - 1], dp[i - 1][j + 1][k + 1]});
        if (j == k) {
          dp[i][j][k] += a2[i][j];
        } else {
          dp[i][j][k] += a2[i][j] + a2[i][k];
        }
      }
    }
  }

  cout << dp[h + w - 2][h][h] << endl;
  return 0;
}

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

int h, w, a1[400][400], a2[400][400], dp[400][400][400];

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

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      a2[i + j][i - j + w] = a1[i][j];
    }
  }

  dp[0][w][w] = a1[0][0];

  for (int i = 1; i <= h + w - 2; i++) {
    for (int j = 1; j <= h + w - 1; j++) {
      for (int k = 1; k <= h + w - 1; k++) {
        dp[i][j][k] = max({dp[i - 1][j - 1][k - 1], dp[i - 1][j - 1][k + 1],
                           dp[i - 1][j + 1][k - 1], dp[i - 1][j + 1][k + 1]});
        if (j == k) {
          dp[i][j][k] += a2[i][j];
        } else {
          dp[i][j][k] += a2[i][j] + a2[i][k];
        }
      }
    }
  }

  cout << dp[h + w - 2][h][h] << endl;
  return 0;
}
Problem p03932 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define maxn 109
using namespace std;
int n, m;
int a[maxn][maxn];
long long dp[maxn][maxn][maxn];
int dx[] = {1, 0};
int dy[] = {0, 1};
const long long INF = 1e18;
bool in(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= m; }
long long dfs(int x, int y, int z) {
  if (!in(x, y) || !in(z, x + y - z))
    return -INF;
  if (x == n && y == m && z == n)
    return a[x][y];
  long long &ans = dp[x][y][z];
  if (ans != -1)
    return ans;
  int add;
  if (x == z)
    add = a[x][y];
  else
    add = a[x][y] + a[z][x + y - z];
  ans = 0;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      ans = max(ans, dfs(x + dx[i], y + dy[i], z + dx[j]));
    }
  }
  ans += add;
  return ans;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];
  memset(dp, -1, sizeof(dp));
  cout << dfs(1, 1, 1) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define maxn 209
using namespace std;
int n, m;
int a[maxn][maxn];
long long dp[maxn][maxn][maxn];
int dx[] = {1, 0};
int dy[] = {0, 1};
const long long INF = 1e18;
bool in(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= m; }
long long dfs(int x, int y, int z) {
  if (!in(x, y) || !in(z, x + y - z))
    return -INF;
  if (x == n && y == m && z == n)
    return a[x][y];
  long long &ans = dp[x][y][z];
  if (ans != -1)
    return ans;
  int add;
  if (x == z)
    add = a[x][y];
  else
    add = a[x][y] + a[z][x + y - z];
  ans = 0;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      ans = max(ans, dfs(x + dx[i], y + dy[i], z + dx[j]));
    }
  }
  ans += add;
  return ans;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];
  memset(dp, -1, sizeof(dp));
  cout << dfs(1, 1, 1) << endl;
  return 0;
}
Problem p03934 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

int main() {
  ios::sync_with_stdio(0);
  int n, q;
  cin >> n >> q;
  // cout << n << " " << q << endl;
  using P = pair<int, ll>;
  set<P> s;
  s.insert(P(-1, TEN(13)));
  s.insert(P(n - 1, 0));

  for (int i = 0; i < q; i++) {
    int a;
    ll b;
    cin >> a >> b;
    a--;
    while (b) {
      auto it = s.lower_bound(P(a, -1));
      auto pr = *it;
      it--;
      auto pl = *it;
      int d = a - pl.first;
      if (d == 0)
        return 0;
      if (a == pr.first)
        s.erase(pr);
      if (d * (pl.second - pr.second) <= b) {
        s.erase(pl);
        s.insert(P(a, pl.second));
        b -= d * (pl.second - pr.second);
      } else {
        s.insert(P(a, pr.second + b / d));
        if (b % d) {
          s.insert(P(pl.first + b % d, pr.second + b / d + 1));
        }
        if (pr.second + (b + d - 1) / d == pl.second) {
          s.erase(pl);
        }
        b = 0;
      }
    }
  }

  ll res[n];
  int b = -1;
  for (auto p : s) {
    for (int i = b + 1; i <= p.first; i++) {
      res[i] = p.second;
    }
    b = p.first;
  }

  for (int i = 0; i < n; i++) {
    cout << res[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

int main() {
  ios::sync_with_stdio(0);
  int n, q;
  cin >> n >> q;
  // cout << n << " " << q << endl;
  using P = pair<int, ll>;
  set<P> s;
  s.insert(P(-1, 3 * TEN(13)));
  s.insert(P(n - 1, 0));

  for (int i = 0; i < q; i++) {
    int a;
    ll b;
    cin >> a >> b;
    a--;
    while (b) {
      auto it = s.lower_bound(P(a, -1));
      auto pr = *it;
      it--;
      auto pl = *it;
      int d = a - pl.first;
      if (d == 0)
        return 0;
      if (a == pr.first)
        s.erase(pr);
      if (d * (pl.second - pr.second) <= b) {
        s.erase(pl);
        s.insert(P(a, pl.second));
        b -= d * (pl.second - pr.second);
      } else {
        s.insert(P(a, pr.second + b / d));
        if (b % d) {
          s.insert(P(pl.first + b % d, pr.second + b / d + 1));
        }
        if (pr.second + (b + d - 1) / d == pl.second) {
          s.erase(pl);
        }
        b = 0;
      }
    }
  }

  ll res[n];
  int b = -1;
  for (auto p : s) {
    for (int i = b + 1; i <= p.first; i++) {
      res[i] = p.second;
    }
    b = p.first;
  }

  for (int i = 0; i < n; i++) {
    cout << res[i] << endl;
  }
  return 0;
}
Problem p03934 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#define mp make_pair
using namespace std;

typedef long long LL;
typedef pair<int, LL> PIL;
typedef set<PIL>::iterator Iter;
int n, q;
set<PIL> S;

Iter pre(Iter x) {
  x--;
  return x;
}

void work() {
  scanf("%d %d", &n, &q);
  S.insert(mp(0, 1LL << 40)), S.insert(mp(1, 0)), S.insert(mp(n + 1, -1));
  for (int i = 1, a, l; i <= q; i++) {
    LL b;
    scanf("%d %lld", &a, &b), a++;
    Iter x = pre(S.upper_bound(mp(a, 1LL << 60))), y;
    x = (S.insert(mp(a, (*x).second))).first;
    while (b > 0) {
      y = pre(x), l = a - (*y).first;
      if (((*pre(y)).second - (*y).second) * l >= b) {
        if (b % l)
          S.insert(mp((*y).first + b % l, (*y).second + b / l));
        S.erase(y),
            y = S.insert(mp((*y).first, (*y).second + (b + l - 1) / l)).first;
        if ((*pre(y)).second == (*y).second)
          S.erase(y);
        b = 0;
      } else
        b -= ((*pre(y)).second - (*y).second) * l, S.erase(y);
    }
    if ((*pre(x)).second == (*x).second)
      S.erase(x);
  }
  for (Iter x = S.begin(), y; x != S.end(); y = x, x++)
    if ((*x).first > 1)
      for (int i = (*y).first; i < (*x).first; i++)
        printf("%lld\n", (*y).second);
}

int main() {
  work();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#define mp make_pair
using namespace std;

typedef long long LL;
typedef pair<int, LL> PIL;
typedef set<PIL>::iterator Iter;
int n, q;
set<PIL> S;

Iter pre(Iter x) {
  x--;
  return x;
}

void work() {
  scanf("%d %d", &n, &q);
  S.insert(mp(0, 1LL << 45)), S.insert(mp(1, 0)), S.insert(mp(n + 1, -1));
  for (int i = 1, a, l; i <= q; i++) {
    LL b;
    scanf("%d %lld", &a, &b), a++;
    Iter x = pre(S.upper_bound(mp(a, 1LL << 60))), y;
    x = (S.insert(mp(a, (*x).second))).first;
    while (b > 0) {
      y = pre(x), l = a - (*y).first;
      if (((*pre(y)).second - (*y).second) * l >= b) {
        if (b % l)
          S.insert(mp((*y).first + b % l, (*y).second + b / l));
        S.erase(y),
            y = S.insert(mp((*y).first, (*y).second + (b + l - 1) / l)).first;
        if ((*pre(y)).second == (*y).second)
          S.erase(y);
        b = 0;
      } else
        b -= ((*pre(y)).second - (*y).second) * l, S.erase(y);
    }
    if ((*pre(x)).second == (*x).second)
      S.erase(x);
  }
  for (Iter x = S.begin(), y; x != S.end(); y = x, x++)
    if ((*x).first > 1)
      for (int i = (*y).first; i < (*x).first; i++)
        printf("%lld\n", (*y).second);
}

int main() {
  work();
  return 0;
}
Problem p03935 (C++)

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

const int mod = 998244353;

int64_t mul(int64_t x, int64_t y) { return (x % mod) * (y % mod) % mod; }

int fib(long long n, int64_t a = 0, int64_t b = 1, int64_t c = 1, int a0 = 0,
        int a1 = 1) {
  if (n == 0)
    return a0;
  int na0 = n & 1 ? (a * a0 + b * a1) % mod : a0;
  int na1 = n & 1 ? (b * a0 + c * a1) % mod : a1;
  return fib(n >> 1, (a * a + b * b) % mod, b * (a + c) % mod,
             (b * b + c * c) % mod, na0, na1);
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> inv(1010101);
  inv[1] = 1;
  for (int i = 2; i < 1010101; i++)
    inv[i] = mul(inv[mod % i], mod - mod / i);
  int ans = fib(m + 2 * (n - 1));
  int C = 1;
  for (int i = 0; i <= n - 2; i++) {
    ans += mul(C, mod - fib(n * 2 - 2 - 2 * i));
    ans %= mod;
    C = mul(C, mul(m - 1 + i + 1, inv[i + 1]));
  }
  cout << ans << endl;
}


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

const int mod = 998244353;

int64_t mul(int64_t x, int64_t y) { return (x % mod) * (y % mod) % mod; }

int fib(long long n, int64_t a = 0, int64_t b = 1, int64_t c = 1, int a0 = 0,
        int a1 = 1) {
  if (n == 0)
    return a0;
  int na0 = n & 1 ? (a * a0 + b * a1) % mod : a0;
  int na1 = n & 1 ? (b * a0 + c * a1) % mod : a1;
  return fib(n >> 1, (a * a + b * b) % mod, b * (a + c) % mod,
             (b * b + c * c) % mod, na0, na1);
}

int main() {
  int n;
  int64_t m;
  cin >> n >> m;
  vector<int> inv(1010101);
  inv[1] = 1;
  for (int i = 2; i < 1010101; i++)
    inv[i] = mul(inv[mod % i], mod - mod / i);
  int ans = fib(m + 2 * (n - 1));
  int C = 1;
  for (int i = 0; i <= n - 2; i++) {
    ans += mul(C, mod - fib(n * 2 - 2 - 2 * i));
    ans %= mod;
    C = mul(C, mul(m - 1 + i + 1, inv[i + 1]));
  }
  cout << ans << endl;
}
Problem p03937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
  int H, W;
  cin >> H >> W;
  vector<string> A(H);
  int i = 0;
  int n = 0;
  for (i = 0; i < H; i++) {
    cin >> A[i];
    int j = 0;
    while (A[i][j] != '\0') {
      if (A[i][j] == '#') {
        n++;
      }
    }
  }
  if (n == H + W - 1) {
    cout << "Possible" << endl;
    return 0;
  } else {
    cout << "Impossible" << endl;
    return 0;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
  int H, W;
  cin >> H >> W;
  vector<string> A(H);
  int i = 0;
  int n = 0;
  for (i = 0; i < H; i++) {
    cin >> A[i];
    int j = 0;
    while (A[i][j] != '\0') {
      if (A[i][j] == '#') {
        n++;
      }
      j++;
    }
  }
  if (n == H + W - 1) {
    cout << "Possible" << endl;
    return 0;
  } else {
    cout << "Impossible" << endl;
    return 0;
  }
  return 0;
}
Problem p03937 (C++)

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

int main() {
  int h, w;
  cin >> h >> w;

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

  int posi = 0, posj = 0;
  bool proceed = true;
  while (proceed && posi != h - 1 || posj != w - 1) {
    proceed = false;

    while (posj < w - 1 && mat[posi][posj + 1] == '#') {
      proceed = true;
      mat[posi][posj] = '.';
      posj++;
    }

    while (posi < h - 1 && mat[posi + 1][posj] == '#') {
      proceed = true;
      mat[posi][posj] = '.';
      posi++;
    }
  }

  if (posi != h - 1 || posj != w - 1) {
    cout << "Impossible" << endl;
  } else {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (i + j == 0 || i + j == h + w - 2)
          continue;
        if (mat[i][j] == '#') {
          cout << "Impossible" << endl;
          return 0;
        }
      }
    }

    cout << "Possible" << endl;
  }

  return 0;
}

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

int main() {
  int h, w;
  cin >> h >> w;

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

  int posi = 0, posj = 0;
  bool proceed = true;
  while (proceed && (posi != h - 1 || posj != w - 1)) {
    proceed = false;

    while (posj < w - 1 && mat[posi][posj + 1] == '#') {
      proceed = true;
      mat[posi][posj] = '.';
      posj++;
    }

    while (posi < h - 1 && mat[posi + 1][posj] == '#') {
      proceed = true;
      mat[posi][posj] = '.';
      posi++;
    }
  }

  if (posi != h - 1 || posj != w - 1) {
    cout << "Impossible" << endl;
  } else {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (i + j == 0 || i + j == h + w - 2)
          continue;
        if (mat[i][j] == '#') {
          cout << "Impossible" << endl;
          return 0;
        }
      }
    }

    cout << "Possible" << endl;
  }

  return 0;
}
Problem p03937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 10
using namespace std;
char s[N][N];
int H, W;
int main() {
  cin >> H >> W;
  for (int i = 1; i <= H; i++)
    scanf("%s", s[i] + 1);
  int lastr = 0, lastc = 1;
  for (int i = 2; i <= H + W; i++) {
    int num = 0;
    for (int r = 1; r <= min(i - 1, H); r++) {
      int c = i - r;
      if (c > W || s[r][c] != '#')
        continue;
      if (num == 0 && (abs(lastr - r) + abs(lastc - c) == 1))
        lastr = r, lastc = c, num++;
      else
        return puts("Impossible"), 1;
    }
  }
  puts("Possible");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 10
using namespace std;
char s[N][N];
int H, W;
int main() {
  cin >> H >> W;
  for (int i = 1; i <= H; i++)
    scanf("%s", s[i] + 1);
  int lastr = 0, lastc = 1;
  for (int i = 2; i <= H + W; i++) {
    int num = 0;
    for (int r = 1; r <= min(i - 1, H); r++) {
      int c = i - r;
      if (c > W || s[r][c] != '#')
        continue;
      if (num == 0 && (abs(lastr - r) + abs(lastc - c) == 1))
        lastr = r, lastc = c, num++;
      else
        return puts("Impossible"), 0;
    }
  }
  puts("Possible");
  return 0;
}
Problem p03937 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> Map(H + 1, vector<char>(W + 1));
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> Map[i][j];
  for (int i = 0; i < H; i++)
    Map[i][W] = '.';
  for (int i = 0; i < W; i++)
    Map[H][i] = '.';
  int y = 0, x = 0;
  if (Map[H - 2][W - 1] == '#' && Map[H - 1][W - 2] == '#') {
    cout << "Impossible" << endl;
    return 0;
  }
  bool ans = true;
  while (y != H - 1 || x != W - 1) {
    if ((Map[y][x + 1] == '#' && Map[y + 1][x] == '#') ||
        (Map[y][x + 1] == '.' && Map[y + 1][x] == '.') ||
        (Map[y][x - 1] == '#' && Map[y - 1][x] == '#')) {
      ans = false;
      break;
    }
    if (Map[y][x + 1] == '#')
      x++;
    else
      y++;
  }
  cout << (ans ? "Possible" : "Impossible") << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> Map(H + 1, vector<char>(W + 1));
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> Map[i][j];
  for (int i = 0; i < H; i++)
    Map[i][W] = '.';
  for (int i = 0; i < W; i++)
    Map[H][i] = '.';
  int y = 0, x = 0;
  if (Map[H - 2][W - 1] == '#' && Map[H - 1][W - 2] == '#') {
    cout << "Impossible" << endl;
    return 0;
  }
  bool ans = true;
  while (y != H - 1 || x != W - 1) {
    if ((Map[y][x + 1] == '#' && Map[y + 1][x] == '#') ||
        (Map[y][x + 1] == '.' && Map[y + 1][x] == '.') ||
        (x > 0 && y > 0 && Map[y][x - 1] == '#' && Map[y - 1][x] == '#')) {
      ans = false;
      break;
    }
    if (Map[y][x + 1] == '#')
      x++;
    else
      y++;
  }
  cout << (ans ? "Possible" : "Impossible") << endl;
}
Problem p03937 (C++)

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

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define pi M_PI

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int main() {
  int w, h;
  cin >> w >> h;
  vector<string> s(w + 1);
  REP(i, w) cin >> s[i];
  REP(i, w) s[i].push_back('.');

  int x = 0;
  REP(i, w) REP(j, h) x += (s[i][j] == '#');

  int i = 0, j = 0, y = 1;
  while (i < w - 1 || j < h - 1) {
    y++;
    if (s[i + 1][j] == '#') {
      i++;
      continue;
    }
    if (s[i][j + 1] == '#') {
      j++;
      continue;
    }
    cout << "Impossible" << endl;
  }

  if (x == y)
    cout << "Possible" << endl;
  else
    cout << "Impossible" << endl;

  return 0;
}

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

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define pi M_PI

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int main() {
  int w, h;
  cin >> w >> h;
  vector<string> s(w + 1);
  REP(i, w) cin >> s[i];
  REP(i, w) s[i].push_back('.');

  int x = 0;
  REP(i, w) REP(j, h) x += (s[i][j] == '#');

  int i = 0, j = 0, y = 1;
  while (i < w - 1 || j < h - 1) {
    y++;
    if (s[i + 1][j] == '#') {
      i++;
      continue;
    }
    if (s[i][j + 1] == '#') {
      j++;
      continue;
    }
    cout << "Impossible" << endl;
    return 0;
  }

  if (x == y)
    cout << "Possible" << endl;
  else
    cout << "Impossible" << endl;

  return 0;
}
Problem p03937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int MOD = (int)1e9 + 7;
int INF = (int)2e18;
// Comment out below and program paste
// #include "UnionFind.h"
// #include "power.h"

int H, W;
vector<vector<char>> A(8, vector<char>(8, '.'));
vector<vector<bool>> check(8, vector<bool>(8, false));

int ans = 0;
void dfs(int x, int y, int z) {
  check[x][y] = true;
  if (A[x + 1][y] == '#')
    dfs(x + 1, y, z + 1);
  if (A[x][y + 1] == '#')
    dfs(x, y + 1, z + 1);
  ans = max(ans, z);
}

signed main() {
  cin >> H >> W;
  int c = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> A[i][j];
      if (A[i][j] == '#')
        ++c;
    }
  }
  dfs(0, 0, 1);
  if (ans == c)
    cout << "Possible" << endl;
  else
    cout << "Impossible" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int MOD = (int)1e9 + 7;
int INF = (int)2e18;
// Comment out below and program paste
// #include "UnionFind.h"
// #include "power.h"

int H, W;
vector<vector<char>> A(10, vector<char>(10, '.'));
vector<vector<bool>> check(10, vector<bool>(10, false));

int ans = 0;
void dfs(int x, int y, int z) {
  check[x][y] = true;
  if (A[x + 1][y] == '#')
    dfs(x + 1, y, z + 1);
  if (A[x][y + 1] == '#')
    dfs(x, y + 1, z + 1);
  ans = max(ans, z);
}

signed main() {
  cin >> H >> W;
  int c = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> A[i][j];
      if (A[i][j] == '#')
        ++c;
    }
  }
  dfs(0, 0, 1);
  if (ans == c)
    cout << "Possible" << endl;
  else
    cout << "Impossible" << endl;
}
Problem p03937 (C++)

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

int main() {
  int h, w;
  cin >> h >> w;
  string graph[h];
  for (int i = 0; i < h; i++) {
    cin >> graph[i];
  }
  pair<int, int> current;
  while (current != make_pair(h - 1, w - 1)) {
    graph[current.first][current.second] = '.';
    if (current.first + 1 < h &&
        graph[current.first + 1][current.second] == '#') {
      current.first++;
    } else if (current.second + 1 < w &&
               graph[current.first][current.second + 1] == '#') {
      current.second++;
    }
  }
  graph[h - 1][w - 1] = '.';
  bool result = true;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (graph[i][j] == '#') {
        result = false;
      }
    }
  }
  if (result) {
    cout << "Possible";
  } else {
    cout << "Impossible";
  }
  return 0;
}

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

int main() {
  int h, w;
  cin >> h >> w;
  string graph[h];
  for (int i = 0; i < h; i++) {
    cin >> graph[i];
  }
  pair<int, int> current;
  while (current != make_pair(h - 1, w - 1)) {
    graph[current.first][current.second] = '.';
    if (current.first + 1 < h &&
        graph[current.first + 1][current.second] == '#') {
      current.first++;
    } else if (current.second + 1 < w &&
               graph[current.first][current.second + 1] == '#') {
      current.second++;
    } else {
      break;
    }
  }
  graph[h - 1][w - 1] = '.';
  bool result = true;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (graph[i][j] == '#') {
        result = false;
      }
    }
  }
  if (result) {
    cout << "Possible";
  } else {
    cout << "Impossible";
  }
  return 0;
}
Problem p03937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> A(H + 2, vector<char>(W + 2, '.'));
  A.at(0).at(1) = '#';
  A.at(H + 1).at(W) = '#';
  for (int i = 1; i <= H; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= W; j++)
      cin >> s.at(j - 1);
  }
  vector<vector<int>> B(H + 2, vector<int>(W + 2, 2));
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (A.at(i).at(j) == '.')
        B.at(i).at(j) = 2;
      else {
        int a = 0;
        if ((A.at(i - 1).at(j) == '#' && A.at(i).at(j - 1) == '.') ||
            (A.at(i - 1).at(j) == '.' && A.at(i).at(j - 1) == '#'))
          a++;
        if ((A.at(i + 1).at(j) == '#' && A.at(i).at(j + 1) == '.') ||
            (A.at(i + 1).at(j) == '.' && A.at(i).at(j + 1) == '#'))
          a++;
        B.at(i).at(j) = a;
      }
    }
  }
  bool x = true;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++)
      if (B.at(i).at(j) != 2) {
        x = false;
        break;
      }
  }
  if (x)
    cout << "Possible" << endl;
  else
    cout << "Impossible" << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> A(H + 2, vector<char>(W + 2, '.'));
  A.at(0).at(1) = '#';
  A.at(H + 1).at(W) = '#';
  for (int i = 1; i <= H; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= W; j++)
      A.at(i).at(j) = s.at(j - 1);
  }
  vector<vector<int>> B(H + 2, vector<int>(W + 2, 2));
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (A.at(i).at(j) == '.')
        B.at(i).at(j) = 2;
      else {
        int a = 0;
        if ((A.at(i - 1).at(j) == '#' && A.at(i).at(j - 1) == '.') ||
            (A.at(i - 1).at(j) == '.' && A.at(i).at(j - 1) == '#'))
          a++;
        if ((A.at(i + 1).at(j) == '#' && A.at(i).at(j + 1) == '.') ||
            (A.at(i + 1).at(j) == '.' && A.at(i).at(j + 1) == '#'))
          a++;
        B.at(i).at(j) = a;
      }
    }
  }
  bool x = true;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++)
      if (B.at(i).at(j) != 2) {
        x = false;
        break;
      }
  }
  if (x)
    cout << "Possible" << endl;
  else
    cout << "Impossible" << endl;
  return 0;
}
Problem p03937 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define IFOR(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define RFOR(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)

using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> data(h + 1);
  FOR(i, 0, h) {
    cin >> data[i];
    data[i].push_back('.');
  }
  data[h].resize(w + 1);
  FOR(i, 0, w + 1) { data[h][i] = '.'; }
  if (data[0][0] == '.' || data[h - 1][w - 1] == '.') {
    cout << "Impossible" << endl;
    return 0;
  }
  int posy = 0, posx = 0;
  bool res = true;
  while (posx != w - 1 || posy != h - 1) {
    if ((posx > 0 && data[posy][posx - 1] == '#') ||
        (posy > 0 && data[posy - 1][posx] == '#')) {
      res = false;
      break;
    }
    if (data[posy + 1][posx] == data[posy][posx + 1]) {
      res = false;
      break;
    }
    data[posy][posx] = '.';

    if (data[posy + 1][posx] == '#') {
      posy++;
    } else
      posx++;
  }
  if (data[posy - 1][posx] == '#' || data[posy][posx - 1] == '#')
    res = false;
  cout << (res ? "Possible" : "Impossible") << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define IFOR(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define RFOR(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)

using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> data(h + 1);
  FOR(i, 0, h) {
    cin >> data[i];
    data[i].push_back('.');
  }
  data[h].resize(w + 1);
  FOR(i, 0, w + 1) { data[h][i] = '.'; }
  if (data[0][0] == '.' || data[h - 1][w - 1] == '.') {
    cout << "Impossible" << endl;
    return 0;
  }
  int posy = 0, posx = 0;
  bool res = true;
  while (posx != w - 1 || posy != h - 1) {
    if ((posx > 0 && data[posy][posx - 1] == '#') ||
        (posy > 0 && data[posy - 1][posx] == '#')) {
      res = false;
      break;
    }
    if (data[posy + 1][posx] == data[posy][posx + 1]) {
      res = false;
      break;
    }
    data[posy][posx] = '.';

    if (data[posy + 1][posx] == '#') {
      posy++;
    } else
      posx++;
  }
  if (res && (data[posy - 1][posx] == '#' || data[posy][posx - 1] == '#'))
    res = false;
  cout << (res ? "Possible" : "Impossible") << endl;
  return 0;
}
Problem p03937 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};
vi dir2 = {0, 1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  vector<vi> g(h + 2, vi(w + 2, -1));
  rep2(i, 1, h + 1) rep2(j, 1, w + 1) {
    char c;
    cin >> c;
    if (c == '#')
      g[i][j] = 0;
  }
  vi v;
  rep(i, h - 1) v.push_back(i * 2);
  rep(j, w - 1) v.push_back(j * 2 + 1);
  sort(all(v));
  bool ans = false;
  do {
    int i = 1, j = 1;
    vector<vi> gg = g;
    gg[1][1] = 1;
    bool ok = true;
    for (auto k : v) {
      if (k % 2)
        j++;
      else
        i++;
      if (gg[i][j] == -1)
        ok = false;
      else
        gg[i][j] = 1;
    }
    rep2(ih, 1, h + 1) rep2(jw, 1, w + 1) if (gg[ih][jw] == 0) ok = false;
    if (ok)
      ans = true;
  } while (next_permutation(all(v)));
  cout << (ans ? "Possible" : "Impossible") << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};
vi dir2 = {0, 1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  vector<vi> g(h + 2, vi(w + 2, -1));
  rep2(i, 1, h + 1) rep2(j, 1, w + 1) {
    char c;
    cin >> c;
    if (c == '#')
      g[i][j] = 0;
  }
  vi v;
  rep(i, h - 1) v.push_back(0);
  rep(j, w - 1) v.push_back(1);
  sort(all(v));
  bool ans = false;
  do {
    int i = 1, j = 1;
    vector<vi> gg = g;
    gg[1][1] = 1;
    bool ok = true;
    for (auto k : v) {
      if (k % 2)
        j++;
      else
        i++;
      if (gg[i][j] == -1)
        ok = false;
      else
        gg[i][j] = 1;
    }
    rep2(ih, 1, h + 1) rep2(jw, 1, w + 1) if (gg[ih][jw] == 0) ok = false;
    if (ok)
      ans = true;
  } while (next_permutation(all(v)));
  cout << (ans ? "Possible" : "Impossible") << endl;
}
Problem p03937 (C++)

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

using namespace std;

const long long MOD = 1000000007;

int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); }

int main() {

  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  freopen("input.txt", "r", stdin);

  int n, m, k = 0;
  cin >> n >> m;

  string cur;

  for (int i = 0; i < n; ++i) {
    cin >> cur;

    /*

    // Forgot to account the case where you go down but go back up again

    if(i > 0){
        for(int j = 1; j < m; ++j){
            if(prev[j] == '#' && cur[j] == '#' && cur[j - 1] == '#'){
                cout << "Impossible";
                return 0;
            }
        }
    }*/

    for (auto j : cur) {
      if (j == '#')
        ++k;
    }

    // prev = cur;
  }

  if (k == n + m - 1)
    cout << "Possible";
  else
    cout << "Impossible";

  return 0;
}


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

using namespace std;

const long long MOD = 1000000007;

int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); }

int main() {

  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  // freopen("input.txt", "r", stdin);

  int n, m, k = 0;
  cin >> n >> m;

  string cur;

  for (int i = 0; i < n; ++i) {
    cin >> cur;

    /*

    // Forgot to account the case where you go down but go back up again

    if(i > 0){
        for(int j = 1; j < m; ++j){
            if(prev[j] == '#' && cur[j] == '#' && cur[j - 1] == '#'){
                cout << "Impossible";
                return 0;
            }
        }
    }*/

    for (auto j : cur) {
      if (j == '#')
        ++k;
    }

    // prev = cur;
  }

  if (k == n + m - 1)
    cout << "Possible";
  else
    cout << "Impossible";

  return 0;
}
Problem p03937 (C++)

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

int h, w;
bool arr[11][11];
bool left[11][11];
int dx[] = {1, 0}, dy[] = {0, 1};

void func(int y, int x) {
  if (y == h && w == x) {
    return;
  }

  left[y][x] = true;
  for (int i = 0; i < 4; ++i) {
    int ny = y + dy[i], nx = x + dx[i];
    if (ny < 0 || ny >= h || nx < 0 || nx >= w) {
      continue;
    }

    if (arr[ny][nx] && !left[ny][nx]) {
      func(ny, nx);

      return;
    }
  }
}

int main() {
  std::cin >> h >> w;

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      char c;
      std::cin >> c;

      arr[i][j] = c == '#';
    }
  }

  func(0, 0);

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (arr[i][j] && !left[i][j]) {
        std::cout << "Impossible" << std::endl;

        return 0;
      }
    }
  }

  std::cout << "Possible" << std::endl;
}

Fixed submission:
#include <iostream>
#include <set>
#include <utility>

int h, w;
bool arr[11][11];
bool left[11][11];
int dx[] = {1, 0}, dy[] = {0, 1};

void func(int y, int x) {
  if (y == h && w == x) {
    return;
  }

  left[y][x] = true;
  for (int i = 0; i < 2; ++i) {
    int ny = y + dy[i], nx = x + dx[i];
    if (ny < 0 || ny >= h || nx < 0 || nx >= w) {
      continue;
    }

    if (arr[ny][nx] && !left[ny][nx]) {
      func(ny, nx);

      return;
    }
  }
}

int main() {
  std::cin >> h >> w;

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      char c;
      std::cin >> c;

      arr[i][j] = c == '#';
    }
  }

  func(0, 0);

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (arr[i][j] && !left[i][j]) {
        std::cout << "Impossible" << std::endl;

        return 0;
      }
    }
  }

  std::cout << "Possible" << std::endl;
}
Problem p03937 (C++)

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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int H, W;

string S[101];

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> H >> W;
  FOR(y, H) cin >> S[y];

  y = x = 0;
  while (1) {
    S[y][x] = '.';
    if (y == H - 1 && x == W - 1)
      break;
    if (x < W - 1 && S[y][x + 1] == '#') {
      x++;
    } else if (y < H - 1 && S[y + 1][x] == '#') {
      y++;
    } else {
      _P("Impossible\n");
    }
  }
  FOR(y, H) FOR(x, W) if (S[y][x] == '#') return _P("Impossible\n");

  _P("Possible\n");
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false);
  FOR(i, argc - 1) s += argv[i + 1], s += '\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  solve();
  return 0;
}


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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int H, W;

string S[101];

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> H >> W;
  FOR(y, H) cin >> S[y];

  y = x = 0;
  while (1) {
    S[y][x] = '.';
    if (y == H - 1 && x == W - 1)
      break;
    if (x < W - 1 && S[y][x + 1] == '#') {
      x++;
    } else if (y < H - 1 && S[y + 1][x] == '#') {
      y++;
    } else {
      return _P("Impossible\n");
    }
  }
  FOR(y, H) FOR(x, W) if (S[y][x] == '#') return _P("Impossible\n");

  _P("Possible\n");
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false);
  FOR(i, argc - 1) s += argv[i + 1], s += '\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  solve();
  return 0;
}
Problem p03937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  string s[10];
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#') {
        if (i != h - 1 && j != w - 1) {
          if (s[i + 1][j] == '#' && s[i][j + 1] == '#' ||
              s[i + 1][j] != '#' && s[i][j + 1] != '#') {
            cout << "Impossible" << endl;
            return (0);
          }
        }
      }
      if (w != 0 && h != 0) {
        if (s[i - 1][j] == '#' && s[i][j - 1] == '#') {
          cout << "Impossible" << endl;
          return (0);
        }
      }
    }
  }
  cout << "Possible" << endl;
  return (0);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  string s[10];
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#') {
        if (i != h - 1 && j != w - 1) {
          if (s[i + 1][j] == '#' && s[i][j + 1] == '#' ||
              s[i + 1][j] != '#' && s[i][j + 1] != '#') {
            cout << "Impossible" << endl;
            return (0);
          }
        }
      }
      if (i != 0 && j != 0) {
        if (s[i - 1][j] == '#' && s[i][j - 1] == '#') {
          cout << "Impossible" << endl;
          return (0);
        }
      }
    }
  }
  cout << "Possible" << endl;
  return (0);
}
Problem p03937 (C++)

Buggy submission (status: Runtime Error):
/**
 *    author:  yuya1234
 *    created: 14.07.2020 11:02:45
 **/

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)

#define SORT(s) sort((s).begin(), (s).end())
#define SORTD(s) sort((s).rbegin(), (s).rend())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define SZ(x) ((int)(x).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(15);

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

  string a[h];
  REP(i, h) cin >> a[i];

  int cnt = 0;
  REP(i, h) REP(j, w) {
    if (a[i][j] == '#')
      cnt++;
  }

  REP(i, h) FOR(j, i, w) {
    if (a[i][j] == '#') {
      if (i == h - 1 && j == w - 1)
        cnt--;
      else if (i < h && a[i + 1][j] == '#') {
        cnt--;
        break;
      } else if (j < w && a[i][j + 1] == '#') {
        cnt--;
      } else {
        i = h;
        j = w;
      }
    } else {
      i = h;
      j = w;
    }
  }

  if (cnt == 0)
    cout << "Possible" << endl;
  else
    cout << "Impossible" << endl;

  return 0;
}

Fixed submission:
/**
 *    author:  yuya1234
 *    created: 14.07.2020 11:02:45
 **/

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)

#define SORT(s) sort((s).begin(), (s).end())
#define SORTD(s) sort((s).rbegin(), (s).rend())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define SZ(x) ((int)(x).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(15);

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

  string a[h];
  REP(i, h) cin >> a[i];

  int cnt = 0;
  REP(i, h) REP(j, w) {
    if (a[i][j] == '#')
      cnt++;
  }

  if (cnt == h + w - 1)
    cout << "Possible" << endl;
  else
    cout << "Impossible" << endl;

  return 0;
}