Problem p00037 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define DEBUG(x) cerr << #x << " = " << x << endl

int main() {
  static bool wall[6][6][4];
  const int dx[4] = {1, 0, -1, 0};
  const int dy[4] = {0, 1, 0, -1};
  REP(i, 6) REP(j, 6) REP(k, 4) {
    int ni = i + dy[k];
    int nj = j + dx[k];
    if (0 <= ni && ni < 6 && 0 <= nj && nj < 6) {
      wall[i][j][k] = 0;
    } else {
      wall[i][j][k] = 1;
    }
  }

  string s;
  for (int i = 0; i < 4; i++) {
    getline(cin, s);
    for (int j = 0; j < 4; j++) {
      wall[i][j + 1][1] = (s[j] == '1');
      wall[i + 1][j + 1][3] = (s[j] == '1');
    }
    getline(cin, s);
    for (int j = 0; j < 5; j++) {
      wall[i + 1][j][0] = (s[j] == '1');
      wall[i + 1][j + 1][2] = (s[j] == '1');
    }
  }
  getline(cin, s);
  for (int j = 0; j < 4; j++) {
    wall[4][j + 1][1] = (s[j] == '1');
    wall[5][j + 1][3] = (s[j] == '1');
  }

  string icon = "RDLU";
#if 0
  REP(i,6) REP(j,6) {
    cerr << "(" << i << "," << j << ")";
    REP(k,4) {
      if(wall[i][j][k]) cerr << icon[k];
    }
    cerr << endl;
  }
#endif

  int X = 1, Y = 0, dir = 0;
  string ans = "R";
  do {
#if 1
    cerr << "X = " << X << ", Y = " << Y << ", dir = " << dir << endl;
#endif
    if (wall[Y][X][dir] == 0) {
      Y += dy[dir];
      X += dx[dir];
      if (wall[Y][X][(dir + 1) % 4] == 0) {
        dir = (dir + 1) % 4;
        X += dx[dir];
        Y += dy[dir];
        if (wall[Y][X][(dir + 1) % 4] == 0) {
          dir = (dir + 1) % 4;
          X += dx[dir];
          Y += dy[dir];
          ans.push_back(icon[dir]);
        } else {
          ans.push_back(icon[dir]);
        }
      } else {
        ans.push_back(icon[dir]);
      }
    } else {
      dir = (dir - 1 + 4) % 4;
      ans.push_back(icon[dir]);
    }
  } while (!((X == 1 && Y == 0)));
  ans = ans.substr(0, (int)ans.size() - 1);
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define DEBUG(x) cerr << #x << " = " << x << endl

int main() {
  static bool wall[6][6][4];
  const int dx[4] = {1, 0, -1, 0};
  const int dy[4] = {0, 1, 0, -1};
  REP(i, 6) REP(j, 6) REP(k, 4) {
    int ni = i + dy[k];
    int nj = j + dx[k];
    if (0 <= ni && ni < 6 && 0 <= nj && nj < 6) {
      wall[i][j][k] = 0;
    } else {
      wall[i][j][k] = 1;
    }
  }

  string s;
  for (int i = 0; i < 4; i++) {
    getline(cin, s);
    for (int j = 0; j < 4; j++) {
      wall[i][j + 1][1] = (s[j] == '1');
      wall[i + 1][j + 1][3] = (s[j] == '1');
    }
    getline(cin, s);
    for (int j = 0; j < 5; j++) {
      wall[i + 1][j][0] = (s[j] == '1');
      wall[i + 1][j + 1][2] = (s[j] == '1');
    }
  }
  getline(cin, s);
  for (int j = 0; j < 4; j++) {
    wall[4][j + 1][1] = (s[j] == '1');
    wall[5][j + 1][3] = (s[j] == '1');
  }

  string icon = "RDLU";
#if 0
  REP(i,6) REP(j,6) {
    cerr << "(" << i << "," << j << ")";
    REP(k,4) {
      if(wall[i][j][k]) cerr << icon[k];
    }
    cerr << endl;
  }
#endif

  int X = 1, Y = 0, dir = 0;
  string ans = "R";
  do {
#if 0
    cerr << "X = " << X << ", Y = " << Y << ", dir = " << dir << endl;
#endif
    if (wall[Y][X][dir] == 0) {
      Y += dy[dir];
      X += dx[dir];
      if (wall[Y][X][(dir + 1) % 4] == 0) {
        dir = (dir + 1) % 4;
        X += dx[dir];
        Y += dy[dir];
        if (wall[Y][X][(dir + 1) % 4] == 0) {
          dir = (dir + 1) % 4;
          X += dx[dir];
          Y += dy[dir];
          ans.push_back(icon[dir]);
        } else {
          ans.push_back(icon[dir]);
        }
      } else {
        ans.push_back(icon[dir]);
      }
    } else {
      dir = (dir - 1 + 4) % 4;
      ans.push_back(icon[dir]);
    }
  } while (!((X == 1 && Y == 0)));
  ans = ans.substr(0, (int)ans.size() - 1);
  cout << ans << endl;
}