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 <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef double db;
ll vx[4]={1,0,-1,0};
ll vy[4]={0,1,0,-1};
struct T{
	ll y,x,k,s;
};
queue<T> q;
ll dp[50][50][4][1<<11];
string str[50];
ll s[50][50];
ll W,H,K;
ll z;
ll S;
ll add;
ll k;
int main()
{
	scanf("%lld%lld%lld",&H,&W,&K);
	memset(dp,-1,sizeof(dp));
	for(ll i=0;i<H;i++){
		cin>>str[i];
		for(ll j=0;j<W;j++){
			if(str[i][j]=='#') s[i][j]=-1;
			else if(str[i][j]=='.') s[i][j]=0;
			else s[i][j]=str[i][j]-'0';
		}
	}
	dp[0][0][3-K][0]=0; //?????¨??????->1
	q.push((T){0,0,3-K,0});
	while(!q.empty()){
		T t=q.front();
		q.pop();
		ll val=dp[t.y][t.x][t.k][t.s];
		k=t.k;
		if(t.k==0){
			if(t.x<W-1&&s[t.y][t.x+1]!=-1){
				add=s[t.y][t.x+1];
				S=t.s;
				S>>=1;
				z=1|(1<<2)|(1<<5);
				S&=~z;
				S|=(1<<8);
				if(dp[t.y][t.x+1][k][S]<val+add){
					dp[t.y][t.x+1][k][S]=val+add;
					q.push((T){t.y,t.x+1,k,S});
				}
			}
			if(t.y<H-1&&s[t.y+1][t.x]!=-1){
				add=s[t.y+1][t.x];
				S=t.s;
				S>>=4;
				z=(1<<3)|(1<<4);
				S&=z;
				S|=((t.s>>2)&1)	| ((t.s&(3<<4))>>3);
				S|=(1<<5);
				if(dp[t.y+1][t.x][k][S]<val+add){
					dp[t.y+1][t.x][k][S]=val+add;
					q.push((T){t.y+1,t.x,k,S});
				}
			}
			if(t.y>0&&s[t.y-1][t.x]!=-1){
				if((t.s>>5)&1) add=0;
				else add=s[t.y-1][t.x];
				S=0;
				z=(1<<5)|(1<<6);
				S|=((t.s<<2)&z);
				S|=(t.s&1);
				z=(1<<2)|(1<<3);
				S|=((t.s<<1)&z);
				z=(1<<8)|(1<<9);
				S|=((t.s<<1)&z);
				S|=(1<<10);
				if(dp[t.y-1][t.x][k+1][S]<val+add){
					dp[t.y-1][t.x][k+1][S]=val+add;
					q.push((T){t.y-1,t.x,k+1,S});
				}
			}
			if(t.x>0&&s[t.y][t.x-1]!=-1){
				S=t.s>>1;
				if((t.s>>8)&1) add=0;
				else add=s[t.y][t.x-1];
				z=(1<<8)|(1<<9)|(1<<10);
				S&=~z;
				S|=(1<<7);
				if(dp[t.y][t.x-1][k+1][S]<val+add){
					dp[t.y][t.x-1][k+1][S]=val+add;
					q.push((T){t.y,t.x-1,k+1,S});
				}
			}
		}
		else if(t.k==1){
			if(t.x<W-1&&s[t.y][t.x+1]!=-1){
				if((t.s>>7)&1) add=0;
				else add=s[t.y][t.x+1];
				S=t.s>>1;
				z=(1<<1)|(1<<4)|(1<<7)|(1<<10);
				S&=~z;
				S|=(1<<6);
				if(dp[t.y][t.x+1][k][S]<val+add){
					dp[t.y][t.x+1][k][S]=val+add;
					q.push((T){t.y,t.x+1,k,S});
				}
			}
			if(t.y<H-1&&s[t.y+1][t.x]!=-1){
				if((t.s>>10)&1) add=0;
				else add=s[t.y+1][t.x];
				S=0;
				z=3;
				S|=(t.s>>3)&z;
				z=(1<<2);
				S|=(t.s>>4)&z;
				z=(1<<4)|(1<<5)|(1<<6);
				S|=(t.s>>3)&z;
				S|=(1<<3);
				if(dp[t.y+1][t.x][k][S]<val+add){
					dp[t.y+1][t.x][k][S]=val+add;
					q.push((T){t.y+1,t.x,k,S});
				}
			}
			if(t.y>0&&s[t.y-1][t.x]!=-1){
				if((t.s>>3)&1) add=0;
				else add=s[t.y-1][t.x];
				S=t.s;
				z=1|(1<<1)|(1<<4)|(1<<6)|(1<<9)|(1<<10);
				S&=z;
				z=(1<<3);
				S|=(t.s<<1)&z;
				S|=(1<<7);
				z|=(1<<8);
				S|=(t.s<<1)&z;
				if(dp[t.y-1][t.x][k+1][S]<val+add){
					dp[t.y-1][t.x][k+1][S]=val+add;
					q.push((T){t.y-1,t.x,k+1,S});
				}
			}
			if(t.x>0&&s[t.y][t.x-1]!=-1){
				if((t.s>>6)&1) add=0;
				else add=s[t.y][t.x-1];
				S=(t.s>>2);
				S|=(1<<4);
				if(dp[t.y][t.x-1][k+1][S]<val+add){
					dp[t.y][t.x-1][k+1][S]=val+add;
					q.push((T){t.y,t.x-1,k+1,S});
				}
			}
		}
		else if(t.k==2){
			if(t.x<W-1&&s[t.y][t.x+1]!=-1){
				if((t.s>>4)&1) add=0;
				else add=s[t.y][t.x+1];
				S=t.s>>1;
				z=(1<<2)|(1<<5)|(1<<8)|(1<<10);
				S&=~z;
				S|=(1<<3);
				if(dp[t.y][t.x+1][k][S]<val+add){
					dp[t.y][t.x+1][k][S]=val+add;
					q.push((T){t.y,t.x+1,k,S});
				}
			}
			if(t.y<H-1&&s[t.y+1][t.x]!=-1){
				if((t.s>>7)&1) add=0;
				else add=s[t.y+1][t.x];
				S=0;
				z=(1<<1)|(1<<2)|(1<<3)|(1<<6)|(1<<7);
				S|=(t.s>>3)&z;
				z=(1<<4);
				S|=(t.s>>4)&z;
				S|=1;
				if(dp[t.y+1][t.x][k][S]<val+add){
					dp[t.y+1][t.x][k][S]=val+add;
					q.push((T){t.y+1,t.x,k,S});
				}
			}
			if(t.y>0&&s[t.y-1][t.x]!=-1){
				if(t.s&1) add=0;
				else add=s[t.y-1][t.x];
				S=0;
				z=1|(1<<1)|(1<<6)|(1<<7);
				S|=(t.s>>1)&z;
				z=(1<<4)|(1<<5);
				S|=(t.s&z);
				z=(1<<8);
				S|=((t.s>>2)&z);
				S|=(1<<3);
				if(dp[t.y-1][t.x][k+1][S]<val+add){
					dp[t.y-1][t.x][k+1][S]=val+add;
					q.push((T){t.y-1,t.x,k+1,S});
				}
			}
			if(t.x>0&&s[t.y][t.x-1]!=-1){
				if((t.s>>3)&1) add=0;
				else add=s[t.y][t.x-1];
				S=t.s>>3;
				S|=1;
				if(dp[t.y][t.x-1][k+1][S]<val+add){
					dp[t.y][t.x-1][k+1][S]=val+add;
					q.push((T){t.y,t.x-1,k+1,S});
				}
			}
		}
		else{
			if(t.x<W-1&&s[t.y][t.x+1]!=-1){
				if(t.s&1) add=0;
				else add=s[t.y][t.x+1];
				S=t.s>>1;
				z=(1<<2)|(1<<5)|(1<<7)|(1<<8);
				S&=~z;
				if(dp[t.y][t.x+1][k][S]<val+add){
					dp[t.y][t.x+1][k][S]=val+add;
					q.push((T){t.y,t.x+1,k,S});
				}
			}
			if(t.y<H-1&&s[t.y+1][t.x]!=-1){
				if((t.s>>3)&1) add=0;
				else add=s[t.y+1][t.x];
				S=0;
				z=3;
				S|=(t.s>>4)&z;
				z=(3<<3);
				S|=(t.s>>3)&z;
				S|=(t.s>>2)&(1<<6);
				if(dp[t.y+1][t.x][k][S]<val+add){
					dp[t.y+1][t.x][k][S]=val+add;
					q.push((T){t.y+1,t.x,k,S});
				}
			}
		}
	}
	ll ans=0;
	for(ll i=0;i<4;i++){
		for(ll j=0;j<(1<<11);j++){
			ans=max(ans,dp[H-1][W-1][i][j]);
		}
	}
	printf("%lld\n",ans);
}