alexjercan/bugnet huggingface dataset:
Problem p00606 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define fr first
#define sc second
typedef pair<int, int> Pt;
int main() {
  map<char, Pt> tmp;
  for (int i = 0; i < 9; i++) {
    tmp['A' + i] = Pt(i / 3, i % 3);
  }

  char s, t, b;
  Pt st, gt, bt;
  int n, d[] = {0, 1, 0, -1};
  while (cin >> n && n) {
    double dp[16][4][4] = {{{}}};
    cin >> s >> t >> b;
    st = tmp[s], gt = tmp[t], bt = tmp[b];
    dp[0][st.fr][st.sc] = 1.0;
    for (int i = 0; i < n; i++) {
      for (int x = 0; x < 4; x++) {
        for (int y = 0; y < 4; y++) {
          for (int j = 0; j < 4; j++) {
            int px = x + d[j], py = y + d[3 - j];
            if (px < 0 || px > 2 || py < 0 || py > 2 || bt == Pt(px, py)) {
              px = x, py = y;
            }
            dp[i + 1][px][py] += dp[i][x][y] / 4.0;
          }
        }
      }
    }
    cout << fixed << setprecision(8) << dp[n][gt.fr][gt.sc] << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second
typedef pair<int, int> Pi;
int n;
char s, t, d;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
void solve() {
  int goal[3][3] = {{}};
  map<char, pair<int, int>> tmp;
  tmp['A'] = Pi(0, 0);
  tmp['B'] = Pi(0, 1);
  tmp['C'] = Pi(0, 2);
  tmp['D'] = Pi(1, 0);
  tmp['E'] = Pi(1, 1);
  tmp['F'] = Pi(1, 2);
  tmp['G'] = Pi(2, 0);
  tmp['H'] = Pi(2, 1);
  tmp['I'] = Pi(2, 2);
  queue<pair<int, Pi>> que;
  que.push(mp(n, tmp[s]));

  while (!que.empty()) {
    pair<int, Pi> p = que.front();
    que.pop();
    if (p.fr == 0) {
      goal[p.sc.fr][p.sc.sc]++;
      continue;
    }
    for (int i = 0; i < 4; i++) {
      int nx = dx[i] + p.sc.fr, ny = dy[i] + p.sc.sc;
      if (nx >= 0 && nx < 3 && ny >= 0 && ny < 3 && tmp[d] != Pi(nx, ny)) {
        que.push(mp(p.fr - 1, Pi(nx, ny)));
      } else {
        que.push(mp(p.fr - 1, Pi(p.sc.fr, p.sc.sc)));
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++)
      sum += goal[i][j];
  }
  printf("%.7f\\n", (double)goal[tmp[t].fr][tmp[t].sc] / (double)sum);
}

int main() {
  while (cin >> n, n) {
    cin >> s >> t >> d;
    solve();
  }
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <iostream>
#define N 3
#define N1 16
using namespace std;
void func(int, int, int);
int n, k, data[N][N], memo[N1][N][N];
int dy[N + 1] = {-1, 0, 1, 0};
int dx[N + 1] = {0, 1, 0, -1};
bool visited[N1][N][N];
char s, t, b;

int main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    cin >> s >> t >> b;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        data[i][j] = 0;
        for (int k = 0; k < N1; k++) {
          memo[k][i][j] = 0;
          visited[k][i][j] = false;
        }
      }
    }
    data[(t - 'A') / N][(t - 'A') % N] = 1;
    data[(b - 'A') / N][(b - 'A') % N] = -1;
    func((s - 'A') / N, (s - 'A') % N, 0);
    k = pow(4, n);
    printf("%.8f\\n", 1.0 * memo[0][(s - 'A') / N][(s - 'A') % N] / k);
  }
  return 0;
}

