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

#include <utility> // pair make_pair
using std::make_pair;
using std::pair;
using std::swap; // c++11
#include <tuple> // tuple(c++11) make_tuple(c++11)
using std::make_tuple;
using std::tuple; // get<address>(variable)
#include <string> // string stoi(c++11) stoll(c++11) to_string(c++11)
using std::stoi;
using std::stoll;
using std::string;
using std::to_string;
#include <queue> // queue priority_queue
using std::priority_queue;
using std::queue;
#include <stack> // stack
using std::stack;
#include <vector> // vector
using std::vector;
#include <map> // map
using std::map;
#include <set>          // set
using std::set;         // insert erase find count
#include <algorithm>    // min max
using std::lower_bound; // iterator,  >=
using std::max;
using std::max_element; // iterator
using std::min;
using std::min_element; // iterator
using std::sort;
using std::upper_bound; // iterator,  >
#include <complex>      // complex
using std::complex;     // real imag
#include <iostream>     // cin cout
using std::cin;
using std::cout;
using std::endl;
#include <iomanip> // setprecision fixed(manipulator)
using std::fixed;
using std::setprecision;
#include <cmath>
#include <cstdio>

#define lli long long int
#define INF (int)1e09
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define EFOR(i, m, n) for (int i = m; i <= n; i++)
#define RFOR(i, m, n) for (int i = m; i > n; i--)
#define REFOR(i, m, n) for (int i = m; i >= n; i--)
#define VSORT(v) sort((v).begin(), (v).end()) // sort vector
typedef pair<int, int> PI;
typedef tuple<int, int, int> TI;
typedef tuple<string, int, int> TS;
template <typename T> using Vec = vector<T>; // c++11

void bfs(Vec<Vec<char>> &table, string &max_ans, int i, int j) {
  queue<TS> que; // number h w
  string str = "";
  str += table[i][j];
  TS t = make_tuple(str, i, j);
  que.push(t);

  while (!que.empty()) {
    TS target = que.front();
    que.pop();
    string now_s = std::get<0>(target);
    int now_i = std::get<1>(target);
    int now_j = std::get<2>(target);

    if (table[now_i][now_j + 1] == '#' && table[now_i + 1][now_j] == '#') {
      int len_max = max_ans.length();
      int len_now = now_s.length();
      if (len_max < len_now) {
        max_ans = now_s;
      } else if ((int)max_ans.length() == (int)now_s.length()) {
        max_ans = max(max_ans, now_s);
      }
    } else {
      if (table[now_i + 1][now_j] != '#') {
        TS now = make_tuple(now_s + table[now_i + 1][now_j], now_i + 1, now_j);
        que.push(now);
      }
      if (table[now_i][now_j + 1] != '#') {
        TS now = make_tuple(now_s + table[now_i][now_j + 1], now_i, now_j + 1);
        que.push(now);
      }
    }
  }

  return;
}

int main(void) {
  int w;
  int h;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }
    Vec<Vec<char>> table(h + 2, Vec<char>(w + 2, '#'));
    char input;
    FOR(i, 1, h + 1) {
      FOR(j, 1, w + 1) {
        cin >> input;
        if (input >= '0' && input <= '9') {
          table[i][j] = input;
        }
      }
    }

    Vec<Vec<string>> ans(h + 2, Vec<string>(w + 2, ""));
    FOR(i, 1, h + 1) {
      FOR(j, 1, w + 1) {
        if (table[i][j] != '#' && table[i][j] >= '0' && table[i][j] <= '9') {
          if (table[i][j] == '0' && table[i - 1][j] == '#' &&
              table[i][j - 1] == '#') {
            table[i][j] = '#';
            continue;
          }
          string up = ans[i - 1][j];
          string left = ans[i][j - 1];
          int up_size = ans[i - 1][j].length();
          int left_size = ans[i][j - 1].length();

          if (up_size < left_size) {
            ans[i][j] = ans[i][j - 1] + table[i][j];
          } else if (up_size > left_size) {
            ans[i][j] = ans[i - 1][j] + table[i][j];
          } else {
            if (up_size == 0) {
              ans[i][j] = table[i][j];
            } else {
              ans[i][j] = max(up, left) + table[i][j];
            }
          }
        }
      }
    }

    string output = "";
    int len = 0;
    FOR(i, 1, h + 1) {
      FOR(j, 1, w + 1) {
        int now_size = ans[i][j].length();
        if (now_size > len) {
          len = now_size;
          output = ans[i][j];
        } else if (now_size == len) {
          output = max(output, ans[i][j]);
        }
      }
    }
    cout << output << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <utility> // pair make_pair
