Problem p00203 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

int X, Y;
int board[20][20];
int cnt;
int memo[20][20];
int solve(int x, int y, int dir) {
  if (x < 1 || x > X || board[y][x] == 1 || (board[y][x] == 2 && dir != 0)) {
    return 0;
  } else if (y >= Y) {
    return 1;
  }
  if (memo[y][x] != -1) {
    return memo[y][x];
  }
  if (board[y][x] == 2) {
    return memo[y][x] = solve(x, y + 2, 0);
  } else {
    return memo[y][x] = solve(x - 1, y + 1, -1) + solve(x, y + 1, 0) +
                        solve(x + 1, y + 1, 1);
  }
}

int main() {
  while (cin >> X >> Y, X || Y) {
    cnt = 0;
    rep(i, 20) {
      rep(j, 20) {
        board[i][j] = 0;
        memo[i][j] = -1;
      }
    }
    for (int i = 1; i <= Y; i++) {
      for (int j = 1; j <= X; j++) {
        cin >> board[i][j];
      }
    }
    for (int i = 1; i <= X; i++) {
      if (board[1][i] == 0) {
        cnt += solve(i, 1, 0);
      }
      // cout <<endl;
    }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {-1, 0, 1};
int X, Y;
int board[20][20];
int cnt;
void dfs(int y, int x) {
  while (board[y][x] == 2) {
    y += 2;
    if (board[y][x] == 1) {
      return;
    }
  }
  if (board[y][x] == 1)
    return;
  if (y >= Y) {
    cnt++;
    // printf("(y,x) =(%d,%d)\\n",y,x);
    return;
  }

  // cout <<"y = "<<y <<" x = "<<x<<endl;
  for (int i = 0; i < 3; i++) {
    int nx = x + dx[i];
    int ny = y + 1;
    if (nx < 1 || nx > X)
      continue;
    if (i != 1 && board[ny][nx] == 2)
      continue;
    if (board[ny][nx] == 1)
      continue;
    if (board[ny][nx] == 0 || board[ny][nx] == 2) {
      dfs(ny, nx);
    }
  }
  return;
}

int main() {
  while (cin >> X >> Y, X || Y) {
    cnt = 0;
    rep(i, 20) rep(j, 20) board[i][j] = 0;
    for (int i = 1; i <= Y; i++) {
      for (int j = 1; j <= X; j++) {
        cin >> board[i][j];
      }
    }
    for (int i = 1; i <= X; i++) {
      if (board[1][i] == 0) {
        dfs(1, i);
      }
      // cout <<endl;
    }
    cout << cnt << endl;
  }
}