void func(int y, int x, int d) {
  visited[d][y][x] = true;
  if (d == n) {
    if (data[y][x] == 1)
      memo[d][y][x] = 1;
    return;
  }
  for (int i = 0; i < N + 1; i++) {
    int ny = y + dy[i], nx = x + dx[i];
    if (ny < 0 || nx < 0 || N <= ny || N <= nx || data[ny][nx] == -1) {
      if (visited[d + 1][y][x] == false)
        func(y, x, d + 1);
      memo[d][y][x] += memo[d + 1][y][x];
    } else {
      if (visited[d + 1][ny][nx] == false)
        func(ny, nx, d + 1);
      memo[d][y][x] += memo[d + 1][ny][nx];
    }
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#define N 3
#define N1 16
using namespace std;
void func(int, int, int);
int n, k, data[N][N], memo[N1][N][N];
int dy[N + 1] = {-1, 0, 1, 0};
int dx[N + 1] = {0, 1, 0, -1};
bool visited[N1][N][N];
char s, t, b;

int main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    cin >> s >> t >> b;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        data[i][j] = 0;
        for (int k = 0; k < N1; k++) {
          memo[k][i][j] = 0;
          visited[k][i][j] = false;
        }
      }
    }
    data[(t - 'A') / N][(t - 'A') % N] = 1;
    data[(b - 'A') / N][(b - 'A') % N] = -1;
    func((s - 'A') / N, (s - 'A') % N, 0);
    k = pow(4, n);
    printf("%.8f\\n", 1.0 * memo[0][(s - 'A') / N][(s - 'A') % N] / k);
  }
  return 0;
}

void func(int y, int x, int d) {
  visited[d + 1][y][x] = true;
  if (d == n) {
    if (data[y][x] == 1)
      memo[d][y][x] = 1;
    return;
  }
  for (int i = 0; i < N + 1; i++) {
    int ny = y + dy[i], nx = x + dx[i];
    if (ny < 0 || nx < 0 || N < ny || N < nx || data[ny][nx] == -1) {
      if (visited[d + 1][y][x] == false)
        func(y, x, d + 1);
      memo[d][y][x] += memo[d + 1][y][x];
    } else {
      if (visited[d + 1][ny][nx] == false)
        func(ny, nx, d + 1);
      memo[d][y][x] += memo[d + 1][ny][nx];
    }
  }
}
Problem p00606 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

double solve(int n, int s, int t, int b) {
  vector<double> probs(9, 0.0);
  probs[s] = 1.0;

  for (int i = 0; i < n; i++) {
    vector<double> next(9, 0.0);
    for (int j = 0; j < 9; j++) {
      for (int d = 0; d < 4; d++) {
        static const int di[] = {-1, 1, 0, 0};
        static const int dj[] = {0, 0, 1, -1};
        const int k = (j / 3) + di[d];
        const int l = (j % 3) + dj[d];
        if (k < 0 || 3 <= k || l < 0 || 3 <= l || 3 * k + l == b) {
          next[j] += probs[j] * 0.25;
        } else {
          next[3 * k + l] += probs[j] * 0.25;
        }
      }
    }
    probs = next;
  }

  return probs[t];
}

