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 <cstring>

using namespace std;

int h,w,k,mask;
int dp[50][50][1<<12][4];
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
char mp[51][51];
bool visit[1<<12];

int dfs(int x,int y,int bit,int lk){
    int pt=0,mx=-25000;
    if(dp[x][y][bit][lk]!=-1) return dp[x][y][bit][lk];
    if(x==w-1&&y==h-1&&lk==0) return 0;
    if(!visit[bit]&&mp[y][x]!='.') pt +=mp[y][x]-'0';
    for(int i=0;i<4;++i){
        if(lk==0&&i>1) break;
        int tx=x+dx[i],ty=y+dy[i],nbit=((bit<<2)|i)&mask,uk=i>1?lk-1:lk;
        if(tx>=0&&ty>=0&&tx<w&&ty<h&&mp[ty][tx]!='#') mx=max(mx,dfs(tx,ty,nbit,uk)+pt);
    }
    dp[x][y][bit][lk]=mx;
    return mx;
}

int main(){
    cin>>h>>w>>k;
    for(int i=0;i<h;++i){
        for(int j=0;j<w;++j) cin>>mp[i][j];
    }
    memset(dp,-1,sizeof(dp));
    mask=(1<<2*2*k)-1;
    for(int i=0;i<=mask;++i){
        int tx=0,ty=0,pre=i;
        for(int j=0;j<2*k;++j){
            tx -=dx[pre&3];ty -=dy[pre&3];
            if(tx==0&&ty==0) visit[i]=true;
            pre >>=2;
        }
    }
    cout<<dfs(0,0,0,k)<<endl;
    return 0;
}