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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

long cnt, as;
char s, t, b;
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};
int main() {
  int n, tx, ty;
  long field[2][5][5];
  for (; cin >> n, n;) {
    cin.ignore();
    cnt = as = 0;
    scanf(" %c %c %c ", &s, &t, &b);
    memset(field, 0, sizeof(field));
    int x = s == 'A' || s == 'D' || s == 'G'   ? 1
            : s == 'B' || s == 'E' || s == 'H' ? 2
                                               : 3;
    int y = s == 'A' || s == 'B' || s == 'C'   ? 1
            : s == 'D' || s == 'E' || s == 'F' ? 2
                                               : 3;

    int p = t == 'A' || t == 'D' || t == 'G'   ? 1
            : t == 'B' || t == 'E' || t == 'H' ? 2
                                               : 3;
    int q = t == 'A' || t == 'B' || t == 'C'   ? 1
            : t == 'D' || t == 'E' || t == 'F' ? 2
                                               : 3;

    int u = b == 'A' || b == 'D' || b == 'G'   ? 1
            : b == 'B' || b == 'E' || b == 'H' ? 2
                                               : 3;
    int t = b == 'A' || b == 'B' || b == 'C'   ? 1
            : b == 'D' || b == 'E' || b == 'F' ? 2
                                               : 3;

    field[0][y][x] = 1;
    field[0][0][0] = field[0][0][1] = field[0][0][2] = field[0][0][3] =
        field[0][0][4] = 1;
    field[0][4][0] = field[0][4][1] = field[0][4][2] = field[0][4][3] =
        field[0][4][4] = 1;
    field[0][1][0] = field[0][2][0] = field[0][3][0] = 1;
    field[0][1][4] = field[0][2][4] = field[0][3][4] = 1;
    for (int k = 0; k < n; k++) {
      for (int i = 1; i < 4; i++) {
        for (int j = 1; j < 4; j++) {
          if (i == t && j == u)
            continue;

          for (int g = 0; g < 4; g++) {
            tx = j + dx[g];
            ty = i + dy[g];
            if (tx < 1 || ty < 1 || tx >= 4 || ty >= 4)
              continue;
            field[1][i][j] += field[0][ty][tx];
          }
          /*
          if(field[0][i][j]!=0 && (i-1==t&&j==u || i+1==t&&j==u ||
                                   i==t&&j-1==u || i==t&&j+1==u) ){
            field[1][i][j]++;
          }*/
          int uguu = 0;
          if (field[0][i][j] > 0) {
            for (int g = 0; g < 4; g++) {
              tx = j + dx[g];
              ty = i + dy[g];
              if ((tx < 1 || ty < 1 || tx >= 4 || ty >= 4) ||
                  ty == t && tx == u) {
                uguu++;
              }
            }

            field[1][i][j] += field[0][i][j] * uguu;
          }
        }
      }
      /*
      for(int ii=1;ii<4;ii++){
        for(int jj=1;jj<4;jj++){
          printf("%7ld ",field[1][ii][jj]);
        }
        puts("");
      }
      puts("");*/
      for (int i = 1; i < 4; i++)
        for (int j = 1; j < 4; j++)
          field[0][i][j] = field[1][i][j], field[1][i][j] = 0;
    }

    printf("%.8lf\\n", (double)field[0][q][p] / pow(4.0, (double)n));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

long cnt, as;
char s, t, b, field[3][3];
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};

void search(int x, int y, int c) {
  int tx, ty;
  if (c == 0.0) {
    if (field[y][x] == 2)
      as++;
    cnt++;
    return;
  }
  for (int k = 0; k < 4; k++) {
    tx = x + dx[k];
    ty = y + dy[k];
    if (tx < 0 || ty < 0 || tx >= 3 || ty >= 3) {
      cnt++;
      continue;
    }
    if (field[ty][tx] == 1) {
      search(x, y, c - 1);
    } else {
      search(tx, ty, c - 1);
    }
  }
}
int main() {
  int n;
  for (; cin >> n, n;) {
    cin.ignore();
    cnt = as = 0;
    scanf(" %c %c %c ", &s, &t, &b);
    memset(field, 0, sizeof(field));
    int x = s == 'A' || s == 'D' || s == 'G'   ? 0
            : s == 'B' || s == 'E' || s == 'H' ? 1
                                               : 2;
    int y = s == 'A' || s == 'B' || s == 'C'   ? 0
            : s == 'D' || s == 'E' || s == 'F' ? 1
                                               : 2;

    int p = t == 'A' || t == 'D' || t == 'G'   ? 0
            : t == 'B' || t == 'E' || t == 'H' ? 1
                                               : 2;
    int q = t == 'A' || t == 'B' || t == 'C'   ? 0
            : t == 'D' || t == 'E' || t == 'F' ? 1
                                               : 2;
    field[q][p] = 2;

    int u = b == 'A' || b == 'D' || b == 'G'   ? 0
            : b == 'B' || b == 'E' || b == 'H' ? 1
                                               : 2;
    int t = b == 'A' || b == 'B' || b == 'C'   ? 0
            : b == 'D' || b == 'E' || b == 'F' ? 1
                                               : 2;
    field[t][u] = 1;

    search(x, y, n);
    // printf("%lf,%d\\n",pow(4.0,(double)n),as);
    printf("%.8lf\\n", (double)as / pow(4.0, (double)n));
  }
  return 0;
}