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

#include <iostream>
using namespace std;

int main() {
  int f, xyf;
  char h[5][4], v[4][5];
  int x, y;
  cin >> h[0];
  for (int i = 0; i < 4; i++) {
    cin >> v[i];
    cin >> h[i + 1];
  }
  f = xyf = 1;
  y = 0;
  x = 1;
  cout << "R";
  while (x != 0 || y != 0) {
    if (f == 1) {
      if (xyf == 1) {
        if (y > 0 && v[y - 1][x] == '1') {
          y--;
          cout << "U";
          f *= -1;
          xyf *= -1;
        } else if (x < 4 && h[y][x] == '1') {
          x++;
          cout << "R";
        } else if (v[y][x] == '0' || y == 4)
          xyf *= -1;
        else {
          y++;
          f *= -1;
          cout << "D";
        }
      } else {
        if (y < 4 && v[y][x] == '1') {
          y++;
          cout << "D";
          f *= -1;
          xyf *= -1;
        } else if (x > 0 && h[y][x - 1] == '1') {
          x--;
          cout << "L";
        } else if (v[y - 1][x] == '0' || y == 0)
          xyf *= -1;
        else {
          y--;
          cout << "U";
          f *= -1;
        }
      }
    } else {
      if (xyf == 1) {
        if (x < 4 && h[y][x] == '1') {
          x++;
          cout << "R";
          f *= -1;
        } else if (y < 4 && v[y][x] == '1') {
          y++;
          cout << "D";
        } else if (h[y][x - 1] == '0' || x == 0)
          xyf *= -1;
        else {
          x--;
          cout << "L";
          f *= -1;
          xyf *= -1;
        }
      } else {
        if (x > 0 && h[y][x - 1] == '1') {
          x--;
          cout << "L";
          f *= -1;
        } else if (y > 0 && v[y - 1][x] == '1') {
          y--;
          cout << "U";
        } else if (h[y][x] == '0' || x == 4)
          xyf *= -1;
        else {
          x++;
          cout << "R";
          f *= -1;
          xyf *= -1;
        }
      }
    }
  }
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int f, xyf;
  char h[5][4], v[4][5];
  int x, y;
  cin >> h[0];
  for (int i = 0; i < 4; i++) {
    cin >> v[i];
    cin >> h[i + 1];
  }
  f = xyf = 1;
  y = 0;
  x = 1;
  cout << "R";
  while (x != 0 || y != 0) {
    if (f == 1) {
      if (xyf == 1) {
        if (y > 0 && v[y - 1][x] == '1') {
          y--;
          cout << "U";
          f *= -1;
          xyf *= -1;
        } else if (x < 4 && h[y][x] == '1') {
          x++;
          cout << "R";
        } else if (v[y][x] == '0' || y == 4)
          xyf *= -1;
        else {
          y++;
          f *= -1;
          cout << "D";
        }
      } else {
        if (y > 0 && v[y - 1][x] == '1') {
          y--;
          cout << "U";
          f *= -1;
        } else if (x > 0 && h[y][x - 1] == '1') {
          x--;
          cout << "L";
        } else if (v[y][x] == '0' || y == 4)
          xyf *= -1;
        else {
          y++;
          cout << "D";
          f *= -1;
          xyf *= -1;
        }
      }
    } else {
      if (xyf == 1) {
        if (x < 4 && h[y][x] == '1') {
          x++;
          cout << "R";
          f *= -1;
        } else if (y < 4 && v[y][x] == '1') {
          y++;
          cout << "D";
        } else if (h[y][x - 1] == '0' || x == 0)
          xyf *= -1;
        else {
          x--;
          cout << "L";
          f *= -1;
          xyf *= -1;
        }
      } else {
        if (x > 0 && h[y][x - 1] == '1') {
          x--;
          cout << "L";
          f *= -1;
        } else if (y > 0 && v[y - 1][x] == '1') {
          y--;
          cout << "U";
        } else if (h[y][x] == '0' || x == 4)
          xyf *= -1;
        else {
          x++;
          cout << "R";
          f *= -1;
          xyf *= -1;
        }
      }
    }
  }
  cout.flush();
  cout << endl;
  return 0;
}