1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<climits> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std;
inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; }
const int maxn=35,maxs=255,maxk=6,maxh=6,dx[]= {1,0,-1,0},dy[]= {0,-1,0,1};
int n,m,k,h,sx,sy,Pow[maxk]; char map[maxn][maxn]; double f[maxn][maxn][maxs][maxh],g[maxs][maxk],p[1<<5]; bool vst[maxn][maxn][maxs][maxh];
#define bit(s,x) (s/Pow[x-1]%3)
double Dp(int x,int y,int s,int h) { if(map[x][y]=='@')return 1; if(vst[x][y][s][h])return f[x][y][s][h]; vst[x][y][s][h]=1; if(h==0)return f[x][y][s][h]=0; for(int i=0; i<4; i++) { int nx=x+dx[i],ny=y+dy[i],ch=map[nx][ny]-'A'+1; if(nx<1||nx>n||ny<1||ny>m||map[nx][ny]=='#')continue; if(!isalpha(map[nx][ny])||(isalpha(map[nx][ny])&&bit(s,ch)==0))f[x][y][s][h]=max(f[x][y][s][h],Dp(nx,ny,s,h)); else if(bit(s,ch)==1)f[x][y][s][h]=max(f[x][y][s][h],Dp(nx,ny,s,h-1)); else if(bit(s,ch)==2) { int ns=s-Pow[ch-1]; double sum=Dp(nx,ny,ns,h-1)*g[s][ch]; ns-=Pow[ch-1],sum+=Dp(nx,ny,ns,h)*(1-g[s][ch]); f[x][y][s][h]=max(f[x][y][s][h],sum); } } return f[x][y][s][h]; }
bool iskey(char x) { return x=='.'||x=='#'||x=='@'||x=='$'||isalpha(x); }
int main() { n=Get_Int(); m=Get_Int(); k=Get_Int(); h=Get_Int(); Pow[0]=1; for(int i=1; i<=k; i++)Pow[i]=Pow[i-1]*3; for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) { while(!iskey(map[i][j]))map[i][j]=getchar(); if(map[i][j]=='$') { sx=i; sy=j; } } for(int i=0; i<(1<<k); i++)scanf("%lf",&p[i]); for(int S=0; S<Pow[k]; S++) { int sum=0; for(int i=0; i<(1<<k); i++) { bool flag=1; for(int j=1; j<=k; j++) { int x=bit(S,j); if(x==2)continue; if(x!=(i>>(j-1)&1)) { flag=0; break; } } if(!flag)continue; sum+=p[i]; for(int j=1; j<=k; j++) { if(bit(S,j)!=2||!(i>>(j-1)&1))continue; g[S][j]+=p[i]; } } for(int i=1; i<=k; i++)g[S][i]/=sum; } printf("%0.3lf\n",Dp(sx,sy,Pow[k]-1,h)); return 0; }
|