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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int a[12][12], sx, sy, n;
int fn(int x, int y, int p) {
  int i;
  int ct = 0;
  if (p == n) {
    if (x == sx || y == sy)
      ct = 1;
  } else {
    a[x][y] = 0;
    for (i = 1; a[x + i][y] == 0; i++)
      ;
    if (a[x + i][y] != -1)
      ct += fn(x + i, y, p + 1);
    for (i = 1; a[x - i][y] == 0; i++)
      ;
    if (a[x - i][y] != -1)
      ct += fn(x - i, y, p + 1);
    for (i = 1; a[x][y + i] == 0; i++)
      ;
    if (a[x][y + i] != -1)
      ct += fn(x, y + i, p + 1);
    for (i = 1; a[x][y - i] == 0; i++)
      ;
    if (a[x][y - i] != -1)
      ct += fn(x, y - i, p + 1);
    a[x][y] = 1;
  }
  return ct;
}
int main() {
  int i, j;
  int w, h;
  while (cin >> w >> h && (w || h)) {
    memset(a, -1, sizeof(a));
    n = 0;
    for (i = 1; i < h + 1; i++) {
      for (j = 1; j < w + 1; j++) {
        cin >> a[j][i];
        if (0) {
        } else if (a[j][i] == 1) {
          n++;
        } else if (a[j][i] == 2) {
          sx = j;
          sy = i;
        }
      }
    }
    cout << fn(sx, sy, 0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int a[12][12], ex[4], ey[4], sx, sy, m, n;
int fn(int x, int y, int p, int q) {
  int i;
  int ct = 0;
  if (x == sx || y == sy) {
    for (i = 0; i < m; i++) {
      if (x == ex[i] && y == ey[i])
        break;
    }
    if (i != m)
      q++;
  }
  if (p == n) {
    if (y == sy) {
      for (i = 1; a[x + i][y] != -1; i++) {
        if (x + i == sx && y == sy)
          ct = 1;
      }
      for (i = 1; a[x - i][y] != -1; i++) {
        if (x - i == sx && y == sy)
          ct = 1;
      }
    }
    if (x == sx) {
      for (i = 1; a[x][y + i] != -1; i++) {
        if (x == sx && y + i == sy)
          ct = 1;
      }
      for (i = 1; a[x][y - i] != -1; i++) {
        if (x == sx && y - i == sy)
          ct = 1;
      }
    }
  } else if (q != m) {
    for (i = 1; a[x + i][y] != -1; i++) {
      if (a[x + i][y] == 1) {
        a[x + i][y] = -1;
        ct += fn(x + i, y, p + 1, q);
        a[x + i][y] = 1;
      }
    }
    for (i = 1; a[x - i][y] != -1; i++) {
      if (a[x - i][y] == 1) {
        a[x - i][y] = -1;
        ct += fn(x - i, y, p + 1, q);
        a[x - i][y] = 1;
      }
    }
    for (i = 1; a[x][y + i] != -1; i++) {
      if (a[x][y + i] == 1) {
        a[x][y + i] = -1;
        ct += fn(x, y + i, p + 1, q);
        a[x][y + i] = 1;
      }
    }
    for (i = 1; a[x][y - i] != -1; i++) {
      if (a[x][y - i] == 1) {
        a[x][y - i] = -1;
        ct += fn(x, y - i, p + 1, q);
        a[x][y - i] = 1;
      }
    }
  }
  return ct;
}
int main() {
  int i, j;
  int w, h;
  while (cin >> w >> h && w + h) {
    memset(a, -1, sizeof(a));
    n = 0;
    for (i = 1; i < h + 1; i++) {
      for (j = 1; j < w + 1; j++) {
        cin >> a[j][i];
        if (0) {
        } else if (a[j][i] == 1) {
          n++;
        } else if (a[j][i] == 2) {
          a[j][i] = 0;
          sx = j;
          sy = i;
        }
      }
    }
    memset(ex, -1, sizeof(ex));
    memset(ey, -1, sizeof(ey));
    m = 0;
    for (i = 1; a[sx + i][sy] != -1 && a[sx + i][sy] != 1; i++)
      ;
    if (a[sx + i][sy] == 1) {
      ex[m] = sx + i;
      ey[m] = sy;
      m++;
    }
    for (i = 1; a[sx - i][sy] != -1 && a[sx - i][sy] != 1; i++)
      ;
    if (a[sx - i][sy] == 1) {
      ex[m] = sx - i;
      ey[m] = sy;
      m++;
    }
    for (i = 1; a[sx][sy + i] != -1 && a[sx][sy + i] != 1; i++)
      ;
    if (a[sx][sy + i] == 1) {
      ex[m] = sx;
      ey[m] = sy + i;
      m++;
    }
    for (i = 1; a[sx][sy - i] != -1 && a[sx][sy - i] != 1; i++)
      ;
    if (a[sx][sy - i] == 1) {
      ex[m] = sx;
      ey[m] = sy - i;
      m++;
    }
    cout << fn(sx, sy, 0, 0) << endl;
  }
  return 0;
}