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

#include <array>
#include <bitset>
#include <cstdint>
#include <iostream>
#include <string>

int main() {
  enum { Left, Up, Right, Down };

  typedef std::array<std::bitset<4>, 5> Row;
  typedef std::array<Row, 5> Matrix;
  Matrix matrix;
  for (auto &row : matrix) {
    row.fill(std::bitset<4>());
  }

  std::string input;
  std::getline(std::cin, input);
  for (std::uint16_t x = 0; x < 4U; ++x) {
    if (input[x] == '1') {
      matrix[0][x].set(Right);
      matrix[0][x + 1].set(Left);
    }
  }

  for (std::uint16_t y = 0; y < 4U; ++y) {
    std::getline(std::cin, input);
    for (std::uint16_t x = 0; x < 5U; ++x) {
      if (input[x] == '1') {
        matrix[y][x].set(Down);
        matrix[y + 1][x].set(Up);
      }
    }
    std::getline(std::cin, input);
    for (std::uint16_t x = 0; x < 4U; ++x) {
      if (input[x] == '1') {
        matrix[y + 1][x].set(Right);
        matrix[y + 1][x + 1].set(Left);
      }
    }
  }

  std::uint16_t y = 0, x = 0;
  char direct = 'R';
  do {
    if (direct == 'R') {
      if (matrix[y][x][Up]) {
        direct = 'U';
        --y;
      } else if (matrix[y][x][Right]) {
        direct = 'R';
        ++x;
      } else if (matrix[y][x][Down]) {
        direct = 'D';
        ++y;
      } else {
        direct = 'L';
        --x;
      }
    } else if (direct == 'D') {
      if (matrix[y][x][Right]) {
        direct = 'R';
        ++x;
      } else if (matrix[y][x][Down]) {
        direct = 'D';
        ++y;
      } else if (matrix[y][x][Left]) {
        direct = 'L';
        --x;
      } else {
        direct = 'U';
        --y;
      }
    } else if (direct == 'L') {
      if (matrix[y][x][Down]) {
        direct = 'D';
        ++y;
      } else if (matrix[y][x][Left]) {
        direct = 'L';
        --x;
      } else if (matrix[y][x][Up]) {
        direct = 'U';
        --y;
      } else {
        direct = 'R';
        ++x;
      }
    } else if (direct == 'U') {
      if (matrix[y][x][Left]) {
        direct = 'L';
        --x;
      } else if (matrix[y][x][Up]) {
        direct = 'U';
        --y;
      } else if (matrix[y][x][Right]) {
        direct = 'R';
        ++x;
      } else {
        direct = 'D';
        ++y;
      }
    }
    std::cout << direct << std::flush;
  } while (not(y == 0 and x == 0));
  std::cout << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <array>
#include <bitset>
#include <cstdint>
#include <iostream>
#include <string>

int main() {
  enum { Left, Up, Right, Down };

  typedef std::array<std::bitset<4>, 5> Row;
  typedef std::array<Row, 5> Matrix;
  Matrix matrix;
  for (auto &row : matrix) {
    row.fill(std::bitset<4>());
  }

  std::string input;
  std::getline(std::cin, input);
  for (std::uint16_t x = 0; x < 4U; ++x) {
    if (input[x] == '1') {
      matrix[0][x].set(Right);
      matrix[0][x + 1].set(Left);
    }
  }

  for (std::uint16_t y = 0; y < 4U; ++y) {
    std::getline(std::cin, input);
    for (std::uint16_t x = 0; x < 5U; ++x) {
      if (input[x] == '1') {
        matrix[y][x].set(Down);
        matrix[y + 1][x].set(Up);
      }
    }
    std::getline(std::cin, input);
    for (std::uint16_t x = 0; x < 4U; ++x) {
      if (input[x] == '1') {
        matrix[y + 1][x].set(Right);
        matrix[y + 1][x + 1].set(Left);
      }
    }
  }

  std::uint16_t y = 0, x = 0;
  char direct = 'R';
  do {
    if (direct == 'R') {
      if (matrix[y][x][Up]) {
        direct = 'U';
        --y;
      } else if (matrix[y][x][Right]) {
        direct = 'R';
        ++x;
      } else if (matrix[y][x][Down]) {
        direct = 'D';
        ++y;
      }
    } else if (direct == 'D') {
      if (matrix[y][x][Right]) {
        direct = 'R';
        ++x;
      } else if (matrix[y][x][Down]) {
        direct = 'D';
        ++y;
      } else if (matrix[y][x][Left]) {
        direct = 'L';
        --x;
      }
    } else if (direct == 'L') {
      if (matrix[y][x][Down]) {
        direct = 'D';
        ++y;
      } else if (matrix[y][x][Left]) {
        direct = 'L';
        --x;
      } else if (matrix[y][x][Up]) {
        direct = 'U';
        --y;
      }
    } else if (direct == 'U') {
      if (matrix[y][x][Left]) {
        direct = 'L';
        --x;
      } else if (matrix[y][x][Up]) {
        direct = 'U';
        --y;
      } else if (matrix[y][x][Right]) {
        direct = 'R';
        ++x;
      }
    }
    std::cout << direct << std::flush;
  } while (not(y == 0 and x == 0));
  std::cout << std::endl;

  return 0;
}