Problem: problem

JOI, who came to Australia for a trip, enjoyed sightseeing in various places and finally returned to Japan. Now, JOI is in the town with the international airport where the return flight departs. The town is divided into north, south, east and west, and each section has roads, souvenir shops, houses, and an international airport. JOI starts from the northwestern section and heads for the international airport in the southeastern section.

JOI can move from the current parcel to an adjacent parcel, but cannot enter a parcel with a house. Also, move only to the east or south section of the current section in order to make it in time for the flight. However, there is some time to spare, so you can move to the north or west section of the section you are up to K times.

When JOI enters the area where the souvenir shop is located, he buys souvenirs for his Japanese friends. JOI did a lot of research on the souvenir shops, so he knows which souvenir shop to buy and how many souvenirs to buy. Create a program to find the maximum number of souvenirs you can buy.

However, the time to buy souvenirs can be ignored, and if you visit the same souvenir shop more than once, you will only buy souvenirs the first time you visit.





Example

Input

5 4 2
...#
.#.#
.#73
8##.
....


Output

11

Solution (in CPP):

Solution:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;

#define INF 1145141919

typedef pair<int, int> P;
int H, W, K;
int map[60][60];
int dp[50][50][4][2048];

bool f(int s, int t) {
  return s & (1 << (t-1));
}

