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

#include <cstring>
#include <iostream>
#include <string>
#define MAX_W 70
#define MAX_H 70
using namespace std;

string c[MAX_H];
char dp[MAX_H][MAX_W][MAX_H * MAX_W];

bool front_greater(char a[], char b[]) {
  if (strlen(a) > strlen(b))
    return true;
  if (strlen(a) < strlen(b))
    return false;

  for (int i = 0; i < strlen(a); i++) {
    int anum = a[i] - '0';
    int bnum = b[i] - '0';
    if (anum > bnum)
      return true;
    if (anum < bnum)
      return false;
  }
  return false;
}

void copy_push(char a[], char b[], char c) {
  int len = strlen(b);
  for (int i = 0; i < len; i++) {
    a[i] = b[i];
  }
  a[len] = c;
  a[len + 1] = '\\0';
}

void search(int w, int h) {
  char secret[MAX_H * MAX_W];
  for (int i = 0; i < MAX_H * MAX_W; i++) {
    secret[i] = '\\0';
  }

  strcpy(secret, dp[0][0]);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (i == 0 && j == 0)
        continue;
      if (front_greater(dp[i][j], secret)) {
        strcpy(secret, dp[i][j]);
      }
    }
  }
  int len = strlen(secret);
  for (int i = 0; i < len; i++) {
    cout << secret[i];
  }
  cout << endl;
}

void set_number(char a[]) {
  while (a[0] == '0') {
    int len = strlen(a);
    for (int i = 1; i < len; i++) {
      a[i - 1] = a[i];
    }
    a[len - 1] = '\\0';
  }
}

void init() {
  for (int i = 0; i < MAX_H; i++) {
    c[i].erase();
  }
  for (int i = 0; i < MAX_H; i++) {
    for (int j = 0; j < MAX_W; j++) {
      for (int k = 0; k < MAX_H * MAX_W; k++) {
        dp[i][j][k] = '\\0';
      }
    }
  }
}

int main() {
  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0)
      return 0;

    init();

    for (int i = 0; i < h; i++) {
      cin >> c[i];
    }

    if ('1' <= c[0][0] && c[0][0] <= '9')
      dp[0][0][0] = c[0][0];
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if ('0' <= c[i][j] && c[i][j] <= '9') {
          if (i == 0) {
            copy_push(dp[i][j], dp[i][j - 1], c[i][j]);
            set_number(dp[i][j]);
          } else if (j == 0) {
            copy_push(dp[i][j], dp[i - 1][j], c[i][j]);
            set_number(dp[i][j]);
          } else {
            if (front_greater(dp[i - 1][j], dp[i][j - 1])) {
              copy_push(dp[i][j], dp[i - 1][j], c[i][j]);
            } else {
              copy_push(dp[i][j], dp[i][j - 1], c[i][j]);
            }
            set_number(dp[i][j]);
          }
        }
      }
    }
    search(w, h);
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#define MAX_H 70
#define MAX_W 70
using namespace std;

string secret_num;
string c[MAX_H];
string temp;
int count;
int status;

int p[2] = {1, 0}, q[2] = {0, 1};

bool front_greater(string a, string b) {
  for (int i = 0; i < a.size(); i++) {
    int anum = a[i] - '0';
    int bnum = b[i] - '0';
    if (anum > bnum)
      return true;
    if (anum < bnum)
      return false;
  }
  return false;
}

string copy(string s) {
  string temp;
  for (int i = 0; i < s.size(); i++) {
    temp.push_back(s[i]);
  }
  return temp;
}

string set_number(string s) {
  while (s[0] == '0') {
    for (int i = 1; i < s.size(); i++) {
      s[i - 1] = s[i];
    }
    s[s.size() - 1] = '\\0';
  }
  return s;
}

void judge(string s) {
  s = set_number(s);
  if (secret_num.size() < s.size()) {
    secret_num = copy(s);
  } else if (secret_num.size() == s.size()) {
    if (front_greater(s, secret_num))
      secret_num = copy(s);
  }
}

string erase_back(string s) {
  int len = s.size();
  string t;
  for (int i = 0; i < len - 1; i++) {
    t.push_back(s[i]);
  }
  return t;
}

void dfs(int w, int h, int x, int y) {
  temp.push_back(c[y][x]);
  for (int i = 0; i < 2; i++) {
    int x2 = x + p[i];
    int y2 = y + q[i];
    if (0 <= x2 && x2 < w && 0 <= y2 && y2 < h && '0' <= c[y2][x2] &&
        c[y2][x2] <= '9') {
      status = 0;
      dfs(w, h, x2, y2);
    } else if (status == 0) {
      judge(temp);
      status = 1;
    }
  }
  if (status == 1) {
    temp = erase_back(temp);
  }
}

void init() {
  for (int i = 0; i < MAX_H; i++) {
    c[i].erase(0);
  }
  count = 0;
  secret_num.erase(0);
}

int main() {
  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0)
      return 0;

    init();
    for (int i = 0; i < h; i++) {
      cin >> c[i];
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if ('0' <= c[i][j] && c[i][j] <= '9') {
          status = 0;
          dfs(w, h, j, i);
        }
      }
    }

    cout << secret_num << endl;
  }
}