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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef vector<string> VS;
const int size = 9;

int main() {
  VS lines(size);
  for (int i = 0; i < size; ++i) {
    cin >> lines[i];
  }

  bool O[size + 5][size + 5];
  for (int i = 0; i < size + 5; ++i) {
    for (int j = 0; j < size + 5; ++j) {
      O[i][j] = false;
    }
  }
  for (int i = 0; i < size; ++i) {
    for (int j = 0; j < (i % 2 == 0 ? 4 : 5); ++j) {
      O[i + 2][j + 2] = (lines[i][j] == '1');
    }
  }

  const string T = "RDLU";
  const int R = 0;
  const int D = 1;
  const int L = 2;
  const int U = 3;
  int r = 2;
  int c = 2;
  int s = R;

  while (true) {
    putchar(T[s]);
    if (r == 2 && c == 2 && s == L)
      break;
    if (r == 3 && c == 2 && s == U)
      break;

    if (s == L) {
      if (O[r + 1][c]) {
        ++r;
        s = D;
      } else if (O[r][c - 1]) {
        --c;
        s = L;
      } else if (O[r - 1][c]) {
        --r;
        s = U;
      } else if (O[r][c + 1]) {
        ++c;
        s = R;
      } else {
        s = R;
      }
    } else if (s == R) {
      if (O[r - 1][c + 1]) {
        --r;
        ++c;
        s = U;
      } else if (O[r][c + 1]) {
        ++c;
        s = R;
      } else if (O[r + 1][c + 1]) {
        ++r;
        ++c;
        s = D;
      } else if (O[r][c - 1]) {
        --c;
        s = L;
      } else {
        s = L;
      }
    } else if (s == U) {
      if (O[r - 1][c - 1]) {
        --r;
        --c;
        s = L;
      } else if (O[r - 2][c]) {
        r -= 2;
        s = U;
      } else if (O[r - 1][c]) {
        --r;
        s = R;
      } else if (O[r + 2][c]) {
        r += 2;
        s = D;
      } else {
        s = D;
      }
    } else if (s == D) {
      if (O[r + 1][c]) {
        ++r;
        s = R;
      } else if (O[r + 2][c]) {
        r += 2;
        s = D;
      } else if (O[r + 1][c - 1]) {
        ++r;
        --c;
        s = L;
      } else if (O[r - 2][c]) {
        r -= 2;
        s = U;
      } else {
        s = U;
      }
    }
  }
  putchar('\\n');

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef vector<string> VS;
const int size = 9;

int main() {
  VS lines(size);
  for (int i = 0; i < size; ++i) {
    cin >> lines[i];
  }

  bool O[size + 5][size + 5];
  for (int i = 0; i < size + 5; ++i) {
    for (int j = 0; j < size + 5; ++j) {
      O[i][j] = false;
    }
  }
  for (int i = 0; i < size; ++i) {
    for (int j = 0; j < (i % 2 == 0 ? 4 : 5); ++j) {
      O[i + 2][j + 2] = (lines[i][j] == '1');
    }
  }

  const string T = "RDLU";
  const int R = 0;
  const int D = 1;
  const int L = 2;
  const int U = 3;
  int r = 2;
  int c = 2;
  int s = R;

  while (true) {
    putchar(T[s]);
    if (r == 2 && c == 2 && s == L)
      break;
    if (r == 3 && c == 2 && s == U)
      break;

    if (s == L) {
      if (O[r + 1][c]) {
        ++r;
        s = D;
      } else if (O[r][c - 1]) {
        --c;
        s = L;
      } else if (O[r - 1][c]) {
        --r;
        s = U;
      } else if (O[r][c + 1]) {
        ++c;
        s = R;
      }
    } else if (s == R) {
      if (O[r - 1][c + 1]) {
        --r;
        ++c;
        s = U;
      } else if (O[r][c + 1]) {
        ++c;
        s = R;
      } else if (O[r + 1][c + 1]) {
        ++r;
        ++c;
        s = D;
      } else if (O[r][c - 1]) {
        --c;
        s = L;
      }
    } else if (s == U) {
      if (O[r - 1][c - 1]) {
        --r;
        --c;
        s = L;
      } else if (O[r - 2][c]) {
        r -= 2;
        s = U;
      } else if (O[r - 1][c]) {
        --r;
        s = R;
      } else if (O[r + 2][c]) {
        r += 2;
        s = D;
      }
    } else if (s == D) {
      if (O[r + 1][c]) {
        ++r;
        s = R;
      } else if (O[r + 2][c]) {
        r += 2;
        s = D;
      } else if (O[r + 1][c - 1]) {
        ++r;
        --c;
        s = L;
      } else if (O[r - 2][c]) {
        r -= 2;
        s = U;
      }
    }
  }
  putchar('\\n');

  return 0;
}