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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>

using namespace std;

typedef long double ld;

const int EMPTY = 0;
const int BATTERY = 1;
const int STORE = 2;
const int WALL = 3;

struct Point {
public:
  int x, y;
  Point() {}
  Point(int x, int y) : x(x), y(y) {}
};

struct State {
public:
  Point p;
  int battery;
  State(){};
  State(Point p, int battery) : p(p), battery(battery) {}
};

Point int2point(int n) { return Point(1 + n % 3, 1 + n / 3); }

int field[5][5];

ld dp[16][5][5];
int main() {
  int n, s, t, b;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    char cs, ct, cb;
    cin >> cs >> ct >> cb;
    s = cs - 'A';
    t = ct - 'A';
    b = cb - 'A';

    memset(field, EMPTY, sizeof(field));
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < 5; i++) {
      field[0][i] = WALL;
      field[4][i] = WALL;
      field[i][0] = WALL;
      field[i][4] = WALL;
    }
    Point batteryPoint = int2point(t);
    field[batteryPoint.x][batteryPoint.y] = BATTERY;
    Point storePoint = int2point(b);
    field[storePoint.x][storePoint.y] = STORE;
    State nowState(int2point(s), n);

    dp[0][nowState.p.x][nowState.p.y] = 1.0f;
    int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
    for (int i = 0; i < nowState.battery; i++) {
      for (int j = 1; j <= 3; j++) {
        for (int k = 1; k <= 3; k++) {
          for (int l = 0; l < 4; l++) {
            int nx = j + dx[l], ny = k + dy[l];
            if (field[nx][ny] == STORE || field[nx][ny] == WALL) {
              nx = j, ny = k;
            }
            dp[i + 1][nx][ny] += dp[i][j][k] / 4.0;
          }
        }
      }
    }
    printf("%.15llf\\n", dp[nowState.battery][batteryPoint.x][batteryPoint.y]);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>

using namespace std;

const int EMPTY = 0;
const int BATTERY = 1;
const int STORE = 2;
const int WALL = 3;

struct Point {
public:
  int x, y;
  Point() {}
  Point(int x, int y) : x(x), y(y) {}
};

struct State {
public:
  Point p;
  int battery;
  State(){};
  State(Point p, int battery) : p(p), battery(battery) {}
};

Point int2point(int n) { return Point(1 + n % 3, 1 + n / 3); }

int main() {
  int n, s, t, b;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    char cs, ct, cb;
    cin >> cs >> ct >> cb;
    int count = 0, count2 = 0;
    s = cs - 'A';
    t = ct - 'A';
    b = cb - 'A';

    int field[5][5];
    memset(field, EMPTY, sizeof(field));
    for (int i = 0; i < 5; i++) {
      field[0][i] = WALL;
      field[4][i] = WALL;
      field[i][0] = WALL;
      field[4][i] = WALL;
    }
    Point batteryPoint = int2point(t);
    field[batteryPoint.x][batteryPoint.y] = BATTERY;
    Point storePoint = int2point(b);
    field[storePoint.x][storePoint.y] = STORE;
    State nowState(int2point(s), n);
    queue<State> q;
    q.push(nowState);

    while (!q.empty()) {
      State s = q.front();
      q.pop();
      if (s.battery == 0) {
        if (field[s.p.x][s.p.y] == BATTERY) {
          count2++;
        }
        count++;
        continue;
      }
      int dx[4] = {0, -1, 1, 0};
      int dy[4] = {-1, 0, 0, 1};
      for (int i = 0; i < 4; i++) {
        Point np = Point(s.p.x + dx[i], s.p.y + dy[i]);
        if (field[np.x][np.y] == STORE) {
          q.push(State(s.p, s.battery - 1));
          continue;
        }
        if (field[np.x][np.y] == WALL) {
          q.push(State(s.p, s.battery - 1));
          continue;
        }
        q.push(State(np, s.battery - 1));
      }
    }
    printf("%.15lf\\n", (double)count2 / count);
  }
  return 0;
}