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

#include <iostream>

using namespace std;

int main() {
  int i, j;
  int map[20][20];
  int smap[20][20] = {0};
  int mx, my;
  int next[3] = {-1, 0, 1};

  for (;;) {
    int ans = 0;

    cin >> mx >> my;

    if (mx == 0 && my == 0)
      break;

    for (i = 0; i < 20; i++)
      for (j = 0; j < 20; j++) {
        smap[i][j] = 0;
        map[i][j] = 0;
      }

    for (i = 0; i < my; i++) {
      for (j = 0; j < mx; j++) {
        cin >> map[i][j];
      }
    }
    for (i = 0; i < mx; i++) {
      if (map[0][i] != 1)
        smap[0][i] = 1;
    }
    for (i = 0; i < my - 1; i++) {
      for (j = 0; j < mx; j++) {
        if (map[i][j] == 2) {
          if (map[i + 2][j] != 1)
            smap[i + 2][j] += smap[i][j];
        } else {
          for (int k = 0; k < 3; k++) {
            if (j + next[k] >= 0 && j + next[k] < mx) {
              if (map[i + 1][j + next[k]] == 1) {
                smap[i + 1][j + next[k]] = 0;
              } else if (map[i + 1][j + next[k]] == 0) {
                smap[i + 1][j + next[k]] += smap[i][j];
              } else if (map[i + 1][j + next[k]] == 2 && j == j + next[k]) {
                smap[i + 1][j + next[k]] += smap[i][j];
              }
            }
          }
        }
      }
    }

    for (i = my - 1; i < my + 1; i++) {
      for (j = 0; j < mx; j++)
        ans += smap[i][j];
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int map[16][16];
int x, y;
int ans;
int goalmap[16][16];
int s_map[16][16];

void serch(int nowx, int nowy) {
  int next[3] = {-1, 0, 1};
  /*
          s_map[nowy][nowx]=1;

          getchar();

          system("cls");
          cout << "x:" << nowx << " y:" << nowy << endl;
          cout << "ans:" << ans << endl;
          for(int i=0;i<y;i++)
          {
                  for(int j=0;j<x;j++)
                  {
                          cout << s_map[i][j] << " " ;
                  }
                  cout << endl;
          }
  //*/
  if (goalmap[nowy][nowx] == -1) {
    //		cout << "ツ湘。ツ害ツ閉ィツづ按つ、" << endl;
    s_map[nowy][nowx] = 0;
    return;
  }

  if (map[nowy][nowx] == 2) {
    //		cout << "jump" << endl;

    if (nowy + 2 >= y - 1) {
      //			cout << "goalup_jump" << endl;
      ans++;
      s_map[nowy][nowx] = 0;
      return;
    }

    serch(nowx, nowy + 2);
  } else {
    for (int i = 0; i < 3; i++) {
      if (nowx + next[i] >= 0 && nowx + next[i] < x) {
        if (map[nowy + 1][nowx + next[i]] != -1) {
          if (nowy + 1 >= y) {
            //						cout << "goalup" <<
            //endl;
            ans++;
            s_map[nowy][nowx] = 0;
            return;
          }
          //					cout << "next 2" << endl;
          if (map[nowy + 1][nowx + next[i]] != 2)
            serch(nowx + next[i], nowy + 1);
          else {
            if (nowx + next[i] == nowx)
              serch(nowx + next[i], nowy + 1);
          }
        }
      }
    }
  }
  s_map[nowy][nowx] = 0;
  return;
}

int main() {
  int i, j;

  for (;;) {
    cin >> x >> y;
    if (x == 0 && y == 0)
      break;

    for (i = 0; i < y; i++) {
      for (j = 0; j < x; j++) {
        cin >> map[i][j];
      }
    }

    ans = 0;

    for (i = 0; i < y; i++)
      for (j = 0; j < x; j++) {
        if (map[i][j] == 1)
          goalmap[i][j] = -1;
        else
          goalmap[i][j] = 0;
        s_map[i][j] = 0;
      }

    for (i = 0; i < x; i++) {
      serch(i, 0);
    }

    /*
                    for(i=0;i<x;i++)
                    {
                            cout << goalmap[0][i] << endl;
                            if(goalmap[0][i]!=-1)
                                    ans+=goalmap[0][i];
                    }


                    for(i=0;i<y;i++)
                    {
                            for(j=0;j<x;j++)
                            {
                                    cout << goalmap[i][j] << " " ;
                            }
                            cout << endl;
                    }
    //*/

    cout << ans << endl;
  }
}