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

#include <algorithm>
#include <cmath>
#include <stack>
#include <stdio.h>

#define NUM 1100000

using namespace std;

int table[NUM];

void func(int n, int budget) {

  int tmp, max_prime = 0, food[n], index = 0;

  bool checkTable[budget + 1], FLG;

  for (int i = 0; i < budget + 1; i++)
    checkTable[i] = false;

  checkTable[0] = true;

  for (int i = 0; i < n; i++) {
    scanf("%d", &tmp);
    FLG = true;

    //??¢?????????????????°???????¢??????????
    for (int loop = 0; loop < index; loop++) {
      if (food[loop] == tmp) {
        FLG = false;
        break;
      }
    }

    if (FLG == true && tmp <= budget) {
      food[index++] = tmp;

      for (int k = 1; k <= budget; k++) {
        if ((k % tmp == 0) || (k >= tmp && checkTable[k - tmp] == true)) {
          checkTable[k] = true;
          if (table[k] == 1) {
            max_prime = max(max_prime, k);
          }
        }
      }
    }
  }

  if (max_prime == 0)
    printf("NA\\n");
  else {
    printf("%d\\n", max_prime);
  }
}

int main() {
  int limit;

  for (int i = 0; i < NUM; i++)
    table[i] = 1;
  table[0] = 0;
  table[1] = 0;

  limit = sqrt(NUM);

  for (int i = 2; i <= limit; i++) {
    if (table[i] == 1) {
      for (int k = 2 * i; k < NUM; k += i) {
        table[k] = 0;
      }
    }
  }

  int n, budget;

  while (true) {
    scanf("%d %d", &n, &budget);
    if (n == 0 && budget == 0)
      break;

    func(n, budget);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <stack>
#include <stdio.h>

#define NUM 1100000

using namespace std;

int table[NUM];

void func(int n, int budget) {

  int food[n], max_prime = 0;
  int *firstCheck = new int[1000001];
  bool *checkTable = new bool[budget + 1];

  for (int i = 0; i < 1000001; i++)
    firstCheck[i] = 0;
  for (int i = 0; i < budget + 1; i++)
    checkTable[i] = false;

  checkTable[0] = true;

  for (int i = 0; i < n; i++) {
    scanf("%d", &food[i]);

    if (firstCheck[food[i]] == 0 && food[i] <= budget) {
      firstCheck[food[i]] = 1;

      for (int k = 1; k <= budget; k++) {
        if ((k % food[i] == 0) ||
            (k >= food[i] && checkTable[k - food[i]] == true)) {
          checkTable[k] = true;
          if (table[k] == 1) {
            max_prime = max(max_prime, k);
          }
        }
      }
    }
  }

  if (max_prime == 0)
    printf("NA\\n");
  else {
    printf("%d\\n", max_prime);
  }
}

int main() {
  int limit;

  for (int i = 0; i < NUM; i++)
    table[i] = 1;
  table[0] = 0;
  table[1] = 0;

  limit = sqrt(NUM);

  for (int i = 2; i <= limit; i++) {
    if (table[i] == 1) {
      for (int k = 2 * i; k < NUM; k += i) {
        table[k] = 0;
      }
    }
  }

  int n, budget;

  while (true) {
    scanf("%d %d", &n, &budget);
    if (n == 0 && budget == 0)
      break;

    func(n, budget);
  }
  return 0;
}
Problem p00202 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

int main() {
  char pri[1000000];
  int i, j;

  for (i = 0; i < 1000000; i++) {
    pri[i] = '1';
  }
  pri[0] = pri[1] = '0';

  for (i = 0; i < 1000000; i++) {
    if (pri[i] == '1') {
      for (j = 2; j * i < 1000000; j++) {
        pri[i * j] = '0';
      }
    }
  }

  int n, max, menu[100];
  vector<char> sum;

  for (;;) {
    cin >> n >> max;
    if (n == 0 && max == 0)
      break;
    int ans = 0;
    sum.clear();
    for (i = 0; i < 1000000; i++)
      sum.push_back(0);

    for (i = 0; i < n; i++) {
      cin >> menu[i];
      sum[menu[i]] = '1';
    }

    for (i = 0; i <= max; i++) {
      if (sum[i] == '1') {
        if (pri[i] == '1')
          ans = i;
        for (j = 0; j < n; j++) {
          if (i + menu[j] <= max)
            sum[i + menu[j]] = '1';
        }
      }
    }
    if (ans == 0)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  char pri[1000000];
  int i, j;

  for (i = 0; i < 1000000; i++) {
    pri[i] = '1';
  }
  pri[0] = pri[1] = '0';

  for (i = 0; i < 1000000; i++) {
    if (pri[i] == '1') {
      for (j = 2; j * i < 1000000; j++) {
        pri[i * j] = '0';
      }
    }
  }

  int n, max, menu[100];
  vector<char> sum;

  for (;;) {
    cin >> n >> max;
    if (n == 0 && max == 0)
      break;
    int ans = 0;
    sum.clear();
    for (i = 0; i < 1000000; i++)
      sum.push_back(0);

    for (i = 0; i < n; i++) {
      cin >> menu[i];
      sum[menu[i]] = '1';
    }
    for (i = 0; i <= max; i++) {
      //			cout << i << endl;
      if (sum[i] != '1') {
        for (j = 0; j < i; j++) {
          if (sum[j] == '1' && sum[i - j] == '1') {
            sum[i] = '1';
            if (pri[i] == '1')
              ans = i;
            break;
          }
        }
      }
    }

    if (ans == 0)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}
Problem p00202 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
bool DP[31][1145149];
int cost[31];
bool prime(int N) {
  if (N == 0)
    return false;
  for (int i = 2; i * i <= N; i++) {
    if (N % i == 0)
      return false;
  }
  return true;
}
int main() {
  while (true) {
    int N, X;
    scanf("%d%d", &N, &X);
    if (N == 0 && X == 0)
      return 0;
    for (int i = 0; i < N; i++)
      scanf("%d", &cost[i]);
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= X; j++) {
        DP[i][j] = false;
      }
    }
    DP[0][0] = true;
    for (int i = 1; i <= N; i++) {
      for (int j = 0; j <= X; j++) {
        if (DP[i - 1][j])
          DP[i][j] = true;
        if (j >= cost[i - 1] && DP[i - 1][j - cost[i - 1]])
          DP[i][j] = true;
        if (j >= cost[i - 1] && DP[i][j - cost[i - 1]])
          DP[i][j] = true;
      }
    }
    bool F = true;
    for (int i = X; i >= 1; i--) {
      if (F && DP[N][i] && prime(i)) {
        printf("%d\\n", i);
        F = false;
      }
    }
    if (F)
      printf("NA\\n");
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
bool DP[31][114514];
int cost[31];
bool prime(int N) {
  if (N == 0)
    return false;
  for (int i = 2; i * i <= N; i++) {
    if (N % i == 0)
      return false;
  }
  return true;
}
int main() {
  while (true) {
    int N, X;
    scanf("%d%d", &N, &X);
    if (N == 0 && X == 0)
      return 0;
    for (int i = 0; i < N; i++)
      scanf("%d", &cost[i]);
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= X; j++) {
        DP[i][j] = false;
      }
    }
    DP[0][0] = true;
    for (int i = 1; i <= N; i++) {
      for (int j = cost[i - 1]; j <= X; j++) {
        if (DP[i - 1][j])
          DP[i][j] = true;
        if (DP[i - 1][j - cost[i - 1]])
          DP[i][j] = true;
        if (DP[i][j - cost[i - 1]])
          DP[i][j] = true;
      }
    }
    bool F = true;
    for (int i = X; i >= 1; i--) {
      if (F && DP[N][i] && prime(i)) {
        printf("%d\\n", i);
        F = false;
      }
    }
    if (F)
      printf("NA\\n");
  }
}
Problem p00203 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int w, h;
int mp[20][20];
int memo[20][20];
int ans;
int count;

int saiki(int x, int y) {
  count++;
  // cout << x <<" "<<y <<endl;
  int dx[3] = {-1, 0, 1};
  int dy[3] = {1, 1, 1};

  if (y >= h - 1)
    ans++, memo[y][x]++;

  else if (mp[y][x] != 1) {
    for (int i = 0; i < 3; i++) {
      int nx = dx[i] + x;
      int ny = dy[i] + y;
      if (nx < 0 || nx >= w || memo[ny][nx] == -1 || mp[ny][nx] == 1)
        continue;

      if (memo[ny][nx] > 0 && mp[ny][nx] == 0) {
        ans += memo[ny][nx];
        memo[y][x] += memo[ny][nx];
      } else if (mp[ny][nx] == 0) {
        saiki(nx, ny);
        if (memo[ny][nx] > 0)
          memo[y][x] += memo[ny][nx];
      } else if (nx == x && mp[ny][nx] == 2) {
        int ty = ny;
        while (mp[ty][nx] == 2)
          ty += 2;
        if (mp[ty][nx] == 0 && memo[ty][nx] == 0) {
          saiki(nx, ty);
          if (memo[ty][nx] > 0)
            memo[y][x] += memo[ty][nx];
        } else if (mp[ty][nx] == 0 && memo[ty][nx] > 0) {
          memo[y][x] += memo[ty][nx];
          ans += memo[ty][nx];
        }
      }
    }
    if (memo[y][x] == 0)
      memo[y][x] = -1;
  }
}

int main() {

  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    for (int i = 0; i <= 19; i++)
      for (int j = 0; j <= 19; j++) {
        memo[i][j] = 0;
        mp[i][j] = 0;
      }

    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> mp[i][j];

    ans = 0;
    count = 0;
    for (int i = 0; i < w; i++) {
      if (mp[0][i] != 1)
        saiki(i, 0);
    }

    /*
    for(int i=0;i<h;i++) {
    for(int j=0;j<w;j++)
      cout << memo[i][j]<<" ";
    cout << endl;
    }
    */

    int ans2 = 0;
    for (int i = 0; i < w; i++)
      if (memo[0][i] > 0)
        ans2 += memo[0][i];

    cout << ans << endl;
    //   cout << ans2 << endl;
    // cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int w, h;
int mp[16][16];
int ans;
int count;

int saiki(int x, int y) {
  count++;
  // cout << x <<" "<<y <<endl;
  int dx[3] = {-1, 0, 1};
  int dy[3] = {1, 1, 1};
  if (mp[y][x] == 2)
    y += 2;

  if ((y >= h) || (y == h - 1 && mp[y][x] != 1))
    ans++; //,cout <<"ok"<<endl;
  else if (mp[y][x] != 1) {
    for (int i = 0; i < 3; i++) {
      int nx = dx[i] + x;
      int ny = dy[i] + y;
      if (nx < 0 || ny < 0 || nx >= w || ny >= h)
        continue;
      if (mp[ny][nx] == 0) {
        saiki(nx, ny);
      } else if (nx == x && mp[ny][nx] == 2) {
        saiki(nx, ny + 2);
      }
    }
  }
}

int main() {

  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> mp[i][j];
    /*
    for(int i=0;i<h;i++) {
      for(int j=0;j<w;j++)
        cout << mp[i][j];
          cout << endl;
    }
    */

    ans = 0;
    count = 0;
    for (int i = 0; i < w; i++) {
      if (mp[0][i] != 1)
        saiki(i, 0);
    }
    cout << ans << endl;
    // cout << count << endl;
  }

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

#define _USE_MATH_DEFINES
#define INF 10000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, string> P;

static const double eps = 1e-8;

const int moveTo[][2] = {{-1, 1}, {0, 1}, {1, 1}};

int stage[17][17];
int dp[17][17];

int main() {
  int w, h;
  while (~scanf("%d %d", &w, &h)) {
    if (w == h && h == 0)
      break;
    memset(stage, -1, sizeof(stage));
    memset(dp, 0, sizeof(dp));
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        scanf("%d", &stage[y][x]);
      }
    }

    // space:0 tree:1 jump:2
    for (int x = 0; x < w; x++) {
      if (stage[0][x] != 1)
        dp[0][x] = 1;
    }

    for (int dy = 1; dy <= h; dy++) {
      for (int dx = 0; dx < w; dx++) {
        if (stage[dy][dx] != 1) {
          for (int j = 0; j < 3; j++) {
            int sx = dx - moveTo[j][0];
            int sy = dy - moveTo[j][1];
            if (sx < 0 || sx >= w || sy < 0 || sy >= h - 1)
              continue;

            // space:0 tree:1 jump:2
            if (stage[sy][sx] != 0)
              continue;
            if (stage[dy][dx] == 2 && dx != sx)
              continue;

            dp[dy][dx] += dp[sy][sx];
          }

          int sx = dx;
          int sy = dy - 2;
          if (sx >= 0 && sx < w && sy >= 0 && sy < h - 1) {
            // space:0 tree:1 jump:2
            if (stage[sy][sx] == 2) {
              dp[dy][dx] += dp[sy][sx];
            }
          }
        }
      }
    }

    int res = 0;
    for (int y = h - 1; y <= h; y++) {
      for (int x = 0; x < w; x++) {
        res += dp[y][x];
      }
    }

    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;

int X, Y, dx[] = {-1, 0, 1}, dy[] = {1, 1, 1};
long long ans;
int field[15][15];

void dfs(int x, int y) {
  if (y >= Y - 1) {
    if (field[y][x] != 1) {
      ans++;
      return;
    }
  }
  if (field[y][x] == 2) {
    dfs(x, y + 2);
    return;
  }
  if (field[y][x] == 1) {
    return;
  }

  for (int i = 0; i < 3; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= 0 && nx < X && ny >= 0 && ny < Y && field[ny][nx] != 1 &&
        !(i != 1 && field[ny][nx] == 2)) {
      if (field[ny][nx] == 2)
        dfs(nx, ny + 2);
      else
        dfs(nx, ny);
    }
  }
}

void solve() {
  for (int x = 0; x < X; x++) {
    if (field[0][x] == 0)
      dfs(x, 0);
  }
}

int main() {
  while (1) {
    memset(field, 0, sizeof(field));
    ans = 0;
    cin >> X >> Y;
    if (!X && !Y)
      break;

    for (int i = 0; i < Y; i++) {
      for (int j = 0; j < X; j++) {
        cin >> field[i][j];
      }
    }

    solve();

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

#include <cstdio>

int main() {
  while (true) {
    int X, Y;
    scanf("%d%d", &X, &Y);
    if (X == 0 || Y == 0) {
      break;
    }
    int field[16][17] = {{0}};
    for (int y = 1; y <= Y; y++) {
      for (int x = 1; x <= X; x++) {
        scanf("%d", &(field[y][x]));
      }
    }
    int dp[16][17] = {{0}};
    for (int x = 1; x <= X; x++) {
      if (field[1][x] != 1) {
        dp[1][x] = 1;
      }
    }
    for (int y = 2; y <= Y; y++) {
      for (int x = 1; x <= X; x++) {
        if (field[y][x] == 1) {
          continue;
        }
        int sum = 0;
        if (field[y][x] == 0) {
          if (field[y - 1][x - 1] == 0) {
            sum += dp[y - 1][x - 1];
          }
          if (field[y - 1][x + 1] == 0) {
            sum += dp[y - 1][x + 1];
          }
        }
        if (field[y - 1][x] == 0) {
          sum += dp[y - 1][x];
        }
        if (field[y - 2][x] == 2) {
          sum += dp[y - 2][x];
        }
        dp[y][x] = sum;
      }
    }
    int answer = 0;
    for (int x = 1; x <= X; x++) {
      if (field[Y - 1][x] == 2) {
        answer += dp[Y - 1][x];
      }
      answer += dp[Y][x];
    }
    printf("%d\\n", answer);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

int main() {
  while (true) {
    int X, Y;
    scanf("%d%d", &X, &Y);
    int field[16][17] = {{0}};
    for (int y = 1; y <= Y; y++) {
      for (int x = 1; x <= X; x++) {
        scanf("%d", &(field[y][x]));
      }
    }
    int dp[16][17] = {{0}};
    for (int x = 1; x <= X; x++) {
      if (field[1][x] != 1) {
        dp[1][x] = 1;
      }
    }
    for (int y = 2; y <= Y; y++) {
      for (int x = 1; x <= X; x++) {
        if (field[y][x] == 1) {
          continue;
        }
        int sum = 0;
        if (field[y][x] == 0) {
          if (field[y - 1][x - 1] == 0) {
            sum += dp[y - 1][x - 1];
          }
          if (field[y - 1][x + 1] == 0) {
            sum += dp[y - 1][x + 1];
          }
        }
        if (field[y - 1][x] == 0) {
          sum += dp[y - 1][x];
        }
        if (field[y - 2][x] == 2) {
          sum += dp[y - 2][x];
        }
        dp[y][x] = sum;
      }
    }
    int answer = 0;
    for (int x = 1; x <= X; x++) {
      if (field[Y - 1][x] == 2) {
        answer += dp[Y - 1][x];
      }
      answer += dp[Y][x];
    }
    printf("%d\\n", answer);
  }
  return 0;
}
Problem p00203 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int X, Y;
int dp[20][20];
char maps[20][20];
int bun(int x, int y);
int dx[3] = {-1, 0, 1};
main() {
  while (1) {
    cin >> X >> Y;
    if (X == 0 && Y == 0) {
      break;
    }
    for (int i = 1; i <= Y; i++) {
      for (int j = 1; j <= X; j++) {
        cin >> maps[i][j];
        dp[i][j] = 0;
      }
    }
    int ans = 0;
    for (int i = 1; i <= X; i++) {
      if (maps[1][i] != '1') {
        ans += bun(1, i);
      }
    }
    cout << ans << endl;
  }
}
int bun(int y, int x) {
  if (y >= Y) {
    return 1;
  } else if (dp[y][x] != 0) {
    return dp[y][x];
  } else if (maps[y][x] == '2') {
    if (maps[y + 2][x] != '1') {
      return dp[y][x] = bun(y + 2, x);
    } else {
      return 0;
    }
  } else if (maps[y][x] == '1') {
    return 0;
  } else {
    int anss = 0;
    for (int i = 0; i < 3; i++) {
      int nx = x + dx[i];
      if (i != 1) {
        if (nx > 0 && nx <= X && maps[y + 1][nx] == '0') {
          anss += bun(y + 1, nx);
        }
      } else {
        if (maps[y + 1][nx] != '1') {
          anss += bun(y + 1, nx);
        }
      }
    }
    return dp[y][x] = anss;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

int main() {
  int x, y;
  while (cin >> x >> y, x, y) {
    vector<vector<int>> course(y + 2, vector<int>(x + 2, 0));
    for (int i = 1; i <= y; i++) {
      for (int j = 1; j <= x; j++) {
        cin >> course[i][j];
      }
    }

    vector<vector<int>> path(y + 2, vector<int>(x + 2, 0));
    //?????????????????????????????????1????????????
    for (int i = 1; i <= x; i++) {
      if (course[1][i] != 1)
        path[1][i] = 1;
    }

    for (int i = 1; i <= y; i++) {
      for (int j = 1; j <= x; j++) {
        //????????????????????´????????????????????´??????????????????????????????????????????
        //?????????????§????????????????????????´?????????????????????????????£????????°?????´????????????????????????
        if (course[i][j] == 0) {
          if (course[i + 1][j + 1] == 0)
            path[i + 1][j + 1] += path[i][j];
          if (course[i + 1][j] != 1)
            path[i + 1][j] += path[i][j];
          if (course[i + 1][j - 1] == 0)
            path[i + 1][j - 1] += path[i][j];
        }
        //?¬???????????????´???????????£????????°????????????????????????????????????
        //?????????????????£????????§??´??????????????´???????????????(?????£?????????????????????????????´???????????????)
        if (course[i][j] == 2 && course[i + 2][j] != 1) {
          path[i + 2][j] += path[i][j];
          if (i + 2 > y && i != y)
            path[y][j] += path[i][j];
        }
        //???????????´???????????????????????????????????????
        if (course[i][j] == 1) { /*do anything*/
        }
      }
    }

    cout << accumulate(path[y].begin() + 1, path[y].end() - 1, 0) << endl;
  }
  return 0;
}
Problem p00203 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int solve(int x, int y);
int ax, ay;
int mas[20][20];
int memo[20][20];
int main() {
  while (1) {
    int ans = 0;
    cin >> ax >> ay;
    if (ax == 0 && ay == 0)
      break;
    for (int i = 1; i <= ay; i++) {
      for (int j = 1; j <= ax; j++) {
        cin >> mas[i][j];
        memo[i][j] = 0;
      }
    }
    for (int i = 1; i <= ax; i++) {
      if (mas[1][i] != 1) {
        ans += solve(i, 1);
      }
    }
    cout << ans << endl;
  }
}
int solve(int x, int y) {
  if (y >= ay) {
    return 1;
  } else if (memo[y][x] != 0) {
    return memo[y][x];
  } else if (mas[y][x] == 2) {
    if (mas[y + 2][x] != 1) {
      return memo[y][x] = solve(x, y + 2);
    } else {
      return 0;
    }
  } else if (mas[y][x] == 1) {
    return 0;
  } else {
    int anss = 0;
    if (mas[y + 1][x] != 1) {
      anss += solve(x, y + 1);
    }
    if (x + 1 <= ax && 0 < x + 1 && mas[y + 1][x + 1] == 0) {
      anss += solve(x + 1, y + 1);
    }
    if (0 < x - 1 && x - 1 <= ax && mas[y + 1][x - 1] == 0) {
      anss += solve(x - 1, y + 1);
    }
    return memo[y][x] = anss;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int solve(int x, int y);
int ax, ay;
int mas[200][200];
long long memo[200][200];
int main() {
  while (1) {
    for (int i = 0; i < 200; i++) {
      for (int j = 0; j < 200; j++) {
        memo[i][j] = 0;
      }
    }
    long long ans = 0;
    cin >> ax >> ay;
    if (ax == 0 && ay == 0)
      break;

    for (int i = 0; i < ay; i++) {
      for (int j = 0; j < ax; j++) {
        cin >> mas[i][j];
      }
    }

    for (int i = 0; i < ax; i++) {
      if (mas[0][i] != 1) {
        ans += solve(i, 0);
      }
    }
    cout << ans << endl;
  }
}
int solve(int x, int y) {
  if (memo[y][x] != 0) {
    return memo[y][x];
  } else if (y == ay) {
    return 1;
  } else if (mas[y][x] == 1) {
    return 0;
  } else if (mas[y][x] == 2) {
    return solve(x, y + 2);
  } else {
    long long anss = 0;
    if (mas[y + 1][x] != 1) {
      anss += solve(x, y + 1);
    }
    if (x + 1 < ax && mas[y + 1][x + 1] == 0 && y + 1 != ay) {
      anss += solve(x + 1, y + 1);
    }
    if (0 <= x - 1 && mas[y + 1][x - 1] == 0 && y + 1 != ay) {
      anss += solve(x - 1, y + 1);
    }
    return memo[y][x] = anss;
  }
}
Problem p00203 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int a[16][16];
int count = 0;
int n, m;

void dfs(int y, int x) {
  if (a[y][x] == 0) {

    if (y == m - 1) {
      count++;
      return;
    }
    if (x - 1 >= 0 && y + 1 < m) {
      if (a[y + 1][x - 1] != 2) {
        dfs(y + 1, x - 1);
      }
    }
    if (y + 1 < m) {
      dfs(y + 1, x);
    }
    if (x + 1 < n && y + 1 < m) {
      if (a[y + 1][x + 1] != 2) {
        dfs(y + 1, x + 1);
      }
    }
    return;
  } else if (a[y][x] == 1) {
    return;
  } else if (a[y][x] == 2) {
    if (y == m - 2 || y == m - 1) {
      count++;
      return;
    } else {
      dfs(y + 2, x);
    }
  }
}

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0) {
      break;
    }

    memset(a, 0, sizeof(a));
    rep(i, m) {
      rep(j, n) { cin >> a[i][j]; }
    }
    count = 0;
    rep(i, n) dfs(0, i);

    cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int a[16][16];
int count = 0;
int n, m;

void dfs(int y, int x) {
  cout << "y:" << y << " x:" << x << endl;
  if (a[y][x] == 0) {
    if (y == m - 1) {
      count++;
      return;
    }
    if (x - 1 >= 0 && y + 1 < m) {
      if (a[y + 1][x - 1] != 2) {
        dfs(y + 1, x - 1);
      }
    }
    if (y + 1 < m) {
      dfs(y + 1, x);
    }
    if (x + 1 < n && y + 1 < m) {
      if (a[y + 1][x + 1] != 2) {
        dfs(y + 1, x + 1);
      }
    }
    return;
  } else if (a[y][x] == 1) {
    return;
  } else if (a[y][x] == 2) {
    if (y == m - 2 || y == m - 1) {
      count++;
      return;
    } else {
      dfs(y + 2, x);
    }
  }
}

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0) {
      break;
    }

    memset(a, 0, sizeof(a));
    rep(i, n) {
      rep(j, m) { cin >> a[i][j]; }
    }
    count = 0;
    rep(i, m) dfs(0, i);

    cout << count << endl;
  }
  return 0;
}