alexjercan/bugnet huggingface dataset:
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define BLANK 0
#define STORE 1
#define CHARGER 2

int main() {
  int n;
  char s, t, b;

  cout << setprecision(8);
  cout << setiosflags(ios::fixed);

  while (cin >> n, n) {
    int room[3 + 2][3 + 2] = {0};
    double dp[16][3 + 2][3 + 2] = {0};
    int dx[4] = {-1, 1, 0, 0};
    int dy[4] = {0, 0, -1, 1};
    int i, j, y, x;

    cin >> s >> t >> b;
    s -= 'A';
    t -= 'A';
    b -= 'A';
    for (i = 0; i < 3 + 2; i++) {
      room[0][i] = STORE;
      room[i][0] = STORE;
      room[3 + 2 - 1][i] = STORE;
      room[i][3 + 2 - 1] = STORE;
    }
    room[(t / 3) + 1][(t % 3) + 1] = CHARGER;
    room[(b / 3) + 1][(b % 3) + 1] = STORE;
    dp[0][(s / 3) + 1][(s % 3) + 1] = 1.0;

    for (i = 1; i <= n; i++) {
      for (y = 1; y <= 3; y++) {
        for (x = 1; x <= 3; x++) {
          for (j = 0; j < 4; j++) {
            int yy = y + dy[j], xx = x + dx[j];
            if (room[yy][xx] == STORE) {
              yy = y;
              xx = x;
            }
            dp[i][yy][xx] += dp[i - 1][y][x] / 4.0;
          }
        }
      }
    }
    cout << dp[n][(t / 3) + 1][(t % 3) + 1] << endl;
  }
}

Error: TLE

Fixed submission:

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

#define CHARGER 1
#define STORE 2

int room[3][3];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
int cnt, cnt2;

void rec(int n, int x, int y) {
  int i, xx, yy;

  if (n == 0) {
    cnt++;
    if (room[x][y] == CHARGER)
      cnt2++;
    return;
  }

  n--;

  for (i = 0; i < 4; i++) {
    xx = x + dx[i];
    yy = y + dy[i];
    if (xx < 0 || 3 <= xx || yy < 0 || 3 <= yy) {
      xx -= dx[i];
      yy -= dy[i];
    }
    if (room[x][y] == STORE) {
      xx -= dx[i];
      yy -= dy[i];
    }
    rec(n, xx, yy);
  }
}

int main() {
  int n, i, j;
  char s, t, b;
  cout << setprecision(8);
  cout << setiosflags(ios::fixed);
  while (cin >> n, n) {
    cin >> s >> t >> b;
    for (i = 0; i < 3; i++)
      for (j = 0; j < 3; j++)
        room[i][j] = 0;
    s -= 'A';
    t -= 'A';
    b -= 'A';
    room[t % 3][t / 3] = CHARGER;
    room[b % 3][b / 3] = STORE;
    cnt = cnt2 = 0;
    rec(n, s % 3, s / 3);
    cout << (double)cnt2 / cnt << endl;
  }
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
using namespace std;
char map[3][3] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'};
double dp[16][3][3];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int main() {
  int n;
  char s, t, d;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    cin >> s >> t >> d;
    for (int i = 0; i <= n; i++)
      for (int f = 0; f < 3; f++)
        for (int g = 0; g < 3; g++)
          dp[i][f][g] = 0.0;
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        if (map[i][j] == s)
          dp[0][i][j] = 1.0;
    for (int i = 0; i < n; i++) {
      for (int f = 0; f < 3; f++) {
        for (int g = 0; g < 3; g++) {
          for (int j = 0; j < 4; j++) {
            bool move = false;
            if (f + dy[j] >= 0 && f + dy[j] < 3 && g + dx[j] >= 0 &&
                g + dx[j] < 3) {
              if (map[f + dy[j]][g + dx[j]] != d)
                move = true;
            }
            if (move)
              dp[i + 1][f + dy[j]][g + dx[j]] += dp[i][f][g] / 4.0;
            else
              dp[i + 1][f][g] += dp[i][f][g] / 4.0;
          }
        }
      }
    }
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        if (map[i][j] == t)
          cout << fixed << setprecision(10) << dp[n][i][j] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
char map[3][3] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'};
int n, ans;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
char s, t, d;
void DFS(int y, int x, int len) {
  if (len == 0) {
    if (map[y][x] == t)
      ans++;
    return;
  }
  for (int i = 0; i < 4; i++) {
    if (y + dy[i] == -1 || y + dy[i] == 3 || x + dx[i] == -1 || x + dx[i] == 3)
      DFS(y, x, len - 1);
    else {
      if (map[y + dy[i]][x + dx[i]] == d)
        DFS(y, x, len - 1);
      else
        DFS(y + dy[i], x + dx[i], len - 1);
    }
  }
}
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    cin >> s >> t >> d;
    ans = 0;
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        if (map[i][j] == s)
          DFS(i, j, n);
    double cnt = 1.0;
    for (int i = 0; i < n; i++)
      cnt *= 4.0;
    cout << fixed << setprecision(10) << ans / cnt << endl;
  }
  return 0;
}
Problem p00606 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

