Problem p00606 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
// AOJ1020
// 10 A E C ->0.12537479
int n, c;
char s, t, b;
double p, p1, nn;
int d1[4] = {0, 1, 0, -1}, d2[4] = {1, 0, -1, 0};
char map[3][3] = {{'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'I'}};
double df[10][16]; // df[i][j]=i番の部屋にバッテリーjでいたときの確率
void dfs(int y, int x, int n1) {
  double p1;
  int st = y * 3 + x;
  if (n1 == 0) {
    if (map[y][x] == t) {
      p += pow(0.25, nn);
      c++;
    }
    return;
  } else {
    if (df[st][n1] >= 0) {
      p += df[st][n1];
      c++;
      return;
    } else {
      for (int i = 0; i < 4; i++) {
        int my = y + d1[i], mx = x + d2[i];
        if (0 <= my && my <= 2 && 0 <= mx && mx <= 2) { // 外壁にはぶつからない
          if (map[my][mx] == b) {                       // 倉庫にぶつかる
            p1 = p;
            dfs(y, x, n1 - 1);
            df[st][n1 - 1] = p - p1;
          } else { // 普通に進む
            p1 = p;
            dfs(my, mx, n1 - 1);
            df[my * 3 + mx][n1 - 1] = p - p1;
          }
        } else { // 外壁にぶつかる
          p1 = p;
          dfs(y, x, n1 - 1);
          df[st][n1 - 1] = p - p1;
        }
      }
    }
  }
  return;
}

int main() {
  while (1) {
    int y1, x1;
    p = 0;
    c = 0;
    cin >> n;
    nn = (double)n;
    if (n == 0)
      break;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 16; j++) {
        df[i][j] = -1;
      }
    }
    cin >> s >> t >> b;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (map[i][j] == s) {
          y1 = i;
          x1 = j;
        }
      }
    }
    dfs(y1, x1, n);
    printf("%f\\n", p);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int s, t, b, n;
double p, p1, nn;
int d[4] = {1, 3, -1, -3};
double df[10][20];
void dfs(int s, int n) {
  if (n == 0) {
    if (s == t) {
      p += pow(0.25, nn);
    }
    return;
  }
  if (df[s][n] >= 0) {
    p += df[s][n];
    return;
  } else {
    for (int i = 0; i < 4; i++) {
      if (s + d[i] == b) {
        p1 = p;
        dfs(s, n - 1);
        df[s][n - 1] = p - p1;
      } else if (s / 3 != (s + d[i]) / 3) {
        if (d[i] * d[i] == 9 && 0 <= s + d[i] && s + d[i] < 9) {
          p1 = p;
          dfs(s + d[i], n - 1);
          df[s + d[1]][n - 1] = p - p1;
        } else {
          p1 = p;
          dfs(s, n - 1);
          df[s][n - 1] = p - p1;
        }
      } else {
        p1 = p;
        dfs(s + d[i], n - 1);
        df[s + d[i]][n - 1] = p - p1;
      }
    }
  }
  return;
}
int main() {
  while (1) {
    char cs, ct, cb;
    p = 0;
    cin >> n;
    nn = n;
    if (n == 0)
      break;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 20; j++) {
        df[i][j] = -1;
      }
    }
    cin >> cs >> ct >> cb;
    s = (int)cs - 'A';
    t = (int)ct - 'A';
    b = (int)cb - 'A';
    dfs(s, n);
    cout << p << endl;
  }
  return 0;
}