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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
int dp[55][55][4][1 << 12];
char f[55][55];
int w, h, K;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
bool out(int x, int y) {
  if (x < 0 || x >= h || y < 0 || y >= w)
    return true;
  return false;
}
bool revisit(int bit) {
  int tx = 0, ty = 0;
  for (int i = 0; i < 6; i++, bit >>= 2) {
    int id = bit % 4;
    tx += dx[id];
    ty += dy[id];
    if (tx == 0 && ty == 0)
      return true;
  }
  return false;
}
int rec(int x, int y, int k, int bit) {
  if (x == 0 && y == 0)
    return 0;
  if (dp[x][y][k][bit] != -INF)
    return dp[x][y][k][bit];
  int num = 0;
  if (f[x][y] != '.' && f[x][y] != '#' && !revisit(bit))
    num = f[x][y] - '0';
  int res = -INF;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (!out(nx, ny) && f[x][y] != '#' && k - (i >= 2) >= 0) {
      res = max(res,
                rec(nx, ny, k - (i >= 2), ((1 << 12) - 1) & ((bit << 2) + i)) +
                    num);
    }
  }
  return dp[x][y][k][bit] = res;
}
int main() {
  cin >> h >> w >> K;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> f[i][j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int k = 0; k < 4; k++) {
        for (int l = 0; l < (1 << 12); l++) {
          dp[i][j][k][l] = -INF;
        }
      }
    }
  }
  cout << rec(h - 1, w - 1, K, 0) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
int dp[50][50][4][1 << 12];
char f[50][50];
int w, h, K;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
bool out(int x, int y) {
  if (x < 0 || x >= h || y < 0 || y >= w)
    return true;
  return false;
}
bool revisit(int bit) {
  int tx = 0, ty = 0;
  for (int i = 0; i < 6; i++, bit >>= 2) {
    int id = bit % 4;
    tx += dx[id];
    ty += dy[id];
    if (tx == 0 && ty == 0)
      return true;
  }
  return false;
}
int rec(int x, int y, int k, int bit) {
  if (x == 0 && y == 0)
    return 0;
  if (dp[x][y][k][bit] >= 0)
    return dp[x][y][k][bit];
  int num = 0;
  if (f[x][y] != '.' && f[x][y] != '#' && !revisit(bit))
    num = f[x][y] - '0';
  int res = -INF;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (!out(nx, ny) && f[x][y] != '#' && k - (i >= 2) >= 0) {
      res = max(res,
                rec(nx, ny, k - (i >= 2), ((1 << 12) - 1) & ((bit << 2) + i)) +
                    num);
    }
  }
  return dp[x][y][k][bit] = res;
}
int main() {
  cin >> h >> w >> K;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> f[i][j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int k = 0; k < 4; k++) {
        for (int l = 0; l < (1 << 12); l++) {
          dp[i][j][k][l] = -INF;
        }
      }
    }
  }
  cout << rec(h - 1, w - 1, K, 0) << endl;
  return 0;
}