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

#include <cstdio>
#include <iostream>
#include <map>
// ABC
// DEF
// GHI
using namespace std;
;
typedef pair<int, int> P;
int n, out, can, dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0}, dp[3][3][16];
int gox, goy, mox, moy, stx, sty;
int func(int y, int x, int bat) {
  if (dp[y][x][bat] >= 0)
    return dp[y][x][bat];
  if (bat == 0) {
    if (y == goy && x == gox) {
      can++;
      out++;
      return dp[y][x][bat] = 1;
    }
    out++;
    return 0;
  }
  int nx, ny, sum = 0;
  for (int i = 0; i < 4; i++) {
    ny = dy[i] + y;
    nx = dx[i] + x;
    if (!(ny >= 0 && ny < 3 && nx >= 0 && nx < 3) || (nx == mox && ny == moy))
      sum += func(y, x, bat - 1);
    else
      sum += func(ny, nx, bat - 1);
  }
  return dp[y][x][bat] = sum;
}
int main() {
  while (cin >> n, n) {
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        for (int k = 0; k <= n; k++)
          dp[i][j][k] = -1;
    char a[3];
    can = 0;
    out = 0;
    int sx[3], sy[3];
    cin >> a[0] >> a[1] >> a[2];
    for (int i = 0; i < 3; i++) {
      if (a[i] == 'A') {
        sy[i] = 0;
        sx[i] = 0;
      } else if (a[i] == 'B') {
        sy[i] = 0;
        sx[i] = 1;
      } else if (a[i] == 'C') {
        sy[i] = 0;
        sx[i] = 2;
      } else if (a[i] == 'D') {
        sy[i] = 1;
        sx[i] = 0;
      } else if (a[i] == 'E') {
        sy[i] = 1;
        sx[i] = 1;
      } else if (a[i] == 'F') {
        sy[i] = 1;
        sx[i] = 2;
      } else if (a[i] == 'G') {
        sy[i] = 2;
        sx[i] = 0;
      } else if (a[i] == 'H') {
        sy[i] = 2;
        sx[i] = 1;
      } else if (a[i] == 'I') {
        sy[i] = 2;
        sx[i] = 2;
      }
    }
    sty = sy[0];
    stx = sx[0];
    goy = sy[1];
    gox = sx[1];
    moy = sy[2];
    mox = sx[2];
    func(sty, stx, n);
    int pre = 1;
    for (int i = 0; i < n; i++)
      pre *= 4;
    printf("%.8lf\\n", (double)dp[sty][stx][n] / pre);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
// ABC
// DEF
// GHI
using namespace std;
;
int n, out, can, dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};
int gox, goy, mox, moy, stx, sty;
void func(int y, int x, int bat) {
  if (bat == 0) {
    if (y == goy && x == gox)
      can++;
    out++;
    return;
  }
  int nx, ny;
  for (int i = 0; i < 4; i++) {
    ny = dy[i] + y;
    nx = dx[i] + x;
    if (!(ny >= 0 && ny < 3 && nx >= 0 && nx < 3) || (nx == mox && ny == moy))
      func(y, x, bat - 1);
    else
      func(ny, nx, bat - 1);
  }
}
int main() {
  while (cin >> n, n) {
    char a[3];
    can = 0;
    out = 0;
    int sx[3], sy[3];
    cin >> a[0] >> a[1] >> a[2];
    for (int i = 0; i < 3; i++) {
      if (a[i] == 'A') {
        a[i] = 0;
        sx[i] = 0;
      } else if (a[i] == 'B') {
        sy[i] = 0;
        sx[i] = 1;
      } else if (a[i] == 'C') {
        sy[i] = 0;
        sx[i] = 2;
      } else if (a[i] == 'D') {
        sy[i] = 1;
        sx[i] = 0;
      } else if (a[i] == 'E') {
        sy[i] = 1;
        sx[i] = 1;
      } else if (a[i] == 'F') {
        sy[i] = 1;
        sx[i] = 2;
      } else if (a[i] == 'G') {
        sy[i] = 2;
        sx[i] = 0;
      } else if (a[i] == 'H') {
        sy[i] = 2;
        sx[i] = 1;
      } else if (a[i] == 'I') {
        sy[i] = 2;
        sx[i] = 2;
      }
    }
    sty = sy[0];
    stx = sx[0];
    goy = sy[1];
    gox = sx[1];
    moy = sy[2];
    mox = sx[2];
    func(sty, stx, n);
    printf("%.8lf\\n", (double)can / out);
  }
  return 0;
}