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

#include <stdio.h>
int ans, wx, wy, rem;
int tab[10][10];
inline void solve(int nx, int ny, int rem) {
  int nowx, nowy;
  nowx = nx;
  nowy = ny;
LA:
  nowx++;
  if (nowx >= wx)
    goto LB;
  else if (tab[nowx][nowy] == 2 && rem == 0)
    ans++;
  else if (tab[nowx][nowy] == 1) {
    tab[nowx][nowy] = -1;
    solve(nowx, nowy, rem - 1);
    tab[nowx][nowy] = 1;
    goto LB;
  }
  goto LA;
LB:

  nowx = nx, nowy = ny;
MA:
  nowx--;
  if (0 > nowx)
    goto MB;
  else if (tab[nowx][nowy] == 2 && rem == 0)
    ans++;
  else if (tab[nowx][nowy] == 1) {
    tab[nowx][nowy] = -1;
    solve(nowx, nowy, rem - 1);
    tab[nowx][nowy] = 1;
    goto MB;
  }
  goto MA;
MB:
  nowx = nx, nowy = ny;
NA:
  nowy++;
  if (nowy >= wy)
    goto NB;
  else if (tab[nowx][nowy] == 2 && rem == 0)
    ans++;
  else if (tab[nowx][nowy] == 1) {
    tab[nowx][nowy] = -1;
    solve(nowx, nowy, rem - 1);
    tab[nowx][nowy] = 1;
    goto NB;
  }
  goto NA;
NB:
  nowx = nx, nowy = ny;
OA:
  nowy--;
  if (0 > nowy)
    goto OB;
  else if (tab[nowx][nowy] == 2 && rem == 0)
    ans++;
  else if (tab[nowx][nowy] == 1) {
    tab[nowx][nowy] = -1;
    solve(nowx, nowy, rem - 1);
    tab[nowx][nowy] = 1;
    goto OB;
  }
  goto OA;
OB:;
}
int main() {
  for (;;) {
    int m, n;
    scanf("%d%d", &m, &n);
    if (m == 0 && n == 0)
      break;
    int stx, sty, homec = 0;
    int i, j;
    for (i = 0; i < n; i++)
      for (j = 0; j < m; j++) {
        scanf("%d", &tab[i][j]);
        if (tab[i][j] == 1)
          homec++;
        else if (tab[i][j] == 2) {
          stx = i;
          sty = j;
        }
      }
    wx = n;
    wy = m;
    ans = 0;
    solve(stx, sty, homec);
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <vector>
using namespace std;
int ans;
char tab[12][12];
int wx, wy;
int nx, ny, rem;
void solve(int nx, int ny, int rem) {
  int nowx, nowy;
  nowx = nx;
  nowy = ny;
LA:
  nowx++;
  if (nowx >= wx)
    goto LB;
  else if (tab[nowx][nowy] == 2 && rem == 0)
    ans++;
  else if (tab[nowx][nowy] == 1) {
    tab[nowx][nowy] = -1;
    solve(nowx, nowy, rem - 1);
    tab[nowx][nowy] = 1;
    goto LB;
  }
  goto LA;
LB:

  nowx = nx, nowy = ny;
MA:
  nowx--;
  if (0 > nowx)
    goto MB;
  else if (tab[nowx][nowy] == 2 && rem == 0)
    ans++;
  else if (tab[nowx][nowy] == 1) {
    tab[nowx][nowy] = -1;
    solve(nowx, nowy, rem - 1);
    tab[nowx][nowy] = 1;
    goto MB;
  }
  goto MA;
MB:
  nowx = nx, nowy = ny;
NA:
  nowy++;
  if (nowy >= wy)
    goto NB;
  else if (tab[nowx][nowy] == 2 && rem == 0)
    ans++;
  else if (tab[nowx][nowy] == 1) {
    tab[nowx][nowy] = -1;
    solve(nowx, nowy, rem - 1);
    tab[nowx][nowy] = 1;
    goto NB;
  }
  goto NA;
NB:
  nowx = nx, nowy = ny;
OA:
  nowy--;
  if (0 > nowy)
    goto OB;
  else if (tab[nowx][nowy] == 2 && rem == 0)
    ans++;
  else if (tab[nowx][nowy] == 1) {
    tab[nowx][nowy] = -1;
    solve(nowx, nowy, rem - 1);
    tab[nowx][nowy] = 1;
    goto OB;
  }
  goto OA;
OB:;
}
int main() {
  for (;;) {
    int m, n;
    scanf("%d%d", &m, &n);
    if (m == 0 && n == 0)
      break;
    int stx, sty, homec = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        int ins;
        scanf("%d", &ins);
        tab[i][j] = ins;
        if (tab[i][j] == 1)
          homec++;
        else if (tab[i][j] == 2) {
          stx = i;
          sty = j;
        }
      }
    wx = n;
    wy = m;
    ans = 0;
    solve(stx, sty, homec);
    printf("%d\\n", ans);
  }
}