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

#include <algorithm>
#include <ctype.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>

using namespace std;

char x[11][11];

void search(int i, int j, int direction);

int main() {

  for (int i = 0; i < 11; i++) {
    for (int j = 0; j < 11; j++) {
      x[i][j] = '0';
    }
  }

  while (cin >> x[1][2] >> x[1][4] >> x[1][6] >> x[1][8] >> x[2][1] >>
         x[2][3] >> x[2][5] >> x[2][7] >> x[2][9] >> x[3][2] >> x[3][4] >>
         x[3][6] >> x[3][8] >> x[4][1] >> x[4][3] >> x[4][5] >> x[4][7] >>
         x[4][9] >> x[5][2] >> x[5][4] >> x[5][6] >> x[5][8] >> x[6][1] >>
         x[6][3] >> x[6][5] >> x[6][7] >> x[6][9] >> x[7][2] >> x[7][4] >>
         x[7][6] >> x[7][8] >> x[8][1] >> x[8][3] >> x[8][5] >> x[8][7] >>
         x[8][9] >> x[9][2] >> x[9][4] >> x[9][6] >> x[9][8])

  {
    search(1, 1, 1);
    cout << endl;
  }

  return 0;
}

void search(int i, int j, int direction) {
  while (1) {
    char around[4];
    around[0] = x[i - 1][j];
    around[1] = x[i][j + 1];
    around[2] = x[i + 1][j];
    around[3] = x[i][j - 1];

    char output[4] = {'U', 'R', 'D', 'L'};
    int next[4] = {0, 1, 2, 3};

    int move[4][2] = {0};
    if (around[0] == '1') {
      move[0][0] = i - 2;
      move[0][1] = j;
    }
    if (around[1] == '1') {
      move[1][0] = i;
      move[1][1] = j + 2;
    }
    if (around[2] == '1') {
      move[2][0] = i + 2;
      move[2][1] = j;
    }
    if (around[3] == '1') {
      move[3][0] = i;
      move[3][1] = j - 2;
    }

    for (int k = 0; k < 4; k++) {
      int temp = (direction + 3 + k) % 4;
      if (around[temp] == '1') {
        direction = next[temp];
        i = move[temp][0];
        j = move[temp][1];
        cout << output[temp];
        break;
      }
    }
    if (i == 1 && j == 1) {
      break;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <ctype.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>

using namespace std;

char x[11][11];

void search(int i, int j, int direction);

int main() {

  for (int i = 0; i < 11; i++) {
    for (int j = 0; j < 11; j++) {
      x[i][j] = '0';
    }
  }

  while (cin >> x[1][2] >> x[1][4] >> x[1][6] >> x[1][8] >> x[2][1] >>
         x[2][3] >> x[2][5] >> x[2][7] >> x[2][9] >> x[3][2] >> x[3][4] >>
         x[3][6] >> x[3][8] >> x[4][1] >> x[4][3] >> x[4][5] >> x[4][7] >>
         x[4][9] >> x[5][2] >> x[5][4] >> x[5][6] >> x[5][8] >> x[6][1] >>
         x[6][3] >> x[6][5] >> x[6][7] >> x[6][9] >> x[7][2] >> x[7][4] >>
         x[7][6] >> x[7][8] >> x[8][1] >> x[8][3] >> x[8][5] >> x[8][7] >>
         x[8][9] >> x[9][2] >> x[9][4] >> x[9][6] >> x[9][8])

  {
    search(1, 1, 1);
  }

  return 0;
}

void search(int i, int j, int direction) {
  while (1) {
    char around[4];
    around[0] = x[i - 1][j];
    around[1] = x[i][j + 1];
    around[2] = x[i + 1][j];
    around[3] = x[i][j - 1];

    char output[4] = {'U', 'R', 'D', 'L'};
    int next[4] = {0, 1, 2, 3};

    int move[4][2] = {0};
    if (around[0] == '1') {
      move[0][0] = i - 2;
      move[0][1] = j;
    }
    if (around[1] == '1') {
      move[1][0] = i;
      move[1][1] = j + 2;
    }
    if (around[2] == '1') {
      move[2][0] = i + 2;
      move[2][1] = j;
    }
    if (around[3] == '1') {
      move[3][0] = i;
      move[3][1] = j - 2;
    }

    for (int k = 0; k < 3; k++) {
      int temp = (direction + 3 + k) % 4;
      if (around[temp] == '1') {
        direction = next[temp];
        i = move[temp][0];
        j = move[temp][1];
        cout << output[temp] << endl;
        break;
      }
    }
    if (i == 1 && j == 1) {
      break;
    }
  }
}