int main() {
  int n;
  while (cin >> n, n) {
    long double dp[16][5][5] = {{{0.0}}};
    char map[5][5];
    rep(i, 5) rep(j, 5) map[i][j] = 'X';
    rep(i, 3) rep(j, 3) map[i + 1][j + 1] = ' ';
    int sx, sy, gx, gy, bx, by;
    char s, t, b;
    cin >> s >> t >> b;
    sy = (s - 'A') / 3 + 1, sx = (s - 'A') % 3 + 1;
    gy = (t - 'A') / 3 + 1, gx = (t - 'A') % 3 + 1;
    by = (b - 'A') / 3 + 1, bx = (b - 'A') % 3 + 1;

    map[by][bx] = 'X';
    dp[n][sy][sx] = 1.0;

    while (n) {
      for (int y = 1; y <= 3; y++) {
        for (int x = 1; x <= 3; x++) {
          rep(i, 4) {
            int tx = x + dx[i], ty = y + dy[i];
            if (map[ty][tx] == 'X')
              ty = y, tx = x;
            dp[n - 1][ty][tx] += dp[n][y][x] / 4.0;
          }
        }
      }
      n--;
    }
    printf("%.8Lf\\n", dp[0][gy][gx]);
  }
}


Error: WA

Fixed submission:

#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

int main() {
  int n;
  while (cin >> n, n) {
    long double dp[15][5][5] = {{{0.0}}};
    char map[5][5];
    rep(i, 5) rep(j, 5) map[i][j] = 'X';
    rep(i, 3) rep(j, 3) map[i + 1][j + 1] = ' ';
    int sx, sy, gx, gy, bx, by;
    char s, t, b;
    cin >> s >> t >> b;
    sy = (s - 'A') / 3 + 1, sx = (s - 'A') % 3 + 1;
    gy = (t - 'A') / 3 + 1, gx = (t - 'A') % 3 + 1;
    by = (b - 'A') / 3 + 1, bx = (b - 'A') % 3 + 1;
    map[by][bx] = 'X';
    // rep(i,5){rep(j,5)cout << map[i][j];cout << endl;}

    dp[n][sy][sx] = 1.0;

    while (n) {
      for (int y = 1; y <= 3; y++) {
        for (int x = 1; x <= 3; x++) {
          rep(i, 4) {
            int tx = x + dx[i], ty = y + dy[i];
            if (map[ty][tx] == 'X')
              continue;
            dp[n - 1][ty][tx] += dp[n][y][x] / 4.0;
          }
        }
      }
      n--;
    }
    printf("%.10Lf\\n", dp[0][gy][gx]);
  }
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

#define pii pair<int, int>

int main() {
  int n;
  int dx[] = {0, 1, 0, -1};
  int dy[] = {-1, 0, 1, 0};

  while (cin >> n, n) {
    char cs, ct, cb;
    cin >> cs >> ct >> cb;
    int s = cs - 'A', t = ct - 'A', b = cb - 'A';
    int sx = s % 3, sy = s / 3;
    int tx = t % 3, ty = t / 3;
    int bx = b % 3, by = b / 3;

    double dp[3][3][n + 1];

    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] = 0;

    dp[sx][sy][0] = 1;
    double p = 1;
    int dx[] = {0, 1, 0, -1};
    int dy[] = {-1, 0, 1, 0};

    for (int i = 1; i <= n; i++) {
      for (int x = 0; x < 3; x++) {
        for (int y = 0; y < 3; y++) {
          if (x == bx && y == by)
            continue;
          for (int j = 0; j < 4; j++) {
            int xx = x + dx[j];
            int yy = y + dy[j];
            if (xx < 0 || 3 <= xx || yy < 0 || 3 <= yy ||
                (xx == bx && yy == by)) {
              xx = x;
              yy = y;
            }
            dp[x][y][i] += dp[xx][yy][i - 1] * 0.25;
          }
        }
      }
    }

    printf("%.8f\\n", dp[tx][ty][n]);
  }
}

