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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define RIGHT 1
#define LEFT -1
#define UP 2
#define DOWN -2
using namespace std;

int main() {
  int i, j, state, sy = 0, sx = 1, t_state;
  char row[5][4], col[4][5];

  cin >> row[0];
  for (i = 0; i < 4; i++) {
    cin >> col[i];
    cin >> row[i + 1];
  }

  for (i = 0; i < 5; i++) {
    for (j = 0; j < 4; j++) {
      row[i][j] = row[i][j] - '0';
    }
  }
  for (i = 0; i < 5; i++) {
    for (j = 0; j < 4; j++) {
      col[j][i] = col[j][i] - '0';
    }
  }

  cout << "R" << flush;
  state = RIGHT;
  while (sy || sx) {
    switch (state) {
    case RIGHT:
      if (sy > 0 && col[sy - 1][sx]) {
        cout << "U" << flush;
        t_state = UP;
        sy--;
      } else if (sx < 4 && row[sy][sx]) {
        cout << "R" << flush;
        t_state = RIGHT;
        sx++;
      } else if (sy < 4 && col[sy][sx]) {
        cout << "D" << flush;
        t_state = DOWN;
        sy++;
      } else if (sx > 0 && row[sy][sx - 1]) {
        cout << "L" << flush;
        t_state = LEFT;
        sx--;
      }
      break;
    case LEFT:
      if (sy < 4 && col[sy][sx]) {
        cout << "D" << flush;
        t_state = DOWN;
        sy++;
      } else if (sx > 0 && row[sy][sx - 1]) {
        cout << "L" << flush;
        t_state = LEFT;
        sx--;
      } else if (sy > 0 && col[sy - 1][sx]) {
        cout << "U" << flush;
        t_state = UP;
        sy--;
      } else if (sx < 4 && row[sy][sx]) {
        cout << "R" << flush;
        t_state = RIGHT;
        sx++;
      }
      break;
    case UP:
      if (sx > 0 && row[sy][sx - 1]) {
        cout << "L" << flush;
        t_state = LEFT;
        sx--;
      } else if (sy > 0 && col[sy - 1][sx]) {
        cout << "U" << flush;
        t_state = UP;
        sy--;
      } else if (sx < 4 && row[sy][sx]) {
        cout << "R" << flush;
        t_state = RIGHT;
        sx++;
      } else if (sy < 4 && col[sy][sx]) {
        cout << "D" << flush;
        t_state = DOWN;
        sy++;
      }
      break;
    case DOWN:
      if (sx < 4 && row[sy][sx]) {
        cout << "R" << flush;
        t_state = RIGHT;
        sx++;
      } else if (sy < 4 && col[sy][sx]) {
        cout << "D" << flush;
        t_state = DOWN;
        sy++;
      } else if (sx > 0 && row[sy][sx - 1]) {
        cout << "L" << flush;
        t_state = LEFT;
        sx--;
      } else if (sy > 0 && col[sy - 1][sx]) {
        cout << "U" << flush;
        t_state = UP;
        sy--;
      }
      break;
    }
    state = t_state;
  }

  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define RIGHT 1
#define LEFT -1
#define UP 2
#define DOWN -2
using namespace std;

int main() {
  int i, j, state, sy = 0, sx = 1, t_state;
  char row[5][4], col[4][5];

  cin >> row[0];
  for (i = 0; i < 4; i++) {
    cin >> col[i];
    cin >> row[i + 1];
  }

  for (i = 0; i < 5; i++) {
    for (j = 0; j < 4; j++) {
      row[i][j] = row[i][j] - '0';
    }
  }
  for (i = 0; i < 5; i++) {
    for (j = 0; j < 4; j++) {
      col[j][i] = col[j][i] - '0';
    }
  }

  cout << "R" << flush;
  state = RIGHT;
  while (sy || sx) {
    switch (state) {
    case RIGHT:
      if (sy > 0 && col[sy - 1][sx]) {
        cout << "U" << flush;
        t_state = UP;
        sy--;
      } else if (sx < 4 && row[sy][sx]) {
        cout << "R" << flush;
        t_state = RIGHT;
        sx++;
      } else if (sy < 4 && col[sy][sx]) {
        cout << "D" << flush;
        t_state = DOWN;
        sy++;
      }
      break;
    case LEFT:
      if (sy < 4 && col[sy][sx]) {
        cout << "D" << flush;
        t_state = DOWN;
        sy++;
      } else if (sx > 0 && row[sy][sx - 1]) {
        cout << "L" << flush;
        t_state = LEFT;
        sx--;
      } else if (sy > 0 && col[sy - 1][sx]) {
        cout << "U" << flush;
        t_state = UP;
        sy--;
      }
      break;
    case UP:
      if (sx > 0 && row[sy][sx - 1]) {
        cout << "L" << flush;
        t_state = LEFT;
        sx--;
      } else if (sy > 0 && col[sy - 1][sx]) {
        cout << "U" << flush;
        t_state = UP;
        sy--;
      } else if (sx < 4 && row[sy][sx]) {
        cout << "R" << flush;
        t_state = RIGHT;
        sx++;
      }
      break;
    case DOWN:
      if (sx < 4 && row[sy][sx]) {
        cout << "R" << flush;
        t_state = RIGHT;
        sx++;
      } else if (sy < 4 && col[sy][sx]) {
        cout << "D" << flush;
        t_state = DOWN;
        sy++;
      } else if (sx > 0 && row[sy][sx - 1]) {
        cout << "L" << flush;
        t_state = LEFT;
        sx--;
      }
      break;
    }
    state = t_state;
  }

  cout << endl;
  return 0;
}