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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int n, m;

int kx, ky;
int field[11][11];
int home;
const int INF = 1000000000;

int cnt = 0;

int psize;
pair<int, int> path[200];

void dfs(int x, int y) {
  if (home == 0) {
    if (x == kx || y == ky) {
      cnt++;
      // for(int i = 0; i < psize; i++){
      //	cout<<path[i].first<< " "<<path[i].second<<endl;
      // }
      // cout<<endl;
      return;
    }
  } else {
    for (int i = y - 1; i >= 0; i--) {
      if (field[i][x] == 1) {
        field[i][x] = 0;
        home--;
        path[psize] = make_pair(i, x);
        psize++;
        dfs(x, i);
        psize--;
        home++;
        field[i][x] = 1;
        break;
      }
    }
    for (int i = y + 1; i < n; i++) {
      if (field[i][x] == 1) {
        field[i][x] = 0;
        home--;
        path[psize] = make_pair(i, x);
        psize++;
        dfs(x, i);
        psize--;
        home++;
        field[i][x] = 1;
        break;
      }
    }

    for (int i = x - 1; i >= 0; i--) {
      if (field[y][i] == 1) {
        field[y][i] = 0;
        home--;
        path[psize] = make_pair(y, i);
        psize++;
        dfs(i, y);
        psize--;
        home++;
        field[y][i] = 1;
        break;
      }
    }
    for (int i = x + 1; i < m; i++) {
      if (field[y][i] == 1) {
        field[y][i] = 0;
        home--;
        path[psize] = make_pair(y, i);
        psize++;
        dfs(i, y);
        psize--;
        home++;
        field[y][i] = 1;
        break;
      }
    }
  }
}

int main() {

  while (cin >> m >> n && !(n == 0 && m == 0)) {
    psize = 0;
    home = 0;
    cnt = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> field[i][j];
        if (field[i][j] == 2) {
          kx = j;
          ky = i;
        } else if (field[i][j] == 1)
          home++;
      }
    }
    dfs(kx, ky);
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int n, m;

int kx, ky;
int field[11][11];
int home;

map<int, int> dp[11][11];

int dfs(int s, int x, int y) {
  if (dp[y][x].find(s) != dp[y][x].end()) {
    return dp[y][x][s];
  }

  if (s == ((1 << home) - 1)) {
    if (ky == y) {
      // ÌûüÖ
      for (int i = x - 1; i >= 0; i--) {
        if (field[y][i] == -1) {
          dp[y][x][s] = 1;
          return 1;
        } else if (field[y][i] != -2) {
          break;
        }
      }
      for (int i = x + 1; i < m; i++) {
        if (field[y][i] == -1) {
          dp[y][x][s] = 1;
          return 1;
        } else if (field[y][i] != -2) {
          break;
        }
      }
      return 0;
    }
    if (kx == x) {
      // ÌûüÖ
      for (int i = y - 1; i >= 0; i--) {
        if (field[i][x] == -1) {
          dp[y][x][s] = 1;
          return 1;
        } else if (field[i][x] != -2) {
          break;
        }
      }
      for (int i = y + 1; i < m; i++) {
        if (field[i][x] == -1) {
          dp[y][x][s] = 1;
          return 1;
        } else if (field[i][x] != -2) {
          break;
        }
      }
      dp[y][x][s] = 0;
      return 0;
    }
    dp[y][x][s] = 0;
    return 0;
  } else {
    // Ü¾S[µÄ¢È¢ê
    // »Ýn©çÚ®Å«éêÖÚ®
    // ÌûüÖ
    int sum = 0;
    for (int i = x - 1; i >= 0; i--) {
      if (field[y][i] >= 0) {
        if (!((s >> field[y][i]) & 1)) {
          sum += dfs(s | (1 << field[y][i]), i, y);
        } else
          break;
      }
    }
    for (int i = x + 1; i < m; i++) {
      if (field[y][i] >= 0) {
        if (!((s >> field[y][i]) & 1)) {
          sum += dfs(s | (1 << field[y][i]), i, y);
        } else
          break;
      }
    }
    // ÌûüÖ
    for (int i = y - 1; i >= 0; i--) {
      if (field[i][x] >= 0) {
        if (!((s >> field[i][x]) & 1)) {
          sum += dfs(s | (1 << field[i][x]), x, i);
        } else
          break;
      }
    }
    for (int i = y + 1; i < m; i++) {
      if (field[i][x] >= 0) {
        if (!((s >> field[i][x]) & 1)) {
          sum += dfs(s | (1 << field[i][x]), x, i);
        } else
          break;
      }
    }
    dp[y][x][s] = sum;
    return sum;
  }
}

int main() {

  while (cin >> m >> n && !(n == 0 && m == 0)) {
    for (int i = 0; i < 11; i++)
      for (int j = 0; j < 11; j++)
        dp[i][j].clear();
    home = 0;
    memset(field, 0, sizeof(field));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int t;
        cin >> t;
        if (t == 1) {
          field[i][j] = home;
          home++;
        } else if (t == 0) {
          field[i][j] = -2;
        } else if (t == 2) {
          field[i][j] = -1;
          kx = j;
          ky = i;
        }
      }
    }
    cout << dfs(0, kx, ky) << endl;
  }

  return 0;
}