Error: TLE

Fixed submission:

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

#define pii pair<int, int>

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int tx, ty, bx, by;
double ans;

void rec(int nx, int ny, int v, double p) {
  // printf("(%d, %d), %d, %f\\n", nx,ny,v,p);
  if (v == 0) {
    if (nx == tx && ny == ty) {
      ans += p;
    }
    return;
  }
  for (int i = 0; i < 4; i++) {
    int xx = nx + dx[i];
    int yy = ny + dy[i];
    if (xx < 0 || 3 < xx || yy < 0 || 3 < yy || (xx == bx && yy == by)) {
      xx = nx;
      yy = ny;
    }
    rec(xx, yy, v - 1, p * 0.25);
  }
}

int main() {
  int n;
  int dx[] = {0, 1, 0, -1};
  int dy[] = {-1, 0, 1, 0};

  while (cin >> n, n) {
    char cs, ct, cb;
    cin >> cs >> ct >> cb;
    int s = cs - 'A', t = ct - 'A', b = cb - 'A';
    tx = t % 3;
    ty = t / 3;
    bx = b % 3;
    by = b / 3;

    // printf("%d,%d  %d,%d\\n", tx,ty,bx,by);

    ans = 0;
    rec(s % 3, s / 3, n, 1);

    printf("%.8f\\n", ans);
  }
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int pow(int x, int n) {
  int ans = 1;
  for (int i = 0; i < n; i++) {
    ans *= x;
  }
  return ans;
}

int main() {
  static int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  while (1) {
    int room[3][3];
    fill(&room[0][0], &room[2][2] + 1, 0);
    int n;
    cin >> n;
    if (n == 0)
      break;

    char s, t, b;
    cin >> s >> t >> b;
    int temp = (int)s - (int)'A';
    int sx = temp % 3;
    int sy = temp / 3;
    temp = (int)t - (int)'A';
    int gx = temp % 3;
    int gy = temp / 3;
    temp = (int)b - (int)'A';
    room[temp / 3][temp % 3] = 2;

    double value[3][3];
    fill(&value[0][0], &value[2][2] + 1, 0);
    value[sy][sx] = 1;
    for (int t = 0; t < n; t++) {
      double newValue[3][3];
      fill(&newValue[0][0], &newValue[2][2] + 1, 0);
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          for (int k = 0; k < 4; k++) {
            int nx = j + d[k][0];
            int ny = i + d[k][1];
            if (nx < 0 || nx >= 3)
              nx = j;
            if (ny < 0 || ny >= 3)
              ny = i;
            if (room[ny][nx] == 2) {
              nx = j;
              ny = i;
            }
            newValue[ny][nx] += value[i][j] / 4;
          }
        }
      }
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          value[i][j] = newValue[i][j];
        }
      }
    }
    printf("%.8f\\n", value[gy][gx]);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int room[3][3];
int ok;
int all;

void rec(int x, int y, int but) {
  static int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  if (but == 0) {
    if (room[y][x] == 1)
      ok++;
    all++;
    return;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + d[i][0];
    int ny = y + d[i][1];
    if (nx < 0 || nx >= 3)
      rec(x, y, but - 1);
    else if (ny < 0 || nx >= 3)
      rec(x, y, but - 1);
    else if (room[ny][nx] == 2)
      rec(x, y, but - 1);
    else
      rec(nx, ny, but - 1);
  }
}