using std::make_pair;
using std::pair;
using std::swap; // c++11
#include <tuple> // tuple(c++11) make_tuple(c++11)
using std::make_tuple;
using std::tuple; // get<address>(variable)
#include <string> // string stoi(c++11) stoll(c++11) to_string(c++11)
using std::stoi;
using std::stoll;
using std::string;
using std::to_string;
#include <queue> // queue priority_queue
using std::priority_queue;
using std::queue;
#include <stack> // stack
using std::stack;
#include <vector> // vector
using std::vector;
#include <map> // map
using std::map;
#include <set>          // set
using std::set;         // insert erase find count
#include <algorithm>    // min max
using std::lower_bound; // iterator,  >=
using std::max;
using std::max_element; // iterator
using std::min;
using std::min_element; // iterator
using std::sort;
using std::upper_bound; // iterator,  >
#include <complex>      // complex
using std::complex;     // real imag
#include <iostream>     // cin cout
using std::cin;
using std::cout;
using std::endl;
#include <iomanip> // setprecision fixed(manipulator)
using std::fixed;
using std::setprecision;
#include <cmath>
#include <cstdio>

#define lli long long int
#define INF (int)1e09
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define EFOR(i, m, n) for (int i = m; i <= n; i++)
#define RFOR(i, m, n) for (int i = m; i > n; i--)
#define REFOR(i, m, n) for (int i = m; i >= n; i--)
#define VSORT(v) sort((v).begin(), (v).end()) // sort vector
typedef pair<int, int> PI;
typedef tuple<int, int, int> TI;
typedef tuple<string, int, int> TS;
template <typename T> using Vec = vector<T>; // c++11

void bfs(Vec<Vec<char>> &table, string &max_ans, int i, int j) {
  queue<TS> que; // number h w
  string str = "";
  str += table[i][j];
  TS t = make_tuple(str, i, j);
  que.push(t);

  while (!que.empty()) {
    TS target = que.front();
    que.pop();
    string now_s = std::get<0>(target);
    int now_i = std::get<1>(target);
    int now_j = std::get<2>(target);

    if (table[now_i][now_j + 1] == '#' && table[now_i + 1][now_j] == '#') {
      int len_max = max_ans.length();
      int len_now = now_s.length();
      if (len_max < len_now) {
        max_ans = now_s;
      } else if ((int)max_ans.length() == (int)now_s.length()) {
        max_ans = max(max_ans, now_s);
      }
    } else {
      if (table[now_i + 1][now_j] != '#') {
        TS now = make_tuple(now_s + table[now_i + 1][now_j], now_i + 1, now_j);
        que.push(now);
      }
      if (table[now_i][now_j + 1] != '#') {
        TS now = make_tuple(now_s + table[now_i][now_j + 1], now_i, now_j + 1);
        que.push(now);
      }
    }
  }

  return;
}

int main(void) {
  int w;
  int h;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }
    Vec<Vec<char>> table(h + 2, Vec<char>(w + 2, '#'));
    char input;
    FOR(i, 1, h + 1) {
      FOR(j, 1, w + 1) {
        cin >> input;
        if (input >= '0' && input <= '9') {
          table[i][j] = input;
        }
      }
    }
    string max_ans = "0";
    FOR(i, 1, h + 1) {
      FOR(j, 1, w + 1) {
        if (table[i][j] != '#' && table[i][j] != '0') {
          bfs(table, max_ans, i, j);
        }
      }
    }

    /*
       FOR (i, 0, h + 2) {
       FOR (j, 0, w + 2) {
       cout << table[i][j] << " ";
       }
       cout << endl;
       }
     */
    int max_ans_len = (int)max_ans.length();
    cout << max_ans << endl;
  }
  return 0;
}