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

#include <iostream>

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

const int INF = 10000000;
using namespace std;
typedef long long ll;

/** Problem0037 : Path on a Grid **/
int main() {
  string s, ds = "LRUD";
  bool grid[5][5][4] = {0};
  int y = 0;
  rep(k, 9) {
    cin >> s;
    if (k % 2 == 0) {
      for (int i = 0; i < 4; i++) {
        if (s[i] == '1') {
          grid[i][y][1] = true;
          grid[i + 1][y][0] = true;
        }
      }
    } else {
      for (int i = 0; i < 5; i++) {
        if (s[i] == '1') {
          grid[i][y][3] = true;
          grid[i][y + 1][2] = true;
        }
      }
      y++;
    }
  }
  int d = 1, x = 0;
  y = 0;
  while (1) {
    switch (d) {
    case 1:
      if (grid[x][y][2]) {
        y--;
        d = 2;
        cout << "U";
      } else if (grid[x][y][1]) {
        x++;
        d = 1;
        cout << "R";
      } else if (grid[x][y][3]) {
        y++;
        d = 3;
        cout << "D";
      } else {
        x--;
        d = 0;
        cout << "L";
      }
      break;

    case 3:
      if (grid[x][y][1]) {
        x++;
        d = 1;
        cout << "R";
      } else if (grid[x][y][3]) {
        y++;
        d = 3;
        cout << "D";
      } else if (grid[x][y][0]) {
        x--;
        d = 0;
        cout << "L";
      } else {
        y--;
        d = 2;
        cout << "U";
      }
      break;

    case 0:
      if (grid[x][y][3]) {
        y++;
        d = 3;
        cout << "D";
      } else if (grid[x][y][0]) {
        x--;
        d = 0;
        cout << "L";
      } else if (grid[x][y][2]) {
        y--;
        d = 2;
        cout << "U";
      } else {
        x++;
        d = 1;
        cout << "R";
      }
      break;

    case 2:
      if (grid[x][y][0]) {
        x--;
        d = 0;
        cout << "L";
      } else if (grid[x][y][2]) {
        y--;
        d = 2;
        cout << "U";
      } else if (grid[x][y][1]) {
        x++;
        d = 1;
        cout << "R";
      } else {
        y++;
        d = 3;
        cout << "D";
      }
      break;
    }
    if (x == 0 && y == 0)
      break;
  }
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "math.h"
#include "stdio.h"

#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

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

const int INF = 10000000;
using namespace std;
typedef long long ll;

/** Problem0037 : Path on a Grid **/
int main() {
  bool wall[6][6][4] = {0};
  string s;
  int y = 1;
  rep(k, 9) {
    cin >> s;
    if (s.length() == 4) {
      rep(x, 4) {
        if (s[x] == '1') {
          wall[x + 1][y - 1][3] = true;
          wall[x + 1][y][2] = true;
        }
      }
    } else if (s.length() == 5) {
      REP(x, 1, 6) {
        if (s[x - 1] == '1') {
          wall[x - 1][y][1] = true;
          wall[x][y][0] = true;
        }
      }
      y++;
    }
  }

  int x = 1, d = 1;
  y = 0;
  cout << "R";
  while (1) {
    switch (d) {
    case 1:
      if (wall[x][y][1]) {
        if (wall[x][y][2]) {
          d = 0;
          cout << "U";
        } else {
          y--;
          d = 2;
          cout << "RU";
        }
      } else if (wall[x + 1][y][3]) {
        x++;
        d = 1;
        cout << "R";
      } else if (wall[x][y + 1][1]) {
        y++;
        x++;
        d = 3;
        cout << "D";
      } else {
        y++;
        d = 0;
      }
      break;

    case 3:
      if (wall[x][y][3]) {
        if (wall[x][y][1]) {
          d = 2;
          cout << "R";
        } else {
          x++;
          d = 1;
          cout << "DR";
        }
      } else if (wall[x][y + 1][0]) {
        y++;
        d = 3;
        cout << "D";
      } else if (wall[x - 1][y][3]) {
        x--;
        y++;
        d = 0;
        cout << "L";
      } else {
        x--;
        d = 2;
      }
      break;

    case 0:
      if (wall[x][y][0]) {
        if (wall[x][y][3]) {
          d = 1;
          cout << "D";
        } else {
          y++;
          d = 3;
          cout << "LD";
        }
      } else if (wall[x - 1][y][2]) {
        x--;
        d = 0;
        cout << "L";
      } else if (wall[x][y - 1][0]) {
        x--;
        y--;
        d = 2;
        cout << "U";
      } else {
        y--;
        d = 1;
      }
      break;

    case 2:
      if (wall[x][y][2]) {
        if (wall[x][y][0]) {
          d = 3;
          cout << "L";
        } else {
          x--;
          d = 0;
          cout << "UL";
        }
      } else if (wall[x][y - 1][1]) {
        y--;
        d = 2;
        cout << "U";
      } else if (wall[x + 1][y][2]) {
        x++;
        y--;
        d = 1;
        cout << "R";
      } else {
        x++;
        d = 3;
      }
      break;
    }
    cout << flush;
    if (d == 2 && x == 0 && y == 1)
      break;
    if (d == 0 && x == 1 && y == 1) {
      cout << "L";
      break;
    }
  }
  cout << endl;
  return 0;
}