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

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

typedef vector<int> VI;
typedef vector<VI> VVI;

const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};

int get_x(char c) {
  if (c == 'A' || c == 'D' || c == 'G')
    return 0;
  if (c == 'B' || c == 'E' || c == 'H')
    return 1;
  if (c == 'C' || c == 'F' || c == 'I')
    return 2;
  return -1;
}

int get_y(char c) {
  if (c == 'A' || c == 'B' || c == 'C')
    return 0;
  if (c == 'D' || c == 'E' || c == 'F')
    return 1;
  if (c == 'G' || c == 'H' || c == 'I')
    return 2;
  return -1;
}

int main(void) {
  int n;
  char s, t, b;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cin >> s >> t >> b;

    int sx, sy, gx, gy, ox, oy;
    sx = get_x(s);
    sy = get_y(s);
    gx = get_x(t);
    gy = get_y(t);
    ox = get_x(b);
    oy = get_y(b);

    VVI prev(3, VI(3, 0)), next;
    prev[sy][sx] = 1;
    for (int lpc = 0; lpc < n; lpc++) {
      next = VVI(3, VI(3, 0));
      for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++) {
          if (i == oy && j == ox)
            continue;
          int stop = 0;
          for (int k = 0; k < 4; k++) {
            int nx = j + dx[k];
            int ny = i + dy[k];
            if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3) {
              stop++;
              continue;
            } else if (nx == ox && ny == oy) {
              stop++;
              continue;
            }
            next[i][j] += prev[ny][nx];
          }
          if (stop)
            next[i][j] += prev[i][j] * stop;
        }
      prev = next;
    }

    int sum = 0;
    // for ( int i = 0; i < 3; i++ ) for ( int j = 0; j < 3; j++ ) sum +=
    // next[i][j];
    sum = 1;
    for (int i = 0; i < n; i++)
      sum *= 4;

    double answer = (double)next[gy][gx] / (double)sum;

    cout.precision(8);
    cout.setf(ios::fixed);
    cout << answer << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

string M[3] = {"ABC", "DEF", "GHI"};

vector<vector<bool>> V;

char s, t, b;
int g;
int cnt;

char get(const int x, const int y) { return M[y][x]; }

void visit(const int x, const int y) { V[y][x] = true; }

bool is_visited(const int x, const int y) { return V[y][x]; }

void search(int &x, int &y, const char c) {
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      if (get(i, j) == c) {
        x = i;
        y = j;
        return;
      }
    }
  }
}

void bf(const int x, const int y, const int n) {
  if (n == 0) {
    if (get(x, y) == t)
      g++;
    cnt++;
    return;
  }

  const int dx[4] = {0, 0, 1, -1};
  const int dy[4] = {1, -1, 0, 0};

  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];

    if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3) {
      bf(x, y, n - 1);
      continue;
    }

    if (get(nx, ny) == b) {
      bf(x, y, n - 1);
      continue;
    }

    bf(nx, ny, n - 1);
  }
  // bf( x, y, n - 1 );
}

int main(void) {
  int n;
  while (cin >> n && n != 0) {
    cin >> s >> t >> b;

    int x, y;

    cnt = 0;
    g = 0;
    search(x, y, s);

    V = vector<vector<bool>>(3, vector<bool>(3, false));

    bf(x, y, n);

    double probability;
    if (cnt != 0)
      probability = (double)g / (double)cnt;

    cout.setf(ios::fixed);
    cout.precision(8);
    cout << probability << endl;
  }
  return 0;
}