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


#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int n, m;
int masu[17][17];
int ura[17][17];

int motom() {
  for (int i = 0; i < 17; i++) {
    for (int j = 0; j < 17; j++) {
      masu[i][j] = 0;
    }
  }
  for (int i = 1; i <= m; i++) {
    switch (ura[1][i]) {
    case 0:
      masu[1][i] += 1;
      break;
    case 1:
      masu[1][i] += 0;
      break;
    case 2:
      masu[1][i] += 1;
      break;
    }
  }
  for (int i = 1; i < n; i++) {
    for (int j = 1; j <= m; j++) {
      if (ura[i][j] == 0) {
        if (ura[i + 1][j - 1] == 0) {
          masu[i + 1][j - 1] += masu[i][j];
        }
        if (ura[i + 1][j] != 1) {
          masu[i + 1][j] += masu[i][j];
        }
        if (ura[i + 1][j + 1] == 0) {
          masu[i + 1][j + 1] += masu[i][j];
        }
      } else if (ura[i][j] == 2) {
        if (ura[i + 2][j] != 1) {
          masu[i + 2][j] += masu[i][j];
        }
      }
    }
  }

  int cont = 0;
  for (int i = n; i <= n + 1; i++) {
    for (int j = 1; j <= m; j++) {
      cont += masu[i][j];
    }
  }
  return cont;
}
int contt;
void saiki(int x, int y) {
  if (y < 1 || y > m) {
    return;
  }

  masu[x][y]++;
  if (x > n - 1) {
    contt++;
    return;
  }
  if (ura[x][y] == 0) {
    if (ura[x + 1][y - 1] == 0) {
      saiki(x + 1, y - 1);
    }
    if (ura[x + 1][y] != 1) {
      saiki(x + 1, y);
    }
    if (ura[x + 1][y + 1] == 0) {
      saiki(x + 1, y + 1);
    }
  }
  if (ura[x][y] == 2) {
    if (ura[x + 2][y] != 1) {
      saiki(x + 2, y);
    }
  }
}
int simu() {
  for (int i = 0; i < 17; i++) {
    for (int j = 0; j < 17; j++) {
      masu[i][j] = 0;
    }
  }
  contt = 0;
  for (int i = 1; i <= m; i++) {
    if (ura[1][i] != 1) {
      saiki(1, i);
    }
  }
  /*
  puts("");
  for(int i=1;i<=n+1;i++){
          for(int j=1;j<=m;j++){
                  printf("%d ",masu[i][j]);
          }
          puts("");
  }*/
  return contt;
}

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

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int n, m;
int masu[17][17];
int ura[17][17];

int motom() {
  for (int i = 1; i <= n; i++) {
    switch (ura[1][i]) {
    case 0:
      masu[1][i] += 1;
      break;
    case 1:
      masu[1][i] += 0;
      break;
    case 2:
      masu[1][i] += 1;
      break;
    }
  }
  for (int i = 1; i < n; i++) {
    for (int j = 1; j <= m; j++) {
      if (ura[i][j] == 0) {
        if (ura[i + 1][j - 1] == 0) {
          masu[i + 1][j - 1] += masu[i][j];
        }
        if (ura[i + 1][j] != 1) {
          masu[i + 1][j] += masu[i][j];
        }
        if (ura[i + 1][j + 1] == 0) {
          masu[i + 1][j + 1] += masu[i][j];
        }
      } else if (ura[i][j] == 2) {
        if (ura[i + 2][j] != 1) {
          masu[i + 2][j] += masu[i][j];
        }
      }
    }
  }

  int cont = 0;
  for (int i = n; i <= n + 1; i++) {
    for (int j = 1; j <= m; j++) {
      cont += masu[i][j];
    }
  }
  return cont;
}
int contt;
void saiki(int x, int y) {
  if (y < 1 || y > m) {
    return;
  }

  masu[x][y]++;
  if (x > n - 1) {
    contt++;
    return;
  }
  if (ura[x][y] == 0) {
    if (ura[x + 1][y - 1] == 0) {
      saiki(x + 1, y - 1);
    }
    if (ura[x + 1][y] != 1) {
      saiki(x + 1, y);
    }
    if (ura[x + 1][y + 1] == 0) {
      saiki(x + 1, y + 1);
    }
  }
  if (ura[x][y] == 2) {
    saiki(x + 2, y);
  }
}
int simu() {
  contt = 0;
  for (int i = 1; i <= m; i++) {
    if (ura[1][i] != 1) {
      saiki(1, i);
    }
  }
  /*
  puts("");
  for(int i=1;i<=n+1;i++){
          for(int j=1;j<=m;j++){
                  printf("%d ",masu[i][j]);
          }
          puts("");
  }*/
  return contt;
}

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;
        ura[i][j] = 0;
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        scanf("%d", &ura[i][j]);
      }
    }

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