int main() {
  int n;
  while (cin >> n && n != 0) {
    char s, t, b;
    cin >> s >> t >> b;
    printf("%.8f\\n", solve(n, s - 'A', t - 'A', b - 'A'));
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

double solve(int n, int s, int t, int b) {
  queue<pair<int, int>> q;
  q.push(make_pair(n, s));
  int cnt = 0;
  while (!q.empty()) {
    const pair<int, int> p = q.front();
    q.pop();
    if (p.first == 0) {
      if (p.second == t) {
        ++cnt;
      }
    } else {
      for (int d = 0; d < 4; d++) {
        static const int di[] = {-1, 1, 0, 0};
        static const int dj[] = {0, 0, -1, 1};
        const int k = (p.second / 3) + di[d];
        const int l = (p.second % 3) + dj[d];
        if (k < 0 || 3 <= k || l < 0 || 3 <= l) {
          q.push(make_pair(p.first - 1, p.second));
        } else if (3 * k + l == b) {
          q.push(make_pair(p.first - 1, p.second));
        } else {
          q.push(make_pair(p.first - 1, 3 * k + l));
        }
      }
    }
  }
  return static_cast<double>(cnt) / pow(4.0, static_cast<double>(n));
}

int main() {
  int n;
  while (cin >> n && n != 0) {
    char s, t, b;
    cin >> s >> t >> b;
    printf("%.8f\\n", solve(n, s - 'A', t - 'A', b - 'A'));
  }
  return 0;
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#define BAT 1
#define SOU 2
using namespace std;

int main() {
  int n, sum;
  char s, t, b;
  int mem[20][9];

  while (cin >> n && n) {
    cin >> s >> t >> b;
    int souko = b - 'A';
    int goal = t - 'A';

    for (int i = 0; i <= n; i++)
      for (int j = 0; j < 9; j++)
        mem[i][j] = 0;

    mem[0][s - 'A'] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 9; j++) {
        if (mem[i][j] == 0)
          continue;
        int k = i + 1;
        // up
        if (j < 3 || j - 3 == souko)
          mem[k][j] += mem[i][j];
        else
          mem[k][j - 3] += mem[i][j];
        // right
        if (j % 3 == 2 || j + 1 == souko)
          mem[k][j] += mem[i][j];
        else
          mem[k][j + 1] += mem[i][j];
        // down
        if (j > 5 || j + 3 == souko)
          mem[k][j] += mem[i][j];
        else
          mem[k][j + 3] += mem[i][j];
        // left
        if (j % 3 == 0 || j - 1 == souko)
          mem[k][j] += mem[i][j];
        else
          mem[k][j - 1] += mem[i][j];
      }
    }
    /*
    for( int i=0;i<=n;i++ ){
      cout << "n="<<i << "|"<<endl ;
      for( int j=0;j<9;j++ ){
        cout << " "<<mem[i][j];
        if( j%3==2 ) cout << endl;
      }
      cout << endl;
    }
    */
    sum = 0;
    for (int i = 0; i < 9; i++)
      sum += mem[n][i];
    /*
    cout << "souko==" << souko << " goal="<<goal<<endl;
    cout << "dev " << mem[n][goal] << "/"<<sum <<endl;
    */
    printf("%lf\\n", (double)mem[n][goal] / sum);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#define BAT 1
#define SOU 2
using namespace std;

int r[9];
int deno; // ツ閉ェツ陛ェ
int num;  // ツ閉ェツ子

void rc(int n, int i) {
  if (n == 0) {
    deno++;
    if (r[i] == BAT)
      num++;
  } else {
    --n;
    // up
    if (i < 3 || r[i - 3] == SOU)
      rc(n, i);
    else
      rc(n, i - 3);
    // right
    if (i % 3 == 2 || r[i + 1] == SOU)
      rc(n, i);
    else
      rc(n, i + 1);
    // down
    if (i > 5 || r[i + 3] == SOU)
      rc(n, i);
    else
      rc(n, i + 3);
    // left
    if (i % 3 == 0 || r[i - 1] == SOU)
      rc(n, i);
    else
      rc(n, i - 1);
  }
}

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

  while (cin >> n && n) {
    cin >> s >> t >> b;
    for (int i = 0; i < 9; i++)
      r[i] = 0;
    r[t - 'A'] = BAT;
    r[b - 'A'] = SOU;
    deno = 0;
    num = 0;
    rc(n, s - 'A');
    double ans = (double)num / deno;
    printf("%lf\\n", ans);
  }
  return 0;
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
const double PI = acos(-1);

int n;
char s, t, b;
pair<int, int> S, T, B;
double dp[16][3][3];

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

int main() {
  while (cin >> n && n) {
    cin >> s >> t >> b;
    S = make_pair((s - 'A') / 3, (s - 'A') % 3);
    T = make_pair((t - 'A') / 3, (t - 'A') % 3);
    B = make_pair((b - 'A') / 3, (b - 'A') % 3);
    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;
        }
      }
    }
    dp[0][S.first][S.second] = 1;
    for (int i = 0; i < n; i++) {
      for (int y = 0; y < 3; y++) {
        for (int x = 0; x < 3; x++) {
          if (dp[i][y][x] == 0)
            continue;
          for (int d = 0; d < 4; d++) {
            int ny = y + dy[d], nx = x + dx[d];
            if (0 <= nx && nx < 3 && 0 <= ny && ny < 3 &&
                make_pair(ny, nx) != B)
              dp[i + 1][ny][nx] += dp[i][y][x] * 0.25;
            else
              dp[i + 1][y][x] += dp[i][y][x] * 0.25;
          }
        }
      }
    }
    cout << fixed << setprecision(10) << dp[n][T.first][T.second] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
const double PI = acos(-1);

int n;
char s, t, b;
pair<int, int> S, T, B;
int counter[10][10];

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

void dfs(pair<int, int> P, int count) {
  if (count == n) {
    counter[P.first][P.second]++;
    return;
  }
  for (int i = 0; i < 4; i++) {
    int nx = P.second + dx[i], ny = P.first + dy[i];
    if (0 <= nx && nx < 3 && 0 <= ny && ny < 3 && make_pair(ny, nx) != B)
      dfs(make_pair(ny, nx), count + 1);
    else
      dfs(P, count + 1);
  }
}

int main() {
  while (cin >> n && n) {
    cin >> s >> t >> b;
    S = make_pair((s - 'A') / 3, (s - 'A') % 3);
    T = make_pair((t - 'A') / 3, (t - 'A') % 3);
    B = make_pair((b - 'A') / 3, (b - 'A') % 3);
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        counter[i][j] = 0;
      }
    }
    dfs(S, 0);
    double sum = 0, ans = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (make_pair(i, j) == T)
          ans += counter[i][j];
        sum += counter[i][j];
      }
    }
    cout << fixed << setprecision(10) << ans / sum << endl;
  }
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ck(a) (0 <= (a) && (a) < 9)

