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

#include <iostream>
#include <vector>
using namespace std;

int m, n, k = 0;
int a[15][15], hnex[15][15], hpre[15][15], wnex[15][15], wpre[15][15], h[15],
    w[15];
int ans = 0, locx, locy;

int solve(int x, int y) {
  int ret = 0;
  k--;
  bool b[4] = {false, false, false, false};
  if (hpre[x][y] != 0) {
    hnex[hpre[x][y]][y] = hnex[x][y];
    b[0] = true;
  }
  if (hnex[x][y] != 0) {
    hpre[hnex[x][y]][y] = hpre[x][y];
    b[1] = true;
  }
  if (wpre[x][y] != 0) {
    wnex[x][wpre[x][y]] = wnex[x][y];
    b[2] = true;
  }
  if (wnex[x][y] != 0) {
    wpre[x][wnex[x][y]] = wpre[x][y];
    b[3] = true;
  }
  if (k == 0) {
    k++;
    // cout<<"solve("<<x<<","<<y<<")="<<1<<endl;
    if (x == locx || y == locy)
      return 1;
    // cout<<"solve("<<x<<","<<y<<")="<<0<<endl;
    return 0;
  }
  if (b[0])
    ret += solve(hpre[x][y], y);
  if (b[1])
    ret += solve(hnex[x][y], y);
  if (b[2])
    ret += solve(x, wpre[x][y]);
  if (b[3])
    ret += solve(x, wnex[x][y]);
  if (b[0])
    hnex[hpre[x][y]][y] = x;
  if (b[1])
    hpre[hnex[x][y]][y] = x;
  if (b[2])
    wnex[x][wpre[x][y]] = y;
  if (b[3])
    wpre[x][wnex[x][y]] = y;
  k++;
  // cout<<"solve("<<x<<","<<y<<")="<<ret<<" "<<k<<endl;
  return ret;
}

int main() {
  while (1) {
    for (int i = 0; i < 15; i++)
      for (int j = 0; j < 15; j++) {
        a[i][j] = 4;
        wnex[i][j] = 0;
        wpre[i][j] = 0;
        hnex[i][j] = 0;
        hpre[i][j] = 0;
      }
    for (int i = 0; i < 15; i++) {
      h[i] = 0;
      w[i] = 0;
    }
    k = 0;
    scanf("%d%d", &m, &n);
    if (m == 0 && n == 0)
      break;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        scanf("%d", &a[i][j]);
        if (a[i][j] == 1) {
          k++;
          hnex[h[j]][j] = i;
          hpre[i][j] = h[j];
          h[j] = i;
          wnex[i][w[i]] = j;
          wpre[i][j] = w[i];
          w[i] = j;
          a[i][j] = 3;
        }
        if (a[i][j] == 2) {
          k++;
          locx = i;
          locy = j;
          hnex[h[j]][j] = i;
          hpre[i][j] = h[j];
          h[j] = i;
          wnex[i][w[i]] = j;
          wpre[i][j] = w[i];
          w[i] = j;
        }
      }
    }
    ans = solve(locx, locy);
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int m, n, k = 0;
int a[15][15], c[15][15], d[15][15], sh[15], sw[15];
vector<int> h[15], w[15];
int ans = 0, locx, locy;

int solve(int x, int y) {
  int ret = 0;
  bool b = false;
  if (a[x][y] == 1) {
    a[x][y] = 3;
    b = true;
    k--;
  }
  if (k == 0) {
    a[x][y] = 1;
    k++;
    // cout<<"solve("<<x<<","<<y<<")="<<1<<endl;
    for (int i = c[x][y] - 1; i >= 0; i--) {
      // if(a[h[y][i]][y]==3)break;
      if (a[h[y][i]][y] == 2)
        return 1;
      break;
    }
    for (int i = c[x][y] + 1; i < sh[y]; i++) {
      // if(a[h[y][i]][y]==3)break;
      if (a[h[y][i]][y] == 2)
        return 1;
      break;
    }
    for (int j = d[x][y] - 1; j >= 0; j--) {
      // if(a[x][w[x][j]]==3)break;
      if (a[x][w[x][j]] == 2)
        return 1;
      break;
    }
    for (int j = d[x][y] + 1; j < sw[x]; j++) {
      // if(a[x][w[x][j]]==3)break;
      if (a[x][w[x][j]] == 2)
        return 1;
      break;
    }
    // cout<<"solve("<<x<<","<<y<<")="<<0<<endl;
    return 0;
  }
  for (int i = c[x][y] - 1; i >= 0; i--) {
    if (a[h[y][i]][y] == 3)
      break;
    if (a[h[y][i]][y] == 1)
      ret += solve(h[y][i], y);
  }
  for (int i = c[x][y] + 1; i < sh[y]; i++) {
    if (a[h[y][i]][y] == 3)
      break;
    if (a[h[y][i]][y] == 1)
      ret += solve(h[y][i], y);
  }
  for (int j = d[x][y] - 1; j >= 0; j--) {
    if (a[x][w[x][j]] == 3)
      break;
    if (a[x][w[x][j]] == 1)
      ret += solve(x, w[x][j]);
  }
  for (int j = d[x][y] + 1; j < sw[x]; j++) {
    if (a[x][w[x][j]] == 3)
      break;
    if (a[x][w[x][j]] == 1)
      ret += solve(x, w[x][j]);
  }
  if (b) {
    a[x][y] = 1;
    k++;
  }
  // cout<<"solve("<<x<<","<<y<<")="<<ret<<endl;
  return ret;
}

int main() {
  while (1) {
    for (int i = 0; i < 15; i++)
      for (int j = 0; j < 15; j++) {
        a[i][j] = 3;
        c[i][j] = 0;
        d[i][j] = 0;
      }
    for (int i = 0; i < 15; i++) {
      h[i].clear();
      w[i].clear();
    }
    k = 0;
    scanf("%d%d", &m, &n);
    if (m == 0 && n == 0)
      break;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        scanf("%d", &a[i][j]);
        if (a[i][j] == 1) {
          k++;
          c[i][j] = h[j].size();
          d[i][j] = w[i].size();
          h[j].push_back(i);
          w[i].push_back(j);
        }
        if (a[i][j] == 2) {
          locx = i;
          locy = j;
          c[i][j] = h[j].size();
          d[i][j] = w[i].size();
          h[j].push_back(i);
          w[i].push_back(j);
        }
      }
    }
    for (int i = 0; i < 15; i++) {
      sh[i] = h[i].size();
      sw[i] = w[i].size();
    }
    ans = solve(locx, locy);
    printf("%d\\n", ans);
  }
}