int main() {
  while (1) {
    fill(&room[0][0], &room[2][2] + 1, 0);
    int n;
    cin >> n;
    if (n == 0)
      break;

    char s, t, b;
    cin >> s >> t >> b;
    int temp = (int)s - (int)'A';
    int sx = temp % 3;
    int sy = temp / 3;
    temp = (int)t - (int)'A';
    room[temp / 3][temp % 3] = 1;
    temp = (int)b - (int)'A';
    room[temp / 3][temp % 3] = 2;

    ok = all = 0;
    rec(sx, sy, n);

    printf("%.8f\\n", (double)ok / all);
  }
  return 0;
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>

using namespace std;

typedef pair<int, int> P;

int n, p, q;
char s, t, b;
double dp[16][3][3];
map<char, P> mpp;
int xdir[4] = {0, 1, 0, -1}, ydir[4] = {1, 0, -1, 0};

void init() {
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
      mpp[(char)('A' + 3 * i + j)] = P(i, j);

  return;
}

void solve() {
  for (; n; --n)
    for (int i = 0; i < 3; ++i)
      for (int j = 0; j < 3; ++j)
        for (int k = 0; k < 4; ++k) {
          int dy = i + ydir[k], dx = j + xdir[k];
          if (!(0 <= dx && dx <= 2 && 0 <= dy && dy <= 2) ||
              (dy == p && dx == q)) {
            dy = i;
            dx = j;
          }
          dp[n - 1][dy][dx] += dp[n][i][j] / 4.0;
        }

  return;
}

int main() {
  init();
  while (cin >> n && n) {
    cin >> s >> t >> b;
    fill(&dp[0][0][0], &dp[16][0][0], 0.0);
    p = mpp[s].first;
    q = mpp[s].second;
    dp[n][p][q] = 1.0;
    p = mpp[b].first;
    q = mpp[b].second;
    solve();
    p = mpp[t].first;
    q = mpp[t].second;

    printf("%.8lf\\n", dp[0][p][q]);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <utility>

using namespace std;

typedef pair<int, int> Pa;

struct step {
  Pa p;
  int t;
};

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

Pa func(string str) {
  if (str == "A") {
    return Pa(0, 0);
  } else if (str == "B") {
    return Pa(0, 1);
  } else if (str == "C") {
    return Pa(0, 2);
  } else if (str == "D") {
    return Pa(1, 0);
  } else if (str == "E") {
    return Pa(1, 1);
  } else if (str == "F") {
    return Pa(1, 2);
  } else if (str == "G") {
    return Pa(2, 0);
  } else if (str == "H") {
    return Pa(2, 1);
  } else {
    return Pa(2, 2);
  }
}

double bfs(int a[3][3], Pa st, int n) {
  double rate = 0.0;
  queue<step> que;
  step s, tmp;
  s.p = st;
  s.t = 0;
  que.push(s);

  while (1) {
    s = que.front();
    if (s.t == n) {
      break;
    }
    que.pop();

    for (int i = 0; i < 4; i++) {
      int y = s.p.first + dy[i], x = s.p.second + dx[i];
      if (0 <= x && x < 3 && 0 <= y && y < 3) {
        if (a[y][x] == 2) {
          y = s.p.first;
          x = s.p.second;
        }
        tmp.p = Pa(y, x);
        tmp.t = s.t + 1;
        que.push(tmp);
      }
    }
  }

  while (!que.empty()) {
    s = que.front();
    que.pop();
    if (a[s.p.first][s.p.second] == 1) {
      rate += 1.0;
    }
  }

  return rate / (1 << (2 * n));
}

int main() {
  int n, a[3][3];
  string s, t, u;
  Pa p;

  while (1) {
    cin >> n;
    if (n == 0) {
      break;
    }
    cin >> s >> t >> u;
    fill(&a[0][0], &a[2][2], 0);
    p = func(t);
    a[p.first][p.second] = 1;
    p = func(u);
    a[p.first][p.second] = 2;

    printf("%.8f\\n", bfs(a, func(s), n));
  }

  return 0;
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
#include <string.h>

using namespace std;

int n;
char room[10] = "ABCDEFGHI";
char s, t, b;
int off[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int x, y;

double table[3][3];

double solve() {
  memset(&table[0][0], 0, sizeof(double) * 9);
  table[y][x] = 1.0;
  for (int k = 0; k < n; k++) {
    double temp[3][3];
    memset(&temp[0][0], 0, sizeof(double) * 9);
    for (int j = 0; j < 3; j++) {
      for (int i = 0; i < 3; i++) {
        if (table[j][i] != 0.0) {
          for (int m = 0; m < 4; m++) {
            int xx = i + off[m][0];
            int yy = j + off[m][1];
            int index = xx + yy * 3;
            xx = (xx >= 0 && xx < 3 && room[index] != b) ? xx : i;
            yy = (yy >= 0 && yy < 3 && room[index] != b) ? yy : j;
            temp[yy][xx] += table[j][i] / 4.0;
          }
        }
      }
    }

    memcpy(&table[0][0], &temp[0][0], sizeof(double) * 9);
  }

  for (int i = 0; i < 9; i++) {
    if (room[i] == t)
      return table[i / 3][i % 3];
  }

  return 0.0;
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;

    cin >> s >> t >> b;
    for (int i = 0; i < 9; i++) {
      if (room[i] == s) {
        x = i % 3;
        y = i / 3;
      }
    }

    printf("%.8f\\n", solve());
  }

  return 0;
}

Error: TLE

Fixed submission:


#include <iostream>

using namespace std;

int n;
char room[10] = "ABCDEFGHI";
char s, t, b;
int off[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int x, y;

void simulate(int cx, int cy, int e, int &trial, int &success) {
  if (e == 0) {
    if (room[cx + cy * 3] == t)
      success++;
    trial++;
    return;
  }

  for (int i = 0; i < 4; i++) {
    int xx = cx + off[i][0];
    int yy = cy + off[i][1];
    int index = xx + yy * 3;
    if (room[index] == b || xx < 0 || yy < 0 || xx >= 3 || yy >= 3)
      simulate(x, y, e - 1, trial, success);
    else
      simulate(xx, yy, e - 1, trial, success);
  }
}

double solve() {
  int trial = 0;
  int success = 0;
  simulate(x, y, n, trial, success);
  return (double)success / (double)trial;
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;

    cin >> s >> t >> b;
    for (int i = 0; i < 9; i++) {
      if (room[i] == s) {
        x = i % 3;
        y = i / 3;
      }
    }

    printf("%.8f\\n", solve());
  }

  return 0;
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

long double dp[16][3][3] = {};
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int bx, by;
int tx, ty;

long double rec(int rest, int x, int y) {
  // cout<<"x:"<<x<<"y:"<<y<<endl;
  if (x < 0 || x > 2 || y < 0 || y > 2)
    return 0;
  if (dp[rest][x][y] > 0)
    return dp[rest][x][y];

  if (rest == 0) {
    if (x == tx && y == ty)
      return 1;
    else
      return 0;
  }
  long double sum = 0;
  for (int i = 0; i < 4; i++) {
    if (((x + dx[i] == bx) && (y + dy[i] == by)) || x + dx[i] < 0 ||
        x + dx[i] > 2 || y + dy[i] > 2 || y + dy[i] < 0) {
      sum += rec(rest - 1, x, y);
    } else {
      sum += rec(rest - 1, x + dx[i], y + dy[i]);
    }
  }
  sum = sum / 4.0;
  dp[rest][x][y] = sum;
  return sum;
}
void topoint(char a, int &x, int &y) {
  int tmp = a - 'A';
  x = tmp / 3;
  y = tmp % 3;
  return;
}
int main() {
  int N;
  cout.precision(8);
  cout.setf(ios::fixed);
  while (cin >> N, N) {
    char s, t, b;
    cin >> s >> t >> b;
    int sx, sy;
    topoint(s, sx, sy);
    topoint(t, tx, ty);
    topoint(b, bx, by);
    // cout<<sx<<":"<<sy<<" "<<tx<<":"<<ty<<endl;
    cout << rec(N, sx, sy) << endl;
    for (int i = 0; i < 16; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          dp[i][j][k] = 0;
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

double dp[15][3][3] = {};
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int bx, by;
int tx, ty;

double rec(int rest, int x, int y) {
  // cout<<"x:"<<x<<"y:"<<y<<endl;
  if (x < 0 || x > 2 || y < 0 || y > 2)
    return 0;
  // if(dp[rest][x][y]>0)return dp[rest][x][y];

  if (rest == 0) {
    if (x == tx && y == ty)
      return 1;
    else
      return 0;
  }
  double sum = 0;
  for (int i = 0; i < 4; i++) {
    if (((x + dx[i] == bx) && (y + dy[i] == by)) || x + dx[i] < 0 ||
        x + dx[i] > 2 || y + dy[i] > 2 || y + dy[i] < 0) {
      sum += rec(rest - 1, x, y);
    } else {
      sum += rec(rest - 1, x + dx[i], y + dy[i]);
    }
  }
  sum = sum / 4.0;
  dp[rest][x][y] = sum;
  return sum;
}
void topoint(char a, int &x, int &y) {
  int tmp = a - 'A';
  x = tmp / 3;
  y = tmp % 3;
  return;
}
int main() {
  int N;
  cout.precision(8);
  cout.setf(ios::fixed);
  while (cin >> N, N) {
    char s, t, b;
    cin >> s >> t >> b;
    int sx, sy;
    topoint(s, sx, sy);
    topoint(t, tx, ty);
    topoint(b, bx, by);
    // cout<<sx<<":"<<sy<<" "<<tx<<":"<<ty<<endl;
    cout << rec(N, sx, sy) << endl;
    for (int i = 0; i < 15; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          dp[i][j][k] = 0;
        }
      }
    }
  }
  return 0;
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<int, P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

double dp[3][3][16];
int n, s, t, b;

int toIndex(char l) { return l - 'A'; }

double rec(int x, int y, int battery) {
  if (dp[y][x][battery] > -1e-6) {
    return dp[y][x][battery];
  }
  if (x == t % 3 && y == t / 3 && battery == n) {
    return 1.0;
  }
  if (battery >= n) {
    return 0.0;
  }

  double res = 0.0;
  REP(i, 4) {
    int nx = x + dx[i], ny = y + dy[i];
    if (0 <= nx && nx < 3 && 0 <= ny && ny < 3 &&
        !(nx == b % 3 && ny == b / 3)) {
      res += 0.25 * rec(nx, ny, battery + 1);
    } else {
      res += 0.25 * rec(x, y, battery + 1);
    }
  }

  return dp[y][x][battery] = res;
}

int main() {
  while (std::cin >> n, n) {
    std::fill(dp[0][0], dp[0][0] + 3 * 3 * 16, -1.0);

    char sl, tl, bl;
    std::cin >> sl >> tl >> bl;

    s = toIndex(sl), t = toIndex(tl), b = toIndex(bl);

    printf("%.7f\\n", rec(s % 3, s / 3, 0));
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<int, P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

double dp[3][3][16];
int n, s, t, b;

int toIndex(char l) { return l - 'A'; }

double rec(int x, int y, int battery) {
  if (dp[y][x][battery] > -1e-6) {
    return dp[y][x][battery];
  }
  if (x == t % 3 && y == t / 3 && battery == n) {
    return 1.0;
  }
  if (battery >= n) {
    return 0.0;
  }

  double res = 0.0;
  REP(i, 4) {
    int nx = x + dx[i], ny = y + dy[i];
    if (0 <= nx && nx < 3 && 0 <= ny && ny < 3 &&
        !(nx == b % 3 && ny == b / 3)) {
      res += 0.25 * rec(nx, ny, battery + 1);
    } else {
      res += 0.25 * rec(x, y, battery + 1);
    }
  }

  return res;
}

int main() {
  while (std::cin >> n, n) {
    std::fill(dp[0][0], dp[0][0] + 3 * 3 * 16, -1.0);

    char sl, tl, bl;
    std::cin >> sl >> tl >> bl;

    s = toIndex(sl), t = toIndex(tl), b = toIndex(bl);

    printf("%.7f\\n", rec(s % 3, s / 3, 0));
  }
}