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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
/*
const int dx[4] = {-1,0,0,0};
const int dy[4] = {0,-1,0,0};
const int dx2[4] = {-1,0,1,0};
const int dy2[4] = {0,1,0,-1};
*/
static const int dx[4] = {1, 0, -1, 0};
static const int dy[4] = {0, 1, 0, -1};
int main(void) {
  int d = 0;
  /*
  int a[5][4];
  int b[4][5];
  */
  int m[9][9];
  int x = 0, y = 0;
  string tmp;
  /*
  for(int i=0; i<4; i++){
    for(int j=0; j<4; j++){
      c = getchar();
      a[i][j] = c - '0';
    }
    getchar();
    for(int j=0; j<5; j++){
      c = getchar();
      b[i][j] = c - '0';
    }
    getchar();
  }
  for(int j=0; j<4; j++){
    c = getchar();
    a[4][j] = c - '0';
  }
  */
  for (int i = 0; i < 9; i++) {
    cin >> tmp;
    int jbegin;
    if (i % 2) {
      jbegin = 0;
    } else {
      jbegin = 1;
    }
    for (int j = jbegin, k = 0; j < 9; j += 2, k++) {
      m[i][j] = tmp[k];
    }
  }
  for (int time = 0; time > -1; time++) {
    for (int r = -1; r < 3; r++) {
      // printf("%d",(d+r+4)%4);
      if (y + dy[(d + r + 4) % 4] >= 0 && y + dy[(d + r + 4) % 4] <= 8 &&
          x + dx[(d + r + 4) % 4] >= 0 && x + dx[(d + r + 4) % 4] <= 8 &&
          m[y + dy[(d + r + 4) % 4]][x + dx[(d + r + 4) % 4]] == '1') {
        d = (d + r + 4) % 4;
        x += 2 * dx[d];
        y += 2 * dy[d];
        break;
      }
    }
    char out;
    switch (d) {
    case 0:
      out = 'R';
      break;
    case 1:
      out = 'D';
      break;
    case 2:
      out = 'L';
      break;
    case 3:
      out = 'U';
      break;
    }
    putchar(out);
    // printf("%d,%d\\n",x,y);
    if (x == 0 && y == 0)
      break;
  }
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
/*
const int dx[4] = {-1,0,0,0};
const int dy[4] = {0,-1,0,0};
const int dx2[4] = {-1,0,1,0};
const int dy2[4] = {0,1,0,-1};
*/
static const int dx[4] = {1, 0, -1, 0};
static const int dy[4] = {0, 1, 0, -1};
int main(void) {
  int d = 0;
  /*
  int a[5][4];
  int b[4][5];
  */
  int m[9][9];
  int x = 0, y = 0;
  string tmp;
  /*
  for(int i=0; i<4; i++){
    for(int j=0; j<4; j++){
      c = getchar();
      a[i][j] = c - '0';
    }
    getchar();
    for(int j=0; j<5; j++){
      c = getchar();
      b[i][j] = c - '0';
    }
    getchar();
  }
  for(int j=0; j<4; j++){
    c = getchar();
    a[4][j] = c - '0';
  }
  */
  for (int i = 0; i < 9; i++) {
    cin >> tmp;
    int jbegin;
    if (i % 2) {
      jbegin = 0;
    } else {
      jbegin = 1;
    }
    for (int j = jbegin, k = 0; j < 9; j += 2, k++) {
      m[i][j] = tmp[k];
    }
  }
  for (int time = 0; time > -1; time++) {
    for (int r = -1; r < 2; r++) {
      // printf("%d",(d+r+4)%4);
      if (y + dy[(d + r + 4) % 4] >= 0 && y + dy[(d + r + 4) % 4] <= 8 &&
          x + dx[(d + r + 4) % 4] >= 0 && x + dx[(d + r + 4) % 4] <= 8 &&
          m[y + dy[(d + r + 4) % 4]][x + dx[(d + r + 4) % 4]] == '1') {
        d = (d + r + 4) % 4;
        x += 2 * dx[d];
        y += 2 * dy[d];
        break;
      }
    }
    char out;
    switch (d) {
    case 0:
      out = 'R';
      break;
    case 1:
      out = 'D';
      break;
    case 2:
      out = 'L';
      break;
    case 3:
      out = 'U';
      break;
    }
    putchar(out);
    // printf("%d,%d\\n",x,y);
    if (x == 0 && y == 0)
      break;
  }
}