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

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit
#include <cstring> // require memset
#include <ctime>
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
#define TOP 0
#define RIGHT 1
#define BOTTOM 2
#define LEFT 3
#define DEBUG 0
#define DEBUG2 0
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.Path_on_a_Grid";
string NAME;
string itos(int n) {
  stringstream ss;
  ss << n;

  return ss.str();
}
int grid[15][15];
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char Dir[] = {'U', 'R', 'D', 'L'};

string res;
void touch_wall(int row, int col, int dir, int hand) {
  if ((1 << hand & grid[row][col]) != 0)
    res += Dir[dir];
}

int main() {
//	cut here before submit
#if DEBUG
  NAME = FILE_NAME;
  int CNT = 1;
  NAME += itos(CNT);
  while (freopen(NAME.c_str(), "r", stdin) != NULL) {
#endif
    memset(grid, 0, sizeof(grid));
    string s = "";
    for (int i = 1; getline(cin, s); i++) {
      //		if (getline (cin, s ) )
      //		getline (cin, s );
      //		cin >> s;
      //		if (s.empty() ) break;
      rep(j, s.length()) {
        if (i % 2 != 0) {
          if (s[j] == '1') {
            grid[i / 2][j + 1] |= 1 << BOTTOM;
            grid[(i + 1) / 2][j + 1] |= 1 << TOP;
          } // end if
        } else {
          if (s[j] == '1') {
            grid[i / 2][j] |= 1 << RIGHT;
            grid[i / 2][j + 1] |= 1 << LEFT;
          } // end if
        }   // end if
      }     // end rep
    }       // end for

#if DEBUG2
    rep(i, 15) {
      rep(j, 15) { cout << setw(2) << grid[i][j]; } // end rep
      cout << endl;
    } // end rep
#endif
    int row = 0, col = 1, dir = RIGHT;
    int hand = (dir + 1) % 4;
    res = "";
    do {
      touch_wall(row, col, dir, hand);
      if (((1 << hand) & grid[row][col]) ==
          0) { // 右手の壁がなくなった、なくなった方向に回転
        dir = hand;
      } else {
        while (((1 << dir) & grid[row][col]) !=
               0) { // 右手の壁があったとしても前に進めない
          dir = (4 + dir - 1) % 4;
          touch_wall(row, col, dir, hand);
        } // end while
      }   // end if
      row = row + dr[dir];
      col = col + dc[dir];
      hand = (dir + 1) % 4;
#if DEBUG2
      cout << '(' << row << ',' << col << ')' << " dir: " << dir << endl;
#endif
    } while (!(row == 0 && col == 0)); // end while
    cout << res << endl;

#if DEBUG
    CNT++; // cut here before submit
    NAME = FILE_NAME;
    NAME += itos(CNT);
  } // end loop; cut here before submit
#endif

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit
#include <cstring> // require memset
#include <ctime>
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0
#define TOP 0
#define RIGHT 1
#define BOTTOM 2
#define LEFT 3
#define DEBUG2 0
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.Path_on_a_Grid";
string NAME;
string itos(int n) {
  stringstream ss;
  ss << n;

  return ss.str();
}
int grid[10][10];
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char Dir[] = {'U', 'R', 'D', 'L'};

string res;
void touch_wall(int row, int col, int dir, int hand) {
  if ((1 << hand & grid[row][col]) != 0)
    res += Dir[dir];
}

int main() {
//	cut here before submit
#if DEBUG
  NAME = FILE_NAME;
  int CNT = 1;
  NAME += itos(CNT);
  while (freopen(NAME.c_str(), "r", stdin) != NULL) {
#endif
    memset(grid, 0, sizeof(grid));
    /*
            rep (j, 5 ){
                    grid[0][j] |= 1<<TOP;
                    grid[5][j] |= 1<<BOTTOM;
            } // end rep
            rep (i, 6 ){
                    grid[i][0] |= 1<<LEFT;
                    grid[i][4] |= 1<<RIGHT;
            } // end rep
    */
    for (int i = 1; i <= 9; i++) {
      string s = "";
      cin >> s;
      rep(j, s.length()) {
        if (i % 2 != 0) {
          if (s[j] == '1') {
            grid[i / 2][j] |= 1 << BOTTOM;
            grid[(i + 1) / 2][j] |= 1 << TOP;
          } // end if
        } else {
          if (s[j] == '1') {
            if (j == 0) {
              grid[i / 2][j] |= 1 << LEFT;
            } else {
              grid[i / 2][j] |= 1 << LEFT;
              grid[i / 2][j - 1] |= 1 << RIGHT;
            } // end if
          }   // end if
        }     // end if
      }       // end rep
    }         // end for
              /*
                      rep (i, 6 ){
                              rep (j, 5 ){
                                      cout << setw(2) << grid[i][j];
                              } // end rep
                              cout << endl;
                      } // end rep
              */
    int row = 0, col = 0, dir = RIGHT;
    int hand = (dir + 1) % 4;
    res = "";
    while (!(row == 1 && col == 0 && (dir == LEFT))) {
      touch_wall(row, col, dir, hand);
      if ((1 << hand & grid[row][col]) ==
          0) { // 右手の壁がなくなった、なくなった方向に回転
        dir = hand;
      } else if ((1 << dir & grid[row][col]) != 0) {
        while ((1 << dir & grid[row][col]) !=
               0) { // 右手の壁があったとしても前に進めない
          dir = (4 + dir - 1) % 4;
          touch_wall(row, col, dir, hand);
        } // end while
      }   // end if
      row = row + dr[dir];
      col = col + dc[dir];
      hand = (dir + 1) % 4;
#if DEBUG2
      cout << '(' << row << ',' << col << ')' << " dir: " << dir << endl;
#endif
    } // end while
    touch_wall(row, col, dir, hand);
    cout << res << endl;

#if DEBUG
    CNT++; // cut here before submit
    NAME = FILE_NAME;
    NAME += itos(CNT);
  } // end loop; cut here before submit
#endif
  return 0;
}