int di[] = {-3, -1, 1, 3};

int main() {
  int n, s, t, b;
  char cs, ct, cb;
  while (scanf("%d", &n)) {
    if (n == 0)
      break;
    getchar();
    scanf("%c %c %c", &cs, &ct, &cb);
    s = cs - 'A', t = ct - 'A', b = cb - 'A';
    double p[9] = {0};
    p[s] = 1.0;

    while (n--) {
      double q[9] = {0};
      rep(i, 9) rep(d, 4) {
        if (ck(i + di[d]) && !(i % 3 == 0 && di[d] == -1) &&
            !(i % 3 == 2 && di[d] == 1) && !(i / 3 == 0 && di[d] == -3) &&
            !(i / 3 == 2 && di[d] == 3) && i + di[d] != b)
          q[i + di[d]] += p[i] / 4;
        else
          q[i] += p[i] / 4;
      }
      rep(i, 9) p[i] = q[i];
    }
    printf("%.8lf\\n", p[t]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ck(a) (0 <= (a) && (a) < 9)

int di[] = {-3, -1, 1, 3};

int main() {
  int n, s, t, b;
  char cs, ct, cb;
  while (scanf("%d", &n), n) {
    scanf("%c %c %c", &cs, &ct, &cb);
    s = cs - 'A', t = ct - 'A', b = cb - 'A';
    double p[9] = {0};
    p[s] = 1;
    while (n--) {
      double q[9] = {0};
      rep(i, 9) rep(d, 4) {
        if (ck(i + di[d]) && !(i % 3 == 0 && di[d] == -1) &&
            (i % 3 == 2 && di[i] == 1) && i + di[d] != b)
          q[i + di[d]] += p[i];
        else
          q[i] += p[i] / 4;
      }
      rep(i, 9) p[i] = q[i];
    }
    printf("%.8lf\\n", p[t]);
  }
  return 0;
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int dp[20][3][3];
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;
    char s, t, b;
    cin >> s >> t >> b;
    int sx, sy, tx, ty, bx, by;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        int num = 3 * i + j;
        if (num == s - 'A') {
          sy = i;
          sx = j;
        }
        if (num == t - 'A') {
          ty = i;
          tx = j;
        }
        if (num == b - 'A') {
          by = i;
          bx = j;
        }
      }
    }
    for (int i = 0; i < 20; i++)
      for (int j = 0; j < 3; j++)
        for (int k = 0; k < 3; k++)
          dp[i][j][k] = 0;
    dp[0][sy][sx] = 1;
    for (int i = 0; i < n; i++) {
      for (int y = 0; y < 3; y++) {
        for (int x = 0; x < 3; x++) {
          if (dp[i][y][x] == 0)
            continue;
          for (int l = 0; l < 4; l++) {
            int ny = y + dy[l];
            int nx = x + dx[l];
            if (ny < 0 || 3 <= ny || nx < 0 || 3 <= nx) {
              dp[i + 1][y][x] += dp[i][y][x];
            } else if (bx == nx && by == ny) {
              dp[i + 1][y][x] += dp[i][y][x];
            } else {
              dp[i + 1][ny][nx] += dp[i][y][x];
            }
          }
        }
      }
    }
    printf("%.15lf\\n", (double)dp[n][ty][tx] / pow(4, n));
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int grd[3][3];
double dp[3][3][20];
int mx[] = {-1, 1, 0, 0}, my[] = {0, 0, -1, 1};
char abc[3][4] = {"ABC", "DEF", "GHI"};
double dfs(int x, int y, int n) {
  if (dp[y][x][n] != 0)
    return dp[y][x][n];
  if (n == 0) {
    if (grd[x][y] == 1)
      return dp[y][x][n] = 1.0;
    else
      return dp[y][x][n] = 0.0;
  }
  double ret = 0.0;
  for (int i = 0; i < 4; i++) {
    int cx = x + mx[i], cy = y + my[i];
    if (cx < 0 || 2 < cx || cy < 0 || 2 < cy)
      ret += dfs(x, y, n - 1) * 0.25;
    else if (grd[cy][cx] == 2)
      ret += dfs(x, y, n - 1) * 0.25;
    else
      ret += dfs(cx, cy, n - 1) * 0.25;
  }
  return dp[y][x][n] = ret;
}
int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;
    char s, t, b;
    scanf(" %c %c %c", &s, &t, &b);
    memset(grd, 0, sizeof(grd));
    memset(dp, 0, sizeof(dp));
    int sx, sy;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (abc[i][j] == s) {
          sx = j;
          sy = i;
        } else if (abc[i][j] == t)
          grd[i][j] = 1;
        else if (abc[i][j] == b)
          grd[i][j] = 2;
      }
    }
    printf("%.15lf\\n", dfs(sx, sy, n));
  }
}
Problem p00606 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int dp[16][5][5];
bool grid[5][5];
const int px[] = {1, 1, 1, 2, 2, 2, 3, 3, 3};
const int py[] = {1, 2, 3, 1, 2, 3, 1, 2, 3};
const int dx[] = {-1, 0, 1, 0, 0};
const int dy[] = {0, -1, 0, 1, 0};

int main() {
  int n;
  while (scanf("%d", &n), n) {
    memset(dp, 0, sizeof(dp));
    memset(grid, true, sizeof(grid));

    char s, t, b;
    scanf(" %c %c %c", &s, &t, &b);
    s -= 'A';
    t -= 'A';
    b -= 'A';

    for (int i = 0; i < 9; i++) {
      grid[px[i]][py[i]] = (b == i);
    }

    dp[0][px[s]][py[s]] = 1;
    for (int i = 0; i < n; i++) {
      for (int x = 1; x <= 3; x++) {
        for (int y = 1; y <= 3; y++) {
          bool f = false;
          for (int dir = 0; dir < 4; dir++) {
            int tx = x + dx[dir];
            int ty = y + dy[dir];
            if (grid[tx][ty]) {
              // if (f) continue;
              dp[i + 1][x][y] += dp[i][x][y];
              // f = true;
            } else
              dp[i + 1][tx][ty] += dp[i][x][y];
          }
        }
      }
    }

    int sum = 0;
    int num = 0;
    for (int i = 0; i < 9; i++) {
      sum += dp[n][px[i]][py[i]];
      num += dp[n][px[i]][py[i]] * (i == t);
    }

    double res = (double)num / sum;
    // printf("%d %d\\n", num, sum);

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

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

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

struct POINT {
  int x, y;

  bool operator<(const POINT t) const {
    if (x == t.x)
      return x < t.x;
    return y < t.y;
  }

  bool operator==(const POINT t) const { return (x == t.x && y == t.y); }
};
typedef pair<POINT, int> P;

POINT toCoor(char c) {
  c -= 'A';
  return {c / 3, c % 3};
}

int main() {
  int n;
  while (scanf("%d", &n), n) {
    char A, B, C;
    scanf(" %c %c %c", &A, &B, &C);

    POINT s, t, b;
    s = toCoor(A);
    t = toCoor(B);
    b = toCoor(C);

    queue<P> que;
    int cnt[3][3] = {0};
    for (que.push(P(s, 0)); que.size(); que.pop()) {
      P p = que.front();

      if (p.second == n) {
        // printf("%d %d\\n", p.first.x, p.first.y);
        cnt[p.first.x][p.first.y]++;
        continue;
      }

      for (int i = 0; i < 4; i++) {
        int tx = p.first.x + dx[i];
        int ty = p.first.y + dy[i];
        if (tx < 0 || tx >= 3 || ty < 0 || ty >= 3 || b == (POINT){tx, ty}) {
          que.push(P(p.first, p.second + 1));
        } else {
          que.push(P({tx, ty}, p.second + 1));
        }
      }
    }

    int sum = 0, a = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        // printf("%d", cnt[i][j]);
        sum += cnt[i][j];
        if (t == (POINT){i, j})
          a += cnt[i][j];
      }
      // puts("");
    }

    // printf("%d %d\\n", a, sum);
    printf("%.8f\\n", (double)a / (double)sum);
  }

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

#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
typedef pair<int, int> P;
char s, t, b;
P dp[20][5][5];
char fld[5][5] = {"ABC", "DEF", "GHI"};
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
P rep(int now, int y, int x) {
  int X, Y, l, r;
  if (dp[now][y][x].second >= 0)
    return dp[now][y][x];
  P pre, sum = P(0, 0);
  if (now == 0) {
    if (fld[y][x] == t) {
      return dp[now][y][x] = P(1, 1);
    }
    return dp[now][y][x] = P(0, 1);
  }
  for (int i = 0; i < 4; i++) {
    X = x + dx[i];
    Y = y + dy[i];
    if (X >= 0 && X < 3 && Y >= 0 && Y < 3 && fld[Y][X] != b) {
      pre = rep(now - 1, Y, X);
      sum = P(sum.first + pre.first, sum.second + pre.second);
    } else {
      pre = rep(now - 1, y, x);
      sum = P(sum.first + pre.first, sum.second + pre.second);
    }
  }
  return dp[now][y][x] = sum;
}
int main() {
  int n;
  P ans;
  while (cin >> n, n) {
    cin >> s >> t >> b;
    for (int i = 0; i <= n; i++)
      for (int j = 0; j < 3; j++)
        for (int k = 0; k < 3; k++) {
          dp[i][j][k].first = -1;
          dp[i][j][k].second = -1;
        }
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++) {
        if (fld[i][j] == s) {
          rep(n, i, j);
          ans = P(dp[n][i][j].first, dp[n][i][j].second);
        }
      }
    printf("%.8lf\\n", (double)ans.first / ans.second);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
char s, t, b;
char fld[5][5] = {"ABC", "DEF", "GHI"};
int ans, sum, dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
void rep(int now, int y, int x) {
  int X, Y;
  if (now == 0) {
    sum++;
    if (fld[y][x] == t) {
      ans++;
    }
    return;
  }
  for (int i = 0; i < 4; i++) {
    X = x + dx[i];
    Y = y + dy[i];
    if (X >= 0 && X < 3 && Y >= 0 && Y < 3 && fld[Y][X] != b) {
      rep(now - 1, Y, X);
    } else {
      rep(now - 1, y, x);
    }
  }
}
int main() {
  int n;
  while (cin >> n, n) {
    cin >> s >> t >> b;
    sum = 0;
    ans = 0;
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++) {
        if (fld[i][j] == s)
          rep(n, i, j);
      }
    cout << ans << ' ' << sum << endl;
    printf("%.8lf\\n", (double)ans / sum);
  }
  return 0;
}