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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
// 参考http://topcoder.g.hatena.ne.jp/isa_rentacs/20111114/1321278656
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value))
template <typename T> void FILL_(void *ptr, size_t size, T value) {
  std::fill((T *)ptr, (T *)ptr + size, value);
}
// 4方向ベクトル→↑←↓
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

double dp[16][9];
int dir[4] = {1, -3, -1, 3};
int main() {
  int n;
  while (cin >> n && n) {
    FILL(dp, 0);
    char tmp;
    int s, t, b, x, y;
    cin >> tmp;
    s = (int)(tmp - 'A');
    cin >> tmp;
    t = (int)(tmp - 'A');
    cin >> tmp;
    b = (int)(tmp - 'A');

    dp[0][s] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 9; j++) {
        if (dp[i][j] == 0)
          continue;
        x = j % 3;
        y = j / 3;
        for (int k = 0; k < 4; k++) {
          if (0 <= x + dx[k] && x + dx[k] < 3 && 0 <= y + dy[k] &&
              y + dy[k] < 3 && j + dir[k] != b) {
            dp[i + 1][j + dir[k]] += (dp[i][j] * 0.25);
          } else {
            dp[i + 1][j] += (dp[i][j] * 0.25);
          }
        }
      }
    }
    printf("%.8f\\n", dp[n][t]);
  }
  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)

// 4方向ベクトル→↑←↓
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int n;
char s; // 最初にロボットがいる部屋
char t; // 充電器のある部屋
char b; // 倉庫として使っている部屋を示す大文字のアルファベット
char start;
char battery;
char souko;
int nenryo;
char board[3][3] = {{'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'I'}};
int main() {
  srand(time(NULL));
  while (cin >> n, n) {

    double ans = 0;
    scanf(" %c %c %c", &s, &t, &b);
    for (int k = 0; k < 9000000; k++) {
      int x;
      int y;
      start = s;
      battery = t;
      souko = b;
      nenryo = n;
      while (nenryo--) {
        for (int i = 0; i < 3; i++) {
          for (int j = 0; j < 3; j++) {
            if (board[i][j] == start) {
              x = j;
              y = i;
            }
          }
        }
        int ransu = rand() % 4;
        int nextx = x + dx[ransu];
        int nexty = y + dy[ransu];
        if (nexty < 0 || nextx > 2 || nexty < 0 || nexty > 2 ||
            board[nexty][nextx] == souko)
          continue;
        else
          start = board[nexty][nextx];
      }
      if (start == battery)
        ans++;
    }
    printf("%f\\n", ans / 9000000);
  }
  return 0;
}