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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

#define MAX_N 60
#define MAX_K 3
#define MAX_D 6
#define MAX_C 4096

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int dp[MAX_N][MAX_N][MAX_K + 1][MAX_C];
int x[MAX_N][MAX_N];
int h, w, K, a;
char c;
char T[12] = ".123456789#";
int b[MAX_N][MAX_N];
int gx[6], gy[6];
int power[10] = {1, 4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144};

int hantei(int p, int q, vector<int> r) {
  int v = p, w = q;
  for (int o = 5; o >= 0; o--) {
    v -= dy[r[o]];
    w -= dx[r[o]];
    gy[o] = v;
    gx[o] = w;
  }
  for (int o = 0; o <= 5; o++) {
    if (p == gy[o] && q == gx[o]) {
      return 0;
    }
  }
  return 1;
}

int DP() {
  int X, Y, F, G, H, dir[6], maxn;

  // zentansaku(first 6)
  for (int i = 0; i < 4096; i++) {
    memset(b, 0, sizeof(b));
    X = 1;
    Y = 1;
    F = x[1][1];
    G = 0;
    b[Y][X] = 1;
    for (int j = 0; j < 6; j++) {
      a = (i / (int)pow(4, j)) % 4;
      X += dx[a];
      Y += dy[a];
      if (x[Y][X] == 10) {
        goto E;
      }
      if (a == 2 || a == 3) {
        G++;
      }
      if (b[Y][X] == 0) {
        F += x[Y][X];
      }
      b[Y][X] = 1;
    }
    if (G <= K) {
      dp[Y][X][G][i] = F;
    }
  E:;
  }

  // dp.
  for (int i = 0; i < 5; i++) {
    for (int j = 1; j <= h; j++) {
      for (int k = 1; k <= w; k++) {
        if (x[j][k] <= 9) {

          for (int l = 0; l <= K; l++) {
            for (int m = 0; m < MAX_C; m++) {
              if (dp[j][k][l][m] >= 0) {

                for (int n = 0; n < 6; n++) {
                  dir[n] = (m / power[n]) % 4;
                }

                if (l < K) {
                  if (x[j - 1][k] <= 9) {
                    a = 0;
                    for (int n = 1; n < 6; n++) {
                      a += power[n - 1] * dir[n];
                    }
                    a += 2048;
                    vector<int> A = {dir[1], dir[2], dir[3], dir[4], dir[5], 2};
                    if (hantei(j - 1, k, A) == 1) {
                      H = x[j - 1][k];
                    } else {
                      H = 0;
                    }
                    X = j;
                    Y = k;

                    dp[j - 1][k][l + 1][a] =
                        max(dp[j - 1][k][l + 1][a], dp[j][k][l][m] + H);
                  }

                  if (x[j][k - 1] <= 9) {
                    a = 0;
                    for (int n = 1; n < 6; n++) {
                      a += power[n - 1] * dir[n];
                    }
                    a += 3072;
                    vector<int> A = {dir[1], dir[2], dir[3], dir[4], dir[5], 3};

                    if (hantei(j, k - 1, A) == 1) {
                      H = x[j][k - 1];
                    } else {
                      H = 0;
                    }

                    dp[j][k - 1][l + 1][a] =
                        max(dp[j][k - 1][l + 1][a], dp[j][k][l][m] + H);
                  }
                }

                if (x[j + 1][k] <= 9) {
                  a = 0;
                  for (int n = 1; n < 6; n++) {
                    a += power[n - 1] * dir[n];
                  }

                  vector<int> A = {dir[1], dir[2], dir[3], dir[4], dir[5], 0};

                  if (hantei(j + 1, k, A) == 1) {
                    H = x[j + 1][k];
                  } else {
                    H = 0;
                  }

                  dp[j + 1][k][l][a] =
                      max(dp[j + 1][k][l][a], dp[j][k][l][m] + H);
                }

                if (x[j][k + 1] <= 9) {
                  a = 0;
                  for (int n = 1; n < 6; n++) {
                    a += power[n - 1] * dir[n];
                  }
                  a += 1024;

                  vector<int> A = {dir[1], dir[2], dir[3], dir[4], dir[5], 1};

                  if (hantei(j, k + 1, A) == 1) {
                    H = x[j][k + 1];
                  } else {
                    H = 0;
                  }

                  dp[j][k + 1][l][a] =
                      max(dp[j][k + 1][l][a], dp[j][k][l][m] + H);
                }
              }
            }
          }
        }
      }
    }
  }
  maxn = 0;
  for (int i = 0; i <= K; i++) {
    for (int j = 0; j < MAX_C; j++) {
      maxn = max(maxn, dp[h][w][i][j]);
    }
  }
  return maxn;
}

int main() {
  // shokika.
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      for (int k = 0; k <= MAX_K; k++) {
        for (int l = 0; l < MAX_C; l++) {
          dp[i][j][k][l] = -1;
        }
      }
    }
  }
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      x[i][j] = 10;
    }
  }
  // cin.
  cin >> h >> w >> K;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> c;
      for (int k = 0; k < 11; k++) {
        if (c == T[k]) {
          x[i][j] = k;
        }
      }
    }
  }
  cout << DP() << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

#define MAX_N 60
#define MAX_K 3
#define MAX_D 6
#define MAX_C 4096

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int dp[MAX_N][MAX_N][MAX_K + 1][MAX_C];
int x[MAX_N][MAX_N];
int h, w, K, a;
char c;
char T[12] = ".123456789#";
int b[MAX_N][MAX_N];
int gx[6], gy[6];

int hantei(int p, int q, vector<int> r) {
  int v = p, w = q;
  for (int o = 5; o >= 0; o--) {
    v -= dy[r[o]];
    w -= dx[r[o]];
    gy[o] = v;
    gx[o] = w;
  }
  for (int o = 0; o <= 5; o++) {
    if (p == gy[o] && q == gx[o]) {
      return 0;
    }
  }
  return 1;
}

int DP() {
  int X, Y, F, G, H, dir[6], maxn;

  // zentansaku(first 6)
  for (int i = 0; i < 4096; i++) {
    memset(b, 0, sizeof(b));
    X = 1;
    Y = 1;
    F = x[1][1];
    G = 0;
    b[Y][X] = 1;
    for (int j = 0; j < 6; j++) {
      a = (i / (int)pow(4, j)) % 4;
      X += dx[a];
      Y += dy[a];
      if (x[Y][X] == 10) {
        goto E;
      }
      if (a == 2 || a == 3) {
        G++;
      }
      if (b[Y][X] == 0) {
        F += x[Y][X];
      }
      b[Y][X] = 1;
    }
    if (G <= K) {
      dp[Y][X][G][i] = F;
    }
  E:;
  }

  // dp.
  for (int i = 0; i < K; i++) {
    for (int j = 1; j <= h; j++) {
      for (int k = 1; k <= w; k++) {
        if (x[j][k] <= 9) {

          for (int l = 0; l <= K; l++) {
            for (int m = 0; m < MAX_C; m++) {
              if (dp[j][k][l][m] >= 0) {

                for (int n = 0; n < 6; n++) {
                  dir[n] = (m / (int)pow(4, n)) % 4;
                }

                if (l < K) {
                  if (x[j - 1][k] <= 9) {
                    a = 0;
                    for (int n = 1; n < 6; n++) {
                      a += (int)pow(4, n - 1) * dir[n];
                    }
                    a += 2048;
                    vector<int> A = {dir[1], dir[2], dir[3], dir[4], dir[5], 2};
                    if (hantei(j - 1, k, A) == 1) {
                      H = x[j - 1][k];
                    } else {
                      H = 0;
                    }
                    X = j;
                    Y = k;

                    dp[j - 1][k][l + 1][a] =
                        max(dp[j - 1][k][l + 1][a], dp[j][k][l][m] + H);
                  }

                  if (x[j][k - 1] <= 9) {
                    a = 0;
                    for (int n = 1; n < 6; n++) {
                      a += (int)pow(4, n - 1) * dir[n];
                    }
                    a += 3072;
                    vector<int> A = {dir[1], dir[2], dir[3], dir[4], dir[5], 3};

                    if (hantei(j, k - 1, A) == 1) {
                      H = x[j][k - 1];
                    } else {
                      H = 0;
                    }

                    dp[j][k - 1][l + 1][a] =
                        max(dp[j][k - 1][l + 1][a], dp[j][k][l][m] + H);
                  }
                }

                if (x[j + 1][k] <= 9) {
                  a = 0;
                  for (int n = 1; n < 6; n++) {
                    a += (int)pow(4, n - 1) * dir[n];
                  }

                  vector<int> A = {dir[1], dir[2], dir[3], dir[4], dir[5], 0};

                  if (hantei(j + 1, k, A) == 1) {
                    H = x[j + 1][k];
                  } else {
                    H = 0;
                  }

                  dp[j + 1][k][l][a] =
                      max(dp[j + 1][k][l][a], dp[j][k][l][m] + H);
                }

                if (x[j][k + 1] <= 9) {
                  a = 0;
                  for (int n = 1; n < 6; n++) {
                    a += (int)pow(4, n - 1) * dir[n];
                  }
                  a += 1024;

                  vector<int> A = {dir[1], dir[2], dir[3], dir[4], dir[5], 1};

                  if (hantei(j, k + 1, A) == 1) {
                    H = x[j][k + 1];
                  } else {
                    H = 0;
                  }

                  dp[j][k + 1][l][a] =
                      max(dp[j][k + 1][l][a], dp[j][k][l][m] + H);
                }
              }
            }
          }
        }
      }
    }
  }
  maxn = 0;
  for (int i = 0; i <= K; i++) {
    for (int j = 0; j < MAX_C; j++) {
      maxn = max(maxn, dp[h][w][i][j]);
    }
  }
  return maxn;
}

int main() {
  // shokika.
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      for (int k = 0; k <= MAX_K; k++) {
        for (int l = 0; l < MAX_C; l++) {
          dp[i][j][k][l] = -1;
        }
      }
    }
  }
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      x[i][j] = 10;
    }
  }
  // cin.
  cin >> h >> w >> K;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> c;
      for (int k = 0; k < 11; k++) {
        if (c == T[k]) {
          x[i][j] = k;
        }
      }
    }
  }
  cout << DP() << endl;
  return 0;
}