alexjercan/bugnet huggingface dataset:
Problem p00203 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#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 FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 30;

int main() {

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

  while (cin >> X >> Y, X || Y) {
    int ba[X][Y];
    REP(y, Y) {
      REP(x, X) { cin >> ba[x][y]; }
    }
    int dp[X][Y];
    memset(dp, 0, sizeof(dp));
    REP(i, X)
    if (ba[i][0] == 0)
      dp[i][0] = 1;
    REP(y, Y - 1) {
      REP(x, X) {
        if (ba[x][y] == 0) {
          REP(i, 3) {
            int xx = x + dx[i];
            int yy = y + 1;
            if (xx < 0 || X <= xx)
              continue;

            if (ba[xx][yy] == 0 || ba[xx][yy] == 2 && i == 0) {
              dp[xx][yy] += dp[x][y];
            }
          }
        } else if (ba[x][y] == 2) {
          if (y + 2 < Y && ba[x][y + 2] != 1)
            dp[x][y + 2] = dp[x][y];
        }
      }
    }
    // REP(y,Y) {
    //   REP(x,X) {
    //     cout << dp[x][y] << " ";
    //   }
    //   cout << endl;
    // }

    int ans = 0;
    REP(x, X) {
      ans += dp[x][Y - 1];
      if (ba[x][Y - 2] == 2)
        ans += dp[x][Y - 2];
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

// ツ堕スツ閉ェDPツつキツづェツづ篠余ツ裕ツつセツつェツ・ツ・ツ・

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#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 FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 30;

int X, Y;
int ba[15][15];
map<pair<int, int>, int> mp;

int const dx[] = {-1, 0, 1};
int solve(int x, int y) {
  if (y >= Y - 1)
    return 1;
  if (mp[make_pair(x, y)])
    return mp[make_pair(x, y)];
  int res = 0;
  REP(i, 3) {
    int xx = x + dx[i];
    int yy = y + 1;

    if (xx < 0 || X <= xx)
      continue;
    if (i == 1) {
      while (yy < Y && ba[xx][yy] == 2) {
        yy += 2;
      }
      if (yy >= Y || ba[xx][yy] == 0)
        res += solve(xx, yy);
    } else if (ba[xx][yy] == 0) {
      res += solve(xx, yy);
    }
  }
  return mp[make_pair(x, y)] = res;
}

int main() {
  while (cin >> X >> Y, X || Y) {
    mp.clear();
    REP(y, Y) {
      REP(x, X) { cin >> ba[x][y]; }
    }
    int ans = 0;
    REP(x, X) { ans += solve(x, 0); }
    cout << ans << endl;
  }
}
Problem p00203 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  int n, m;
  int masu[17][17];
  int ura[17][17];

  while (cin >> m >> n, n != 0 && m != 0) {
    for (int i = 0; i < 17; i++) {
      for (int j = 0; j < 17; j++) {
        masu[i][j] = ura[i][j] = 0;
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> masu[i][j];
      }
    }

    for (int j = 1; j <= m; j++) {
      if (masu[0][j] != 1) {
        ura[0][j] = 1;
      }
    }

    for (int i = 0; i < n - 1; i++) {
      for (int j = 1; j <= m; j++) {
        if (masu[i][j] == 0) {
          if (masu[i + 1][j - 1] == 0) {
            ura[i + 1][j - 1] += ura[i][j];
          }
          if (masu[i + 1][j] != 1) {
            ura[i + 1][j] += ura[i][j];
          }
          if (masu[i + 1][j + 1] == 0) {
            ura[i + 1][j + 1] += ura[i][j];
          }
        }

        if (masu[i][j] == 2) {
          if (masu[i + 2][j] != 1) {
            ura[i + 2][j] += ura[i][j];
          }
        }
      }
    }
    int cont = 0;
    for (int i = 1; i <= m; i++) {
      cont += ura[n][i] + ura[n - 1][i];
    }

    /*

    for(int i=0;i<17;i++){
            for(int j=0;j<17;j++){
                    printf("%2d ",ura[i][j]);
            }
            puts("");
    }*/
    printf("%d\\n", cont);
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int masu[17][17];

int count;
int n, m;
void saiki(int x, int y) {

  if (y < 1 || y > n) {
    return;
  }
  // printf("make %d %d\\n",x,y);
  if (x >= m) {
    // printf("ans %d %d\\n",x,y);
    count++;
    return;
  }

  if (masu[x][y] == 0) {
    if (masu[x + 1][y - 1] == 0 && x <= m - 2) {
      saiki(x + 1, y - 1);
    }
    if (masu[x + 1][y] == 0) {
      saiki(x + 1, y);
    }
    if (masu[x + 1][y] == 2) {
      saiki(x + 1, y);
    }
    if (masu[x + 1][y + 1] == 0 && x <= m - 2) {
      saiki(x + 1, y + 1);
    }
  }
  if (masu[x][y] == 1) {
  }
  if (masu[x][y] == 2) {
    if (masu[x + 2][y] != 1) {
      saiki(x + 2, y);
    }
  }

  // printf("--- %d %d\\n",x,y);
}

int main() {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < 17; i++) {
      for (int j = 0; j < 17; j++) {
        masu[i][j] = 0;
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> masu[i][j];
      }
    }

    count = 0;

    for (int i = 1; i <= n; i++) {
      saiki(0, i);
    }

    printf("%d\\n", count);
  }
}
Problem p00203 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

int memo[20][20];
int x, y;

int calc(std::vector<std::vector<int>> &v, int x, int y) {

  int res = 0;
  if (memo[x][y] != -1)
    return memo[x][y];
  if ((x > v.size() - 1) || (x == v.size() - 1 && v[x][y] != 1))
    return 1;

  if (v[x][y] == 2)
    res += calc(v, x + 2, y);

  if (v[x][y] == 0) {
    if (v[x + 1][y] == 0 || v[x + 1][y] == 2)
      res += calc(v, x + 1, y);

    if (y < v[x].size() - 1 && v[x + 1][y + 1] == 0)
      res += calc(v, x + 1, y + 1);

    if (y > 0 && v[x + 1][y - 1] == 0)
      res += calc(v, x + 1, y - 1);
  }

  return memo[x][y] = res;
}

int main() {

  while (std::cin >> x >> y) {

    if (x == 0 && y == 0)
      break;

    int res = 0;
    std::vector<std::vector<int>> v(y, std::vector<int>(x));

    for (int i = 0; i < 20; i++) {
      for (int j = 0; j < 20; j++) {
        memo[i][j] = -1;
      }
    }

    for (int i = 0; i < y; i++) {
      for (int j = 0; j < x; j++) {
        std::cin >> v[i][j];
      }
    }

    for (int i = 0; i < v[0].size(); i++)
      res += calc(v, 0, i);

    std::cout << res << std::endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

int memo[16][16];

int calc(std::vector<std::vector<int>> &v, int x, int y) {

  if (memo[x][y] != -1)
    return memo[x][y];
  int res = 0;
  if (x >= v[y].size() - 1)
    return 1;

  if (v[x][y] == 2)
    res += calc(v, x + 2, y);

  if (v[x][y] == 0) {
    if (v[x + 1][y] == 0)
      res += calc(v, x + 1, y);
    else if (v[x + 1][y] == 2)
      res += calc(v, x + 1, y);

    if (y < v.size() - 1 && v[x + 1][y + 1] == 0)
      res += calc(v, x + 1, y + 1);

    if (y > 0 && v[x + 1][y - 1] == 0)
      res += calc(v, x + 1, y - 1);
  }

  return memo[x][y] = res;
}

int main() {

  int x, y;
  while (std::cin >> x >> y) {

    if (x == 0 && y == 0)
      break;

    int res = 0;
    std::vector<std::vector<int>> v(x, std::vector<int>(y));

    for (int i = 0; i < 16; i++) {
      for (int j = 0; j < 16; j++) {
        memo[i][j] = -1;
      }
    }

    for (int i = 0; i < x; i++) {
      for (int j = 0; j < y; j++) {
        std::cin >> v[i][j];
      }
    }

    for (int i = 0; i < v.size(); i++)
      res += calc(v, 0, i);

    std::cout << res << std::endl;
  }
  return 0;
}
Problem p00203 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int field[19][17];
int DP[19][17];
int main() {
  int w, h;
  while (cin >> w >> h) {
    if (!w && !h)
      break;
    for (int i = 0; i < 19; i++) {
      for (int j = 0; j < 17; j++) {
        field[i][j] = 0;
        DP[i][j] = 0;
      }
    }
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> field[i][j];
      }
    }
    for (int j = 1; j <= w; j++) {
      if (field[1][j] == 1)
        DP[1][j] = 0;
      else if (field[1][j] == 0)
        DP[1][j] = 1;
    }

    for (int i = 2; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (field[i][j] == 1)
          DP[i][j] = 0;
        else if (field[i][j] == 0)
          DP[i][j] += DP[i - 1][j - 1] + DP[i - 1][j] + DP[i - 1][j + 1];
        else if (field[i][j] == 2) {
          DP[i][j] = 0;
          int dy = 0;
          while (field[i + dy][j] == 2) {
            DP[i + dy][j] = 0;
            dy += 2;
          }
          DP[i + dy][j] += DP[i - 1][j];
        }
      }
    }

    int sum = 0;
    for (int i = h; i <= h + 2; i++) {
      for (int j = 1; j <= w; j++) {
        sum += DP[i][j];
      }
    }
    cout << sum << endl;
    /*
    for(int i=0;i<18;i++){
            for(int j=0;j<17;j++){
                    cout << DP[i][j];
            }
            cout << endl;
    }*/
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int mx, my;
int count;
int field[17][17]; // field[y][x];

void init() {
  for (int i = 0; i < 17; i++) {
    for (int j = 0; j < 17; j++) {
      field[i][j] = 0;
    }
  }
}

void DFS(int x, int y) {
  if (y >= my - 1) {
    count++;
    return;
  }
  int dx, dy;
  dx = x - 1;
  dy = y + 1;
  if (0 <= dx && dx < mx && field[dy][dx] == 0)
    DFS(dx, dy);
  dx = x;
  dy = y + 1;
  if (0 <= dx && dx < mx && field[dy][dx] != 1) {
    if (field[dy][dx] == 2) {
      while (field[dy][dx] == 2) {
        dy += 2;
      }
    }
    DFS(dx, dy);
  }
  dx = x + 1;
  dy = y + 1;
  if (0 <= dx && dx < mx && field[dy][dx] == 0)
    DFS(dx, dy);
  return;
}

int main() {
  while (cin >> mx >> my) {
    if (mx == 0 && my == 0)
      break;
    for (int i = 0; i < my; i++) {
      for (int j = 0; j < mx; j++) {
        cin >> field[i][j];
      }
    }
    count = 0;
    for (int i = 0; i < mx; i++) {
      if (field[0][i] == 0) {
        DFS(i, 0);
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00203 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
using namespace std;
int w, h;
int s[40][40];
int dp[40][40];
int dx[] = {-1, 0, 1};
int ret(int y, int x) {
  if (y >= h - 1) {
    if (h - 1 == y && s[y][x] == 1)
      return 0;
    return 1;
  }
  if (dp[y][x] != -1)
    return dp[y][x];
  if (s[y][x] == 1)
    return dp[y][x] = 0;
  if (s[y][x] == 2) {
    return dp[y][x] = ret(y + 2, x);
  }
  int ans = 0;
  for (int i = 0; i < 3; i++) {
    int nx = dx[i] + x;
    if (nx >= w || nx < 0)
      continue;

    if (s[y + 1][nx] == 0) {
      ans += ret(y + 1, nx);
    } else if (s[y + 1][nx] == 2) {
      if (i != 1)
        continue;
      ans += ret(y + 1, nx);
    }
  }
  return dp[y][x] = ans;
}
int main() {
  while (cin >> w >> h, w || h) {
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> s[i][j];
    int ans = 0;
    for (int i = 0; i < w; i++) {
      // if(s[0][i]==0)
      ans += ret(0, i);
    }

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
typedef long long int ll;
// typedef int ll;
ll w, h, xd[] = {1, 0, -1}, p, u[20][20], d[20][20];
ll ret(ll x, ll y) {
  ll s = x, sum = 0;
  if (h - 1 <= y) {
    return 1;
  }
  if (d[y][x] >= 1)
    return d[y][x];
  else if (u[y][x] == 2)
    return d[y][x] = ret(x, y + 2);
  else if (u[y][x] == 0) {
    y++;
    for (ll i = 0; i < 3; i++) {
      x = s + xd[i];
      if (x >= 0 && x <= w - 1) {
        if (u[y][x] == 2 && i == 1)
          sum += d[y - 1][s] = ret(x, y + 2);
        else if (u[y][x] == 0)
          sum += d[y - 1][s] = ret(x, y);
      }
    }
    return d[y - 1][s] = sum;
  }
  return d[y][s] = -1;
}
int main() {
  ll x, y;
  while (cin >> w >> h, w || h) {
    p = 0;
    for (ll i = 0; i < h; i++)
      for (ll j = 0; j < w; j++) {
        cin >> u[i][j];
        d[i][j] = 0;
      }
    for (ll i = 0; i < w; i++)
      u[h][i] = 0;
    for (ll i = 0; i < w; i++) {
      if (u[0][i] == 0) {
        x = i;
        y = 0;
        p += ret(x, y);
      } else if (u[0][i] == 2) {
        x = i;
        y = 2;
        p += ret(x, y);
      }
    }
    cout << p << endl;
  }
  return 0;
}
Problem p00203 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iostream>
using namespace std;
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)

int m[500][500];
int dp[500][500];

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

int solve(int r, int c) {
  rep(i, r + 2) rep(j, c) dp[i][j] = 0;
  rep(j, c) if (m[0][j] == 0) dp[0][j] = 1;

  rep(i, r - 1) {
    rep(j, c) {
      if (m[i][j] == 0) {
        rep(k, 3) {
          int nex = j + dx[k];
          if (nex < 0 || nex >= c)
            continue;
          if (m[i + 1][nex] == 0)
            dp[i + 1][nex] += dp[i][j];
          else if (m[i + 1][nex] == 2 && k == 1)
            dp[i + 1][nex] += dp[i][j];
        }
      } else if (m[i][j] == 2 && m[i + 2][j] != 1) {
        dp[i + 2][j] += dp[i][j];
      }
    }
  }

  /*
  rep(i,r+1) {
    rep(j,c) printf("%8d",dp[i][j]);
    cout<<endl;
  }*/

  int ans = 0;
  rep(j, c) ans += dp[r][j] + dp[r - 1][j];
  return ans;
}

main() {
  int c, r;
  while (cin >> c >> r && r) {
    rep(i, r) rep(j, c) cin >> m[i][j];
    rep(j, c) m[r][j] = 0;
    cout << solve(r, c) << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
const int MAX_Y = 30;
const int MAX_X = 30;
int mx, my, c[MAX_Y][MAX_X], dp[MAX_Y][MAX_X];

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

int dfs(int y, int x) {
  int res = 0;
  if (y == my - 1)
    return 1;

  if (c[y][x] == 2) {
    if (y + 2 >= my - 1)
      return 1;
    res += dfs(y + 2, x);
    return res;
  }

  rep(z, 3) {
    int tx = x + dx[z], ty = y + dy[z];
    if (tx < 0 || ty < 0 || tx >= mx || ty >= my)
      continue;
    if (c[ty][tx] == 1)
      continue;
    if (c[ty][tx] == 2 && tx != x)
      continue;
    res += dfs(ty, tx);
  }

  return res;
}

void solve() {
  memset(dp, -1, sizeof dp);
  rep(i, my) rep(j, mx) cin >> c[i][j];

  int res = 0;
  rep(i, mx) {
    if (c[0][i] == 1)
      continue;
    res += dfs(0, i);
  }
  cout << res << endl;
}

int main(int argc, char const *argv[]) {
  while (cin >> mx >> my, mx || my) {
    solve();
  }
  return 0;
}
Problem p00203 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

using namespace std;

int run[15][15], w, h;
int memo[15][15];

int ski(int x, int y, bool s) {
  if (y >= h)
    return 1;
  if (memo[y][x] != -1)
    return memo[y][x];
  if (run[y][x] == 1)
    return memo[y][x] = 0;
  if (run[y][x] == 2)
    return s ? ski(x, y + 2, true) : 0;

  if (y == h - 1)
    return memo[y][x] = 1;

  int cnt = 0;
  for (int dx = -1; dx <= 1; dx++) {
    int xx = x + dx;
    if (0 <= xx && xx < w)
      cnt += ski(xx, y + 1, !dx);
  }
  return memo[y][x] = cnt;
}

int main() {
  for (; scanf("%d%d", &w, &h), w;) {
    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++)
        scanf("%d", run[y] + x), memo[y][x] = -1;

    int cnt = 0;
    for (int x = 0; x < w; x++)
      cnt += ski(x, 0, true);
    printf("%d\\n", cnt);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

using namespace std;

int run[15][15], w, h;

int ski(int x, int y, bool s) // s : go straight in previous step?
{
  if (y >= h)
    return 1;
  if (run[y][x] == 1)
    return 0;
  if (run[y][x] == 2) {
    if (s)
      return ski(x, y + 2, true);
    else
      return 0;
  }

  // plane slope (run[y][x]==0)
  if (y == h - 1)
    return 1;

  int cnt = 0;
  for (int dx = -1; dx <= 1; dx++) {
    int xx = x + dx;
    if (0 <= xx && xx < w)
      cnt += ski(xx, y + 1, !dx);
  }
  return cnt;
}

int main() {
  for (; scanf("%d%d", &w, &h), w;) {
    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++)
        scanf("%d", run[y] + x);

    int cnt = 0;
    for (int x = 0; x < w; x++)
      cnt += ski(x, 0, true);
    printf("%d\\n", cnt);
  }

  return 0;
}
Problem p00203 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>

#define SIZE 15 + 5

using namespace std;

int main() {
  std::ios_base::sync_with_stdio(false);

  int X, Y;
  int course[SIZE + 1][SIZE];
  int dp[SIZE + 1][SIZE];
  int ans;

  while (1) {
    cin >> X >> Y;
    if (X == 0 && Y == 0)
      break;

    for (int i = 0; i < Y * X; i++)
      cin >> course[i / X][i % X];
    for (int i = 0; i < X; i++)
      course[Y + 1][i] = 0;
    for (int i = 0; i < (Y + 1) * (X); i++)
      dp[i / X][i % X] = (i / X == 0 && course[i / X][i % X] == 0) ? 1 : 0;

    for (int i = 0; i < (Y - 1) * X; i++) {
      if (dp[i / X][i % X] > 0) {
        if (course[i / X][i % X] == 0) {
          if (i % X != 0) {
            if (course[i / X + 1][i % X - 1] == 0)
              dp[i / X + 1][i % X - 1] += dp[i / X][i % X];
          }
          if (course[i / X + 1][i % X] != 1)
            dp[i / X + 1][i % X] += dp[i / X][i % X];
          if (i % X != X - 1) {
            if (course[i / X + 1][i % X + 1] == 0)
              dp[i / X + 1][i % X + 1] += dp[i / X][i % X];
          }
        } else if (course[i / X][i % X] == 2) {
          if (course[i / X + 2][i % X] != 1)
            dp[i / X + 2][i % X] += dp[i / X][i % X];
        }
      }
    }
    ans = 0;
    for (int i = (Y - 1) * X; i < (Y + 1) * X; i++) {
      ans += dp[i / X][i % X];
    }
    cout << ans << endl;
  }

  return 0;
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <cstdio>
#include <iostream>

#define SIZE 15

using namespace std;

int main() {
  std::ios_base::sync_with_stdio(false);

  int X, Y;
  int course[SIZE][SIZE + 1];
  int dp[SIZE][SIZE + 1];
  int ans;

  while (1) {
    cin >> X >> Y;
    if (X == 0 && Y == 0)
      break;

    for (int i = 0; i < Y * X; i++)
      cin >> course[i / X][i % X];
    for (int i = 0; i < X; i++)
      course[Y + 1][i] = 0;
    for (int i = 0; i < Y * (X + 1); i++)
      dp[i / X][i % X] = (i / X == 0 && course[i / X][i % X] == 0) ? 1 : 0;

    for (int i = 0; i < (Y - 1) * X; i++) {
      if (dp[i / X][i % X] > 0) {
        if (course[i / X][i % X] == 0) {
          if (i % X != 0) {
            if (course[i / X + 1][i % X - 1] == 0)
              dp[i / X + 1][i % X - 1] += dp[i / X][i % X];
          }
          if (course[i / X + 1][i % X] != 1)
            dp[i / X + 1][i % X] += dp[i / X][i % X];
          if (i % X != X - 1) {
            if (course[i / X + 1][i % X + 1] == 0)
              dp[i / X + 1][i % X + 1] += dp[i / X][i % X];
          }
        } else if (course[i / X][i % X] == 2) {
          if (course[i / X + 2][i % X] != 1)
            dp[i / X + 2][i % X] += dp[i / X][i % X];
        }
      }
    }
    ans = 0;
    for (int i = (Y - 1) * X; i < (Y + 1) * X; i++) {
      ans += dp[i / X][i % X];
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p00203 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int dfs(int x, int y);
int mas[20][20];
int memo[20][20];
int X, Y;

int main() {
  while (1) {
    cin >> X >> Y;
    if (X == 0 && Y == 0)
      break;
    for (int i = 0; i < 20; i++) {
      for (int j = 0; j < 20; j++) {
        memo[i][j] = -1;
      }
    }
    for (int i = 0; i < Y; i++) {
      for (int j = 0; j < X; j++) {
        cin >> mas[i][j];
      }
    }
    int ans = 0;
    for (int i = 0; i < X; i++) {
      if (mas[0][i] == 0) {
        ans += dfs(i, 0);
      }
    }
    cout << ans << endl;
  }
}

int dfs(int x, int y) {
  if (memo[y][x] != -1)
    return memo[y][x];
  if (mas[y][x] == 1)
    return 0;
  if (y >= Y - 1)
    return 1;
  if (mas[y][x] == 2)
    return (memo[y][x] = dfs(x, y + 2));
  else {
    int cou = 0;
    for (int i = -1; i < 2; i++) {
      int nx = x + i;
      int ny = y + 1;
      if (nx >= 0 && nx < X &&
          ((i == 0 && mas[ny][nx] == 2) || (mas[ny][nx] == 0))) {
        cou += dfs(nx, ny);
      }
    }
    return (memo[y][x] = cou);
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int dfs(int x, int y);
int mas[20][20];
int memo[20][20];
int X, Y;

int main() {
  while (1) {
    cin >> X >> Y;
    if (X == 0 && Y == 0)
      break;
    for (int i = 0; i < 20; i++) {
      for (int j = 0; j < 20; j++) {
        memo[20][20] = -1;
      }
    }
    for (int i = 0; i < Y; i++) {
      for (int j = 0; j < X; j++) {
        cin >> mas[i][j];
      }
    }
    int ans = 0;
    for (int i = 0; i < X; i++) {
      if (mas[0][i] == 0) {
        ans += dfs(i, 0);
      }
    }
    cout << ans << endl;
  }
}

int dfs(int x, int y) {
  if (memo[y][x] == -1)
    return memo[y][x];
  if (mas[y][x] == 1)
    return 0;
  if (y >= Y - 1)
    return 1;
  if (mas[y][x] == 2)
    return (memo[y][x] = dfs(x, y + 2));
  else {
    int cou = 0;
    for (int i = -1; i < 2; i++) {
      int nx = x + i;
      int ny = y + 1;
      if (nx >= 0 && nx < X &&
          ((i == 0 && mas[ny][nx] == 2) || (mas[ny][nx] == 0))) {
        cou += dfs(nx, ny);
      }
    }
    return (memo[y][x] = cou);
  }
}
Problem p00203 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int W, H, mas[16][16];
  while (cin >> W >> H, W) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> mas[i][j];
      }
    }
    vector<vector<int>> dp(H + 1, vector<int>(W, 0));
    for (int i = 0; i < W; i++) {
      if (mas[0][i] != 1)
        dp[0][i] = 1;
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (dp[i][j] == 0)
          continue;
        if (mas[i][j] == 0) {
          for (int k = -1; k <= 1; k++) {
            if (j + k < 0 || j + k >= W)
              continue;
            if ((i == H - 1 && k == 0) ||
                (i != H - 1 && k == 0 && mas[i + 1][j + k] == 2) ||
                (i != H - 1 && mas[i + 1][j + k] == 0)) {
              dp[i + 1][j + k] += dp[i][j];
            }
          }
        } else if (mas[i][j] == 2) {
          if (i >= H - 2 || mas[i + 2][j] != 1) {
            dp[min(H, i + 2)][j] += dp[i][j];
          }
        }
      }
    }

    int ret = 0;
    for (int i = 0; i < W; i++) {
      ret += dp[H][i];
    }
    cout << ret << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int W, H, mas[16][16];
  while (cin >> W >> H, W) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> mas[i][j];
      }
    }
    vector<vector<int>> dp(H + 1, vector<int>(W, 0));
    for (int i = 0; i < W; i++) {
      if (mas[0][i] != 1)
        dp[0][i] = 1;
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (dp[i][j] == 0)
          continue;
        if (mas[i][j] == 0) {
          for (int k = -1; k <= 1; k++) {
            if (j + k < 0 || j + k >= W)
              continue;
            if ((i == H - 1 && k == 0) ||
                (i != H - 1 && k == 0 && mas[i + 1][j + k] == 2) ||
                (i != H - 1 && mas[i + 1][j + k] == 0)) {
              dp[i + 1][j + k] += dp[i][j];
            }
          }
        } else if (mas[i][j] == 2) {
          if (i >= H - 2 || mas[i + 2][j] != 1) {
            dp[max(H, i + 2)][j] += dp[i][j];
          }
        }
      }
    }

    int ret = 0;
    for (int i = 0; i < W; i++) {
      ret += dp[H][i];
    }
    cout << ret << endl;
  }
}