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

// 49
#include <cstring>
#include <iomanip>
#include <iostream>

using namespace std;

#define tox(c) (((c) - 'A') % 3)
#define toy(c) (((c) - 'A') / 3)

int main() {
  for (int n; cin >> n, n;) {
    char s, t, b;
    double p[3][3] = {};
    cin >> s >> t >> b;
    p[toy(s)][tox(s)] = 1;
    while (n--) {
      double pt[3][3] = {};
      for (int y = 0; y < 3; y++) {
        for (int x = 0; x < 3; x++) {
          int d[] = {0, 1, 0, -1, 0};
          for (int i = 0; i < 4; i++) {
            int ny = y + d[i];
            int nx = x + d[i + 1];
            if (0 <= ny && ny < 3 && 0 <= nx && nx < 3 &&
                !(ny == toy(b) && nx == tox(b))) {
              pt[ny][nx] += p[y][x] / 4;
            } else {
              pt[y][x] += p[y][x] / 4;
            }
          }
        }
      }
      memcpy(p, pt, sizeof(p));
    }
    cout << fixed << setprecision(6) << p[toy(t)][tox(t)] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 49
#include <iomanip>
#include <iostream>

using namespace std;

int tx, ty, bx, by;

#define tox(c) (((c) - 'A') % 3)
#define toy(c) (((c) - 'A') / 3)

/*
int tox(char c){
  return (c-'A')%3;
}

int toy(char c){
  return (c-'A')/3;
}
*/

double cal(int r, int y, int x, double p) {
  if (r == 0) {
    return (y == ty && x == tx) ? p : 0;
  } else {
    double a = 0;
    for (int i = 0; i < 4; i++) {
      static const int d[] = {0, 1, 0, -1, 0};
      int nx = x + d[i];
      int ny = y + d[i + 1];
      if (0 <= nx && nx < 3 && 0 <= ny && ny < 3 && !(nx == bx && ny == by)) {
        a += cal(r - 1, ny, nx, p / 4);
      } else {
        a += cal(r - 1, y, x, p / 4);
      }
    }
    return a;
  }
}

int main() {
  for (int n; cin >> n, n;) {
    char s, t, b;
    cin >> s >> t >> b;
    tx = tox(t);
    ty = toy(t);
    bx = tox(b);
    by = toy(b);
    cout << fixed << setprecision(6) << cal(n, toy(s), tox(s), 1) << endl;
  }
  return 0;
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int sum, ans;
int map[3][3];
int dp[3][3][15];
int move(int x, int y, int n) {
  int a = 0;
  //  cout<<x<<" "<<y<<" "<<n<<" "<<endl;
  if (dp[x][y][n] != -1)
    return dp[x][y][n];
  if (n == 0) {
    if (map[y][x] == 1)
      return 1;
    else
      return 0;
  }

  if (x - 1 >= 0 && map[y][x - 1] != -1)
    a += move(x - 1, y, n - 1);
  else
    a += move(x, y, n - 1);

  if (y - 1 >= 0 && map[y - 1][x] != -1)
    a += move(x, y - 1, n - 1);
  else
    a += move(x, y, n - 1);

  if (x + 1 < 3 && map[y][x + 1] != -1)
    a += move(x + 1, y, n - 1);
  else
    a += move(x, y, n - 1);

  if (y + 1 < 3 && map[y + 1][x] != -1)
    a += move(x, y + 1, n - 1);
  else
    a += move(x, y, n - 1);

  dp[x][y][n] = a;

  return a;
}

int main() {
  int n;

  for (;;) {
    cin >> n;
    if (n == 0)
      break;
    char s, t, b;
    sum = 0;
    ans = 0;
    cin >> s >> t >> b;
    memset(map, 0, sizeof(map));
    memset(dp, -1, sizeof(dp));
    int x, y, tm;
    tm = t - 'A';
    x = tm % 3;
    y = tm / 3;
    map[y][x] = 1;
    tm = b - 'A';
    x = tm % 3;
    y = tm / 3;
    map[y][x] = -1;

    tm = s - 'A';
    ans = move(tm % 3, tm / 3, n);
    sum = 4;
    for (int g = 1; g < n; g++)
      sum *= 4;
    cout << (double)ans / sum << endl;
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

int sum, ans;
int map[3][3];
void move(int x, int y, int n) {
  //  cout<<x<<" "<<y<<" "<<n<<" "<<endl;
  if (n == 0) {
    sum++;
    if (map[y][x] == 1)
      ans++;
    return;
  }

  if (x - 1 >= 0 && map[y][x - 1] != -1)
    move(x - 1, y, n - 1);
  else
    move(x, y, n - 1);

  if (y - 1 >= 0 && map[y - 1][x] != -1)
    move(x, y - 1, n - 1);
  else
    move(x, y, n - 1);

  if (x + 1 < 3 && map[y][x + 1] != -1)
    move(x + 1, y, n - 1);
  else
    move(x, y, n - 1);

  if (y + 1 < 3 && map[y + 1][x] != -1)
    move(x, y + 1, n - 1);
  else
    move(x, y, n - 1);
}

int main() {
  int n;

  for (;;) {
    cin >> n;
    if (n == 0)
      break;
    char s, t, b;
    sum = 0;
    ans = 0;
    cin >> s >> t >> b;
    memset(map, 0, sizeof(map));
    int x, y, tm;
    tm = t - 'A';
    x = tm % 3;
    y = tm / 3;
    map[y][x] = 1;
    tm = b - 'A';
    x = tm % 3;
    y = tm / 3;
    map[y][x] = -1;

    tm = s - 'A';
    move(tm % 3, tm / 3, n);

    cout << (double)ans / sum << endl;
  }
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

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

double dp[16][9];
int k, x, y, z;
int b[9][4] = {{1, 3, -1, -1}, {0, 2, 4, -1}, {1, 5, -1, -1},
               {0, 4, 6, -1},  {1, 3, 5, 7},  {2, 4, 8, -1},
               {3, 7, -1, -1}, {4, 6, 8, -1}, {5, 7, -1, -1}};
int main() {
  while (cin >> k, k) {
    memset(dp, 0, sizeof(dp));
    char a, b, c;
    cin >> a >> b >> c;
    dp[0][a - 'A'] = 1;
    y = b - 'A';
    z = c - 'A';
    double S = 0;
    for (int i = 0; i < k; i++) {
      for (int j = 0; j < 9; j++) {
        for (int l = 0; l < 4; l++) {
          if (::b[j][l] == -1 || ::b[j][l] == z) {
            dp[i + 1][j] += dp[i][j] * 0.25;
          } else {
            dp[i + 1][::b[j][l]] += dp[i][j] * 0.25;
          }
        }
      }
    }
    printf("%.10lf\\n", dp[k][y]);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

int k, x, y, z;
double S;
int b[9][4] = {{1, 3, -1, -1}, {0, 2, 4, -1}, {1, 5, -1, -1},
               {0, 4, 6, -1},  {1, 3, 5, 7},  {2, 4, 8, -1},
               {3, 7, -1, -1}, {4, 6, 8, -1}, {5, 7, -1, -1}};
void saiki(int c, int d, double e) { // c?????????d?????§e????¢??????§
  if (c == k) {
    if (d == y)
      S += e;
    return;
  }
  for (int i = 0; i < 4; i++) {
    if (b[d][i] == -1 || b[d][i] == z)
      saiki(c + 1, d, e * 0.25);
    else
      saiki(c + 1, b[d][i], e * 0.25);
  }
}
int main() {
  while (cin >> k, k) {
    S = 0;
    char a, b, c;
    cin >> a >> b >> c;
    x = a - 'A';
    y = b - 'A';
    z = c - 'A';
    saiki(0, x, 1.0);
    printf("%.10lf\\n", S);
  }
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

const int tx[] = {0, 1, 0, -1};
const int ty[] = {-1, 0, 1, 0};

char stage[3][3];
double prob[17][3][3];

int main() {
  int n;
  while (~scanf("%d", &n)) {
    if (n == 0)
      break;

    char s, t, b;
    memset(stage, '.', sizeof(stage));
    cin >> s >> t >> b;
    int sx = (s - 'A') % 3;
    int sy = (s - 'A') / 3;
    int gx = (t - 'A') % 3;
    int gy = (t - 'A') / 3;
    stage[(b - 'A') / 3][(b - 'A') % 3] = 'B';

    fill(prob[0][0], prob[0][0] + 3 * 3 * 17, 0.0);

    prob[0][sy][sx] = 1.0;

    for (int i = 0; i < n; i++) {
      for (int y = 0; y < 3; y++) {
        for (int x = 0; x < 3; x++) {
          for (int j = 0; j < 4; j++) {
            int dx = x + tx[j];
            int dy = y + ty[j];
            if ((dx < 0 || dx >= 3 || dy < 0 || dy >= 3) ||
                stage[dy][dx] == 'B')
              prob[i + 1][y][x] += prob[i][y][x] / 4.0;
            else
              prob[i + 1][dy][dx] += prob[i][y][x] / 4.0;
          }
        }
      }
    }

    printf("%lf\\n", prob[n][gy][gx]);
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

const int tx[] = {0, 1, 0, -1};
const int ty[] = {-1, 0, 1, 0};

char stage[3][3];

int gAll = 0, gOk = 0;

void dfs(int sx, int sy, int gx, int gy, int cost) {

  if (cost == 0) {
    gAll++;
    if (sx == gx && sy == gy) {
      gOk++;
    }
    return;
  }

  for (int i = 0; i < 4; i++) {
    int dx = sx + tx[i];
    int dy = sy + ty[i];
    if (dx < 0 || dx >= 3 || dy < 0 || dy >= 3) {
      dfs(sx, sy, gx, gy, cost - 1);
    }

    else if (stage[dy][dx] != 'B') {
      dfs(dx, dy, gx, gy, cost - 1);
    } else if (stage[dy][dx] == 'B') {
      dfs(sx, sy, gx, gy, cost - 1);
    }
  }
}

int main() {
  int n;
  while (~scanf("%d", &n)) {
    if (n == 0)
      break;

    char s, t, b;
    memset(stage, '.', sizeof(stage));
    cin >> s >> t >> b;
    int sx = (s - 'A') % 3;
    int sy = (s - 'A') / 3;
    int gx = (t - 'A') % 3;
    int gy = (t - 'A') / 3;
    stage[(b - 'A') / 3][(b - 'A') % 3] = 'B';

    gOk = 0;
    gAll = 0;
    dfs(sx, sy, gx, gy, n);

    printf("%lf\\n", (double)gOk / (double)gAll);
  }
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>

using namespace std;

int result[15][9];
int able[9][4];
char t;

int solve(int battery, char room, int dst) {
  if (able[room - 'A'][dst]) {
    if (dst == 0)
      room -= 3;
    else if (dst == 1)
      ++room;
    else if (dst == 2)
      room += 3;
    else
      --room;
  }

  --battery;
  if (battery == 0) {
    return room == t;
  } else {
    if (result[battery][room - 'A'] != -1)
      return result[battery][room - 'A'];

    result[battery][room - 'A'] = 0;
    for (int i = 0; i < 4; ++i)
      result[battery][room - 'A'] += solve(battery, room, i);

    return result[battery][room - 'A'];
  }
}

int main() {
  int n;
  char s;

  while (scanf("%d", &n), n) {
    char b;
    scanf(" %c %c %c", &s, &t, &b);

    for (int i = 0; i < 9; ++i) {
      for (int j = 0; j < 4; ++j)
        able[i][j] = 1;

      if (i < 3)
        able[i][0] = 0;
      if (i > 5)
        able[i][2] = 0;
      if (i % 3 == 0)
        able[i][3] = 0;
      if (i % 3 == 2)
        able[i][1] = 0;
    }

    b -= 'A';
    if (b >= 3)
      able[b - 3][2] = 0;
    if (b <= 5)
      able[b + 3][0] = 0;
    if (b % 3 != 0)
      able[b - 1][1] = 0;
    if (b % 3 != 2)
      able[b + 1][3] = 0;

    for (int i = 0; i < 15; ++i) {
      for (int j = 0; j < 9; ++j)
        result[i][j] = -1;
    }

    int acc = 0;
    ;
    for (int i = 0; i < 4; ++i)
      acc += solve(n, s, i);

    printf("%lf\\n", acc / pow(4.0, n));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int able[9][4];
char t;
int acc, fal;

void solve(int battery, char room, int dst) {
  if (able[room - 'A'][dst]) {
    if (dst == 0)
      room -= 3;
    else if (dst == 1)
      ++room;
    else if (dst == 2)
      room += 3;
    else
      --room;
  }

  --battery;
  if (battery == 0) {
    if (room == t)
      ++acc;
    else
      ++fal;
  } else {
    for (int i = 0; i < 4; ++i)
      solve(battery, room, i);
  }
}

int main() {
  int n;
  char s;

  while (scanf("%d", &n), n) {
    char b;
    scanf(" %c %c %c", &s, &t, &b);
    acc = fal = 0;

    for (int i = 0; i < 9; ++i) {
      for (int j = 0; j < 4; ++j)
        able[i][j] = 1;

      if (i < 3)
        able[i][0] = 0;
      if (i > 5)
        able[i][2] = 0;
      if (i % 3 == 0)
        able[i][3] = 0;
      if (i % 3 == 2)
        able[i][1] = 0;
    }

    b -= 'A';
    if (b >= 3)
      able[b - 3][2] = 0;
    if (b <= 5)
      able[b + 3][0] = 0;
    if (b % 3 != 0)
      able[b - 1][1] = 0;
    if (b % 3 != 2)
      able[b + 1][3] = 0;

    for (int i = 0; i < 4; ++i)
      solve(n, s, i);

    printf("%lf\\n", (double)acc / (acc + fal));
  }
  return 0;
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
int initial, battery, junk;
int dr[] = {-1, 0, 1, 0}, dc[] = {0, -1, 0, 1};
double memo[3][3][16];
double func(int r, int c, int n) {
  if (n == 0)
    return (3 * r + c == battery);
  double &res = memo[r][c][n];
  if (res > -0.5)
    return res;

  res = 0;
  for (int i = 0; i < 4; ++i) {
    int nr = r + dr[i], nc = c + dc[i];
    if (nr < 0 || nr >= 3 || nc < 0 || nc >= 3 || 3 * nr + nc == junk)
      res += func(r, c, n - 1) / 4;
    else
      res += func(nr, nc, n - 1) / 4;
  }
  return res;
}
int main() {
  int n;
  while (scanf("%d ", &n), n) {
    for (int i = 0; i < 3; ++i)
      for (int j = 0; j < 3; ++j)
        for (int k = 0; k < 16; ++k)
          memo[i][j][k] = -1;

    char s, t, b;
    scanf("%c %c %c ", &s, &t, &b);
    initial = s - 'A';
    battery = t - 'A';
    junk = b - 'A';
    printf("%.10f\\n", func(initial / 3, initial % 3, n));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
int initial, battery, junk;
int dr[] = {-1, 0, 1, 0}, dc[] = {0, -1, 0, 1};
double func(int r, int c, int n) {
  if (n == 0)
    return (3 * r + c == battery);
  double res = 0;
  for (int i = 0; i < 4; ++i) {
    int nr = r + dr[i], nc = c + dc[i];
    if (nr < 0 || nr >= 3 || nc < 0 || nc >= 3 || 3 * nr + nc == junk)
      res += func(r, c, n - 1) / 4;
    else
      res += func(nr, nc, n - 1) / 4;
  }
  return res;
}
int main() {
  int n;
  while (scanf("%d ", &n), n) {
    char s, t, b;
    scanf("%c %c %c ", &s, &t, &b);
    initial = s - 'A';
    battery = t - 'A';
    junk = b - 'A';
    printf("%.10f\\n", func(initial / 3, initial % 3, n));
  }
  return 0;
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000
typedef long long ll;

int n;
int sx, sy, gx, gy, bx, by;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double dp[3][3][16];

// 残りk回動けて今x, yにいるとき最後充電スペースにいける確率
double dfs(int x, int y, int k) {
  if (dp[x][y][k] >= 0)
    return dp[x][y][k];
  if (k == 0) {
    if (x == gx && y == gy)
      return 1;
    else
      return 0;
  }
  double ret = 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)
      ret += dfs(x, y, k - 1) / 4;
    else if (nx == bx && ny == by)
      ret += dfs(x, y, k - 1) / 4;
    else
      ret += dfs(nx, ny, k - 1) / 4;
  }
  return dp[x][y][k] = ret;
}

int main(void) {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    char s, t, b;
    cin >> s >> t >> b;
    sx = (int)(s - 'A') % 3;
    sy = (int)(s - 'A') / 3;
    gx = (int)(t - 'A') % 3;
    gy = (int)(t - 'A') / 3;
    bx = (int)(b - 'A') % 3;
    by = (int)(b - 'A') / 3;
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        for (int k = 0; k < 16; k++)
          dp[i][j][k] = -1;
    printf("%.10f\\n", dfs(sx, sy, n));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000
typedef long long ll;

int n;
int sx, sy, gx, gy, bx, by;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

// 残りk回動けて今x, yにいるとき最後充電スペースにいける確率
double dfs(int x, int y, int k) {
  if (k == 0) {
    if (x == gx && y == gy)
      return 1;
    else
      return 0;
  }
  double ret = 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)
      ret += dfs(x, y, k - 1) / 4;
    else if (nx == bx && ny == by)
      ret += dfs(x, y, k - 1) / 4;
    else
      ret += dfs(nx, ny, k - 1) / 4;
  }
  return ret;
}

int main(void) {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    char s, t, b;
    cin >> s >> t >> b;
    sx = (int)(s - 'A') % 3;
    sy = (int)(s - 'A') / 3;
    gx = (int)(t - 'A') % 3;
    gy = (int)(t - 'A') / 3;
    bx = (int)(b - 'A') % 3;
    by = (int)(b - 'A') / 3;
    cout << dfs(sx, sy, n) << endl;
  }
  return 0;
}
Problem p00606 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};
int dp[3][3][16];
int main() {
  int n;
  while (cin >> n, n) {
    char s, t, b;
    cin >> s >> t >> b;
    memset(dp, 0, sizeof(dp));
    dp[(s - 'A') / 3][(s - 'A') % 3][0] = 1;
    rep(k, n) {
      rep(i, 3) {
        rep(j, 3) {
          if (!dp[i][j][k])
            continue;
          rep(l, 4) {
            int nx = i + dx[l], ny = j + dy[l];
            if (nx < 0 || ny < 0 || nx >= 3 || ny >= 3 ||
                'A' + nx * 3 + ny == b)
              dp[i][j][k + 1] += dp[i][j][k];
            else
              dp[nx][ny][k + 1] += dp[i][j][k];
          }
        }
      }
    }
    int sum = 0;
    rep(i, 3) rep(j, 3) sum += dp[i][j][n];
    printf("%.7lf\\n",
           (double)dp[(t - 'A') / 3][(t - 'A') % 3][n] / (double)sum);
  };
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<char, ll> P;

char dir[4][4]{"ABC", "DEF", "GHI"};
ll dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};
int main() {
  ll n;
  while (cin >> n, n) {
    char s, t, b;
    cin >> s >> t >> b;
    ll sum = 0, cnt = 0;
    queue<P> que;
    que.push(P(s, n));
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      if (!p.second) {
        sum++;
        if (p.first == t)
          cnt++;
        continue;
      }
      ll x = (p.first - 'A') / 3, y = (p.first - 'A') % 3;
      rep(i, 4) {
        ll nx = x + dx[i], ny = y + dy[i];
        if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3 || dir[nx][ny] == b) {
          nx = x;
          ny = y;
        }
        que.push(P(dir[nx][ny], p.second - 1));
      }
    }
    if (sum == 0)
      puts("0.00000000");
    else
      printf("%.7lf\\n", (double)cnt / (double)sum);
  }
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;
int mp[3][3];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int ans, all;
int n;
char s, t, b;
int memo[16][3][3];
int visited[16][3][3];
void saiki(int x, int y, int bat) {
  visited[bat][x][y] = 1;
  if (n == bat) {
    if ((t - 'A') % 3 == x && (t - 'A') / 3 == y)
      memo[bat][x][y] = 1;
    return;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (((b - 'A') % 3 == nx && (b - 'A') / 3 == ny) || nx < 0 || ny < 0 ||
        nx >= 3 || ny >= 3) {
      if (visited[bat + 1][x][y] == 1)
        memo[bat][x][y] += memo[bat + 1][x][y];
      else {
        saiki(x, y, bat + 1);
        if (memo[bat + 1][x][y] > 0)
          memo[bat][x][y] += memo[bat + 1][x][y];
      }
    } else {
      if (visited[bat + 1][nx][ny])
        memo[bat][x][y] += memo[bat + 1][nx][ny];
      else {
        saiki(nx, ny, bat + 1);
        if (memo[bat + 1][nx][ny] > 0)
          memo[bat][x][y] += memo[bat + 1][nx][ny];
      }
    }
  }
}

int main() {

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    ans = all = 0;
    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++)
          memo[i][j][k] = visited[i][j][k] = 0;
    saiki((s - 'A') % 3, (s - 'A') / 3, 0);
    ans = memo[0][(s - 'A') % 3][(s - 'A') / 3];
    printf("%.8f\\n", 1.0 * ans / (1 << (2 * n)));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int mp[3][3];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int ans, all;
int n;
char s, t, b;
void saiki(int x, int y, int bat) {
  if (n == bat) {
    all++;
    if ((t - 'A') % 3 == x && (t - 'A') / 3 == y)
      ans++;
    return;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || ny < 0 || nx >= 3 || ny >= 3)
      saiki(x, y, bat + 1);
    else if ((b - 'A') % 3 == nx && (b - 'A') / 3 == ny)
      saiki(x, y, bat + 1);
    else
      saiki(nx, ny, bat + 1);
  }
}

int main() {

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    ans = all = 0;
    cin >> s >> t >> b;
    saiki((s - 'A') % 3, (s - 'A') / 3, 0);
    printf("%.8f\\n", 1.0 * ans / all);
  }
  return 0;
}
Problem p00606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

double prob(int n, int pos, int t, int d) {
  double map[9] = {0};
  map[pos] = 1;
  for (int i = 0; i < n; i++) {
    double nmap[9] = {0};
    for (int j = 0; j < 9; j++) {
      if ((j - 3 < 0) || (j - 3 == d)) {
        nmap[j] += map[j] / 4.0;
      } else {
        nmap[j - 3] += map[j] / 4.0;
      }
      if ((j % 3 == 0) || (j - 1 == d)) {
        nmap[j] += map[j] / 4.0;
      } else {
        nmap[j - 1] += map[j] / 4.0;
      }
      if ((j % 3 == 2) || (j + 1 == d)) {
        nmap[j] += map[j] / 4.0;
      } else {
        nmap[j + 1] += map[j] / 4.0;
      }
      if ((j + 3 > 8) || (j + 3 == d)) {
        nmap[j] += map[j] / 4.0;
      } else {
        nmap[j + 3] += map[j] / 4.0;
      }
    }
    for (int j = 0; j < 9; j++) {
      map[j] = nmap[j];
    }
  }
  return map[t];
}

int main() {
  vector<double> r;
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    char a[2], b[2], c[2];
    int s = 0, t = 0, d = 0;
    scanf("%s %s %s", a, b, c);
    s = a[0] - 'A';
    t = b[0] - 'A';
    d = c[0] - 'A';
    r.push_back(prob(n, s, t, d));
  }
  for (int i = 0; i < r.size(); i++) {
    cout << setprecision(10) << r[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

double prob(int n, int pos, int t, int d) {
  if (n == 0) {
    if (pos == t) {
      return 1;
    } else {
      return 0;
    }
  }
  double p = 0;
  if ((pos - 3 < 0) || (pos - 3 == d)) {
    p += prob(n - 1, pos, t, d) / 4.0;
  } else {
    p += prob(n - 1, pos - 3, t, d) / 4.0;
  }
  if ((pos % 3 == 0) || (pos - 1 == d)) {
    p += prob(n - 1, pos, t, d) / 4.0;
  } else {
    p += prob(n - 1, pos - 1, t, d) / 4.0;
  }
  if ((pos % 3 == 2) || (pos + 1 == d)) {
    p += prob(n - 1, pos, t, d) / 4.0;
  } else {
    p += prob(n - 1, pos + 1, t, d) / 4.0;
  }
  if ((pos + 3 > 8) || (pos + 3 == d)) {
    p += prob(n - 1, pos, t, d) / 4.0;
  } else {
    p += prob(n - 1, pos + 3, t, d) / 4.0;
  }
  return p;
}

int main() {
  vector<double> r;
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    char a[2], b[2], c[2];
    int s = 0, t = 0, d = 0;
    scanf("%s %s %s", a, b, c);
    s = a[0] - 'A';
    t = b[0] - 'A';
    d = c[0] - 'A';
    r.push_back(prob(n, s, t, d));
  }
  for (int i = 0; i < r.size(); i++) {
    cout << setprecision(10) << r[i] << endl;
  }
  return 0;
}