int main() {
  cin >> H >> W >> K;
  for (int y=0; y<H; y++) {
    for (int x=0; x<W; x++) {
      char c;
      cin >> c;
      map[x][y] = c == '#' ? -1 : c == '.' ? 0 : (int)c-(int)'0';
    }
  }
  for (int x=0; x<W; x++) {
    for (int y=0; y<H; y++) {
      for (int k=0; k<=K; k++) {
        for (int s=0; s<2048; s++) {
          dp[x][y][k][s] = -INF;
        }
      }
    }
  }
  dp[0][0][0][0] = 0;
  queue< pair<P, P> > q;
  q.push(make_pair(P(0, 0), P(0, 0)));

  while (!q.empty()) {
    int x = q.front().first.first, y = q.front().first.second,
        k = q.front().second.first, s = q.front().second.second; q.pop();
    if (x < W-1 && map[x+1][y] != -1) {
      int ns = 0;
      if (k == 0) ns = (1<<1)*f(s,3) | (1<<3)*f(s,5) | (1<<4)*f(s,6) | (1<<6)*f(s,8) | (1<<7)*f(s,9) | (1<<8);
      if (k == 1) ns = (1<<0)*f(s,2) | (1<<2)*f(s,4) | (1<<3)*f(s,5) | (1<<5)*f(s,7) | (1<<6) | (1<<8)*f(s,10) | (1<<9)*f(s,11);
      if (k == 2) ns = (1<<0)*f(s,2) | (1<<1)*f(s,3) | (1<<3) | (1<<4)*f(s,6) | (1<<6)*f(s,8) | (1<<7)*f(s,9) | (1<<9)*f(s,11);
      if (k == 3) ns = (1<<0)*f(s,2) | (1<<1)*f(s,3) | (1<<3)*f(s,5) | (1<<4)*f(s,6) | (1<<6)*f(s,8);
      bool t = (k==1 && f(s,8)) || (k==2 && f(s,5)) || (k==3 && f(s,1));
      if (dp[x+1][y][k][ns] < dp[x][y][k][s] + (t?0:map[x+1][y])) {
        dp[x+1][y][k][ns] = dp[x][y][k][s] + (t?0:map[x+1][y]);
        q.push(make_pair(P(x+1, y), P(k, ns)));
      }
    }
    if (y < H-1 && map[x][y+1] != -1) {
      int ns = 0;
      if (k == 0) ns = (1<<0)*f(s,3) | (1<<1)*f(s,5) | (1<<2)*f(s,6) | (1<<3)*f(s,8) | (1<<4)*f(s,9) | (1<<5);
      if (k == 1) ns = (1<<0)*f(s,4) | (1<<1)*f(s,5) | (1<<2)*f(s,7) | (1<<3) | (1<<4)*f(s,8) | (1<<5)*f(s,9) | (1<<6)*f(s,10);
      if (k == 2) ns = (1<<0) | (1<<1)*f(s,5) | (1<<2)*f(s,6) | (1<<3)*f(s,7) | (1<<4)*f(s,9) | (1<<6)*f(s,10) | (1<<7)*f(s,11);
      if (k == 3) ns = (1<<0)*f(s,5) | (1<<1)*f(s,6) | (1<<3)*f(s,7) | (1<<4)*f(s,8) | (1<<6)*f(s,9);
      bool t = (k==1 && f(s,11)) || (k==2 && f(s,8)) || (k==3 && f(s,4));
      if (dp[x][y+1][k][ns] < dp[x][y][k][s] + (t?0:map[x][y+1])) {
        dp[x][y+1][k][ns] = dp[x][y][k][s] + (t?0:map[x][y+1]);
        q.push(make_pair(P(x, y+1), P(k, ns)));
      }
    }
    if (k < K && x > 0 && map[x-1][y] != -1) {
      int ns = 0;
      if (k == 0) ns = (1<<0)*f(s,2) | (1<<1)*f(s,3) | (1<<2)*f(s,4) | (1<<3)*f(s,5) | (1<<4)*f(s,6) | (1<<5)*f(s,7) | (1<<6)*f(s,8) | (1<<7);
      if (k == 1) ns = (1<<0)*f(s,3) | (1<<1)*f(s,4) | (1<<2)*f(s,5) | (1<<3)*f(s,6) | (1<<4) | (1<<5)*f(s,8) | (1<<6)*f(s,9) | (1<<7)*f(s,10) | (1<<8)*f(s,11);
      if (k == 2) ns = (1<<0) | (1<<1)*f(s,5) | (1<<2)*f(s,6) | (1<<3)*f(s,7) | (1<<4)*f(s,8) | (1<<5)*f(s,9) | (1<<6)*f(s,10) | (1<<7)*f(s,11);
      bool t = (k==0 && f(s,9)) || (k==1 && f(s,7)) || (k==2 && f(s,4));
      if (dp[x-1][y][k+1][ns] < dp[x][y][k][s] + (t?0:map[x-1][y])) {
        dp[x-1][y][k+1][ns] = dp[x][y][k][s] + (t?0:map[x-1][y]);
        q.push(make_pair(P(x-1, y), P(k+1, ns)));
      }
    }
    if (k < K && y > 0 && map[x][y-1] != -1) {
      int ns = 0;
      if (k == 0) ns = (1<<0)*f(s,1) | (1<<2)*f(s,2) | (1<<3)*f(s,3) | (1<<5)*f(s,4) | (1<<6)*f(s,5) | (1<<8)*f(s,8) | (1<<9)*f(s,9) | (1<<10);
      if (k == 1) ns = (1<<0)*f(s,1) | (1<<1)*f(s,2) | (1<<3)*f(s,3) | (1<<4)*f(s,5) | (1<<6)*f(s,7) | (1<<7) | (1<<8)*f(s,8) | (1<<9)*f(s,10) | (1<<10)*f(s,11);
      if (k == 2) ns = (1<<1)*f(s,2) | (1<<2)*f(s,3) | (1<<3) | (1<<4)*f(s,5) | (1<<5)*f(s,6) | (1<<6)*f(s,8) | (1<<7)*f(s,9) | (1<<8)*f(s,11);
      bool t = (k==0 && f(s,6)) || (k==1 && f(s,4)) || (k==2 && f(s,1));
      if (dp[x][y-1][k+1][ns] < dp[x][y][k][s] + (t?0:map[x][y-1])) {
        dp[x][y-1][k+1][ns] = dp[x][y][k][s] + (t?0:map[x][y-1]);
        q.push(make_pair(P(x, y-1), P(k+1, ns)));
      }
    }
  }
  int ans = 0;
  for (int k=0; k<=K; k++) {
    for (int s=0; s<(k==0||k==3?512:2048); s++) {
      ans = max(ans, dp[W-1][H-1][k][s]);
    }
  }
  cout << ans << "\n";
  return 0;
}