Problem A. Monster Path Google APAC 2017 University Test Round C

这一题的数据量直接DFS暴力就可以了。对于每一个点,要计算其对expected number of monsters的增加量。

由于一个点可以被多次访问,如果这个点是首次被访问,那么对应的期望值就是P or Q。如果非首次访问,那么说明前几次都没有捕捉到monster,对应的期望值就是这几个事件相乘:(1-P or Q)^n-1 * P or Q,n是当前访问的次数。

因为一条path上各个点的monster数目是independent event,总的期望值直接相加即可。

#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<string>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<vector>
#include<queue>
#include<map>

using namespace std;

//2017 RoundC Problem A. Monster Path

const int maxn=25;
int T;
int R;
int C;
int Rs;
int Cs;
double P;
double Q;
int S;
int mp[maxn][maxn];
int viscounter[maxn][maxn];
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};
double ans;
double calmonsters(int x,int y)
{
    double attr=0;
    if(mp[x][y]==1)
    {
        attr=P;
    }
    else
    {
        attr=Q;
    }
    //cout<<x<<" "<<y<<" "<<attr<<endl;
    if(viscounter[x][y]==1)
    {
        //cout<<x<<" "<<y<<" "<<viscounter[x][y]<<" "<<attr<<endl;
        return attr;
    }
    else
    {
        double tmp=attr;
        for(int i=1;i<viscounter[x][y];i++)
        {
            tmp*=(1-attr);
        }
        //cout<<x<<" "<<y<<" "<<viscounter[x][y]<<" "<<attr<<endl;
        return tmp;
    }
}
void dfs(int x,int y,double curans,int step)
{
    //cout<<x<<" "<<y<<endl;
    if(step>0)
    {
        curans+=calmonsters(x,y);
    }
    if(step==S)
    {
        //cout<<ans<<" ans "<<curans<<endl;
        ans=max(ans,curans);
        //cout<<"haha"<<endl;
        return;
    }
    step++;
    for(int d=0;d<4;d++)
    {
        int nextx=x+dx[d];
        int nexty=y+dy[d];
        //cout<<nextx<<" "<<nexty<<endl;
        //if(viscounter[nextx][nexty]!=0) continue;
        if(nextx<0||nextx>=R||nexty<0||nexty>=C)
        {
            //cout<<nextx<<" "<<nexty<<endl;
            continue;
        }
        viscounter[nextx][nexty]++;
        dfs(nextx,nexty,curans,step);
        viscounter[nextx][nexty]--;
    }
}
int main()
{
    freopen("A-large-practice.in","r",stdin);
    freopen("output.txt","w",stdout);
    scanf("%d", &T);
    for(int ca=1;ca<=T;ca++)
    {
        memset(mp,0,sizeof(mp));
        memset(viscounter,0,sizeof(viscounter));
        ans=0;
        scanf("%d %d %d %d %d",&R,&C,&Rs,&Cs,&S);
        //cout<<"hh"<<endl;
        scanf("%lf %lf",&P,&Q);
        //cout<<P<<" "<<Q<<endl;
        for(int i=0;i<R;i++)
        {
            for(int j=0;j<C;j++)
            {
                char tmp;
                //scanf("%c",&tmp);
                cin>>tmp;
                //cout<<tmp<<endl;
                if(tmp=='A')
                {
                    mp[i][j]=1;
                    //cout<<"hh"<<endl;
                }
            }
        }
        dfs(Rs,Cs,0,0);
        //cout<<ans<<endl;
        printf("Case #%d: %.6f\n",ca,ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值