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

// 2011/10/22 Tazoe

#include <iostream>
using namespace std;

int DFS(int M[12][12], int N, int Y, int X, int Y2, int X2) {
  //	cout << M[Y][X] << ' ' << N << endl;

  if (M[Y][X] == 1)
    N--;

  if (N == 0)
    if (Y == Y2 || X == X2)
      return 1;
    else
      return 0;

  M[Y][X] = 0;

  int cnt = 0;

  int y = Y;
  int x = X;
  y--;
  while (M[y][x] == 0)
    y--;
  if (M[y][x] == 1)
    cnt += DFS(M, N, y, x, Y2, X2);

  y = Y;
  x = X;
  y++;
  while (M[y][x] == 0)
    y++;
  if (M[y][x] == 1)
    cnt += DFS(M, N, y, x, Y2, X2);

  y = Y;
  x = X;
  x--;
  while (M[y][x] == 0)
    x--;
  if (M[y][x] == 1)
    cnt += DFS(M, N, y, x, Y2, X2);

  y = Y;
  x = X;
  x++;
  while (M[y][x] == 0)
    x++;
  if (M[y][x] == 1)
    cnt += DFS(M, N, y, x, Y2, X2);

  M[Y][X] = 1;
  return cnt;
}

int main() {
  while (true) {
    int m, n;
    cin >> m >> n;
    if (m == 0 && n == 0)
      break;

    int M[12][12];
    for (int y = 0; y < n + 2; y++)
      for (int x = 0; x < m + 2; x++)
        M[y][x] = 3;

    int N = 0;
    int Y, X;
    for (int y = 1; y <= n; y++) {
      for (int x = 1; x <= m; x++) {
        cin >> M[y][x];
        if (M[y][x] == 1) {
          N++;
        } else if (M[y][x] == 2) {
          Y = y;
          X = x;
        }
      }
    }

    int cnt;
    cnt = DFS(M, N, Y, X, Y, X);

    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// 2011/10/22 Tazoe

#include <iostream>
using namespace std;

int DFS(int M[12][12], int N, int Y, int X) {
  //	cout << M[Y][X] << ' ' << N << endl;

  int m[12][12];
  for (int y = 0; y < 12; y++)
    for (int x = 0; x < 12; x++)
      m[y][x] = M[y][x];

  int n = N;

  if (m[Y][X] == 1) {
    m[Y][X] = 0;
    n--;
  } else if (m[Y][X] == 2) {
    ;
  } else {
    return 0;
  }

  int y, x;

  if (n == 0) {
    y = Y;
    x = X;
    y--;
    while (m[y][x] == 0)
      y--;
    if (m[y][x] == 2)
      return 1;

    y = Y;
    x = X;
    y++;
    while (m[y][x] == 0)
      y++;
    if (m[y][x] == 2)
      return 1;

    y = Y;
    x = X;
    x--;
    while (m[y][x] == 0)
      x--;
    if (m[y][x] == 2)
      return 1;

    y = Y;
    x = X;
    x++;
    while (m[y][x] == 0)
      x++;
    if (m[y][x] == 2)
      return 1;

    return 0;
  }

  int cnt = 0;

  y = Y;
  x = X;
  y--;
  while (m[y][x] == 0 || m[y][x] == 2)
    y--;
  if (m[y][x] == 1)
    cnt += DFS(m, n, y, x);

  y = Y;
  x = X;
  y++;
  while (m[y][x] == 0 || m[y][x] == 2)
    y++;
  if (m[y][x] == 1)
    cnt += DFS(m, n, y, x);

  y = Y;
  x = X;
  x--;
  while (m[y][x] == 0 || m[y][x] == 2)
    x--;
  if (m[y][x] == 1)
    cnt += DFS(m, n, y, x);

  y = Y;
  x = X;
  x++;
  while (m[y][x] == 0 || m[y][x] == 2)
    x++;
  if (m[y][x] == 1)
    cnt += DFS(m, n, y, x);

  return cnt;
}

int main() {
  while (true) {
    int m, n;
    cin >> m >> n;
    if (m == 0 && n == 0)
      break;

    int M[12][12];
    for (int y = 0; y < n + 2; y++)
      for (int x = 0; x < m + 2; x++)
        M[y][x] = 3;

    int N = 0;
    int Y, X;
    for (int y = 1; y <= n; y++) {
      for (int x = 1; x <= m; x++) {
        cin >> M[y][x];
        if (M[y][x] == 1) {
          N++;
        } else if (M[y][x] == 2) {
          Y = y;
          X = x;
        }
      }
    }

    int cnt;
    cnt = DFS(M, N, Y, X);

    cout << cnt << endl;
  }

  return 0;
}