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

#include <algorithm>
#include <complex>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, -1, 0, 1};

  bool yoko[4][5];
  bool tate[5][4];
  REP(i, 9) {
    char x;
    if (i % 2 == 0)
      REP(j, 4) {
        cin >> x;
        yoko[j][i / 2] = (x == '1');
      }
    else
      REP(j, 5) {
        cin >> x;
        tate[j][i / 2] = (x == '1');
      }
  }
  int x = 0, y = 0;
  int dir = 0;
  char ss[4] = {'R', 'U', 'L', 'D'};
  x++;
  while (true) {
    cout << ss[dir];
    if (x == 0 && y == 0)
      break;
    if (dir == 0) {
      if (y > 0 && tate[x][y - 1]) {
        dir = 1;
        y--;
      } else if (x < 4 && yoko[x][y]) {
        x++;
      } else if (y < 4 && tate[x][y]) {
        dir = 3;
        y++;
      } else {
        dir = 2;
        x--;
      }
    } else if (dir == 1) {
      if (x > 0 && yoko[x - 1][y]) {
        dir = 2;
        x--;
      } else if (y > 0 && tate[x][y - 1]) {
        y--;
      } else if (x < 4 && yoko[x][y]) {
        dir = 0;
        x++;
      } else {
        dir = 3;
        y++;
      }
    } else if (dir == 2) {
      if (y < 4 && tate[x][y]) {
        dir = 3;
        y++;
      } else if (x > 0 && yoko[x - 1][y]) {
        x--;
      } else if (y > 0 && tate[x][y - 1]) {
        dir = 1;
        y--;
      } else {
        dir = 0;
        x++;
      }
    } else if (dir == 3) {
      if (x < 4 && yoko[x][y]) {
        dir = 0;
        x++;
      } else if (y < 4 && tate[x][y]) {
        y++;
      } else if (x > 0 && yoko[x - 1][y]) {
        dir = 2;
        x--;
      } else {
        dir = 1;
        y--;
      }
    }
  }
  cout << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, -1, 0, 1};

  bool yoko[4][5];
  bool tate[5][4];
  REP(i, 9) {
    char x;
    if (i % 2 == 0)
      REP(j, 4) {
        cin >> x;
        yoko[j][i / 2] = (x == '1');
      }
    else
      REP(j, 5) {
        cin >> x;
        tate[j][i / 2] = (x == '1');
      }
  }
  int x = 0, y = 0;
  int dir = 0;
  char ss[4] = {'R', 'U', 'L', 'D'};
  x++;
  while (true) {
    cout << ss[dir];
    if (x == 0 && y == 0)
      break;
    if (dir == 0) {
      if (y > 0 && tate[x][y - 1]) {
        dir = 1;
        y--;
      } else if (x < 4 && yoko[x][y]) {
        x++;
      } else if (y < 4 && tate[x][y]) {
        dir = 3;
        y++;
      }
    } else if (dir == 1) {
      if (x > 0 && yoko[x - 1][y]) {
        dir = 2;
        x--;
      } else if (y > 0 && tate[x][y - 1]) {
        y--;
      } else if (x < 4 && yoko[x][y]) {
        dir = 0;
        x++;
      }
    } else if (dir == 2) {
      if (y < 4 && tate[x][y]) {
        dir = 3;
        y++;
      } else if (x > 0 && yoko[x - 1][y]) {
        x--;
      } else if (y > 0 && tate[x][y - 1]) {
        dir = 1;
        y--;
      }
    } else if (dir == 3) {
      if (x < 4 && yoko[x][y]) {
        dir = 0;
        x++;
      } else if (y < 4 && tate[x][y]) {
        y++;
      } else if (x > 0 && yoko[x - 1][y]) {
        dir = 2;
        x--;
      }
    }
  }
  cout << endl;
}