热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

RoundCAPACTest2017ProblemA.MonsterPath

ProblemA.MonsterPathCodejamonisamobilegameinwhichmonstertrainer

Problem A. Monster Path

Codejamon is a mobile game in which monster trainers walk around in the real world to catch monsters. You have an old smartphone with a short battery life, so you need to choose your path carefully to catch as many monsters as possible.

Suppose the Codejamon world is a rectangular grid with R rows and C columns. Rows are numbered from top to bottom, starting from 0; columns are numbered from left to right, starting from 0. You start in the cell in the Rsth row and the Csth column. You will take a total of S unit steps; each step must be to a cell sharing an edge (not just a corner) with your current cell.

Whenever you take a step into a cell in which you have not already caught a monster, you will catch the monster in that cell with probability P if the cell has a monster attractor, or Qotherwise. If you do catch the monster in a cell, it goes away, and you cannot catch any more monsters in that cell, even on future visits. If you do not catch the monster in a cell, you may still try to catch the monster on a future visit to that cell. The starting cell is special: you have no chance of catching a monster there before taking your first step.

If you plan your path optimally before making any move, what is the maximum possible expected number of monsters that you will be able to catch?

The battery can only support limited steps, so hurry up!

Input

The first line of the input gives the number of test cases, TT test cases follow.

Each test case starts with a line of five integers: RCRsCs and SR and C are the numbers of rows and columns in the grid; Rs and Cs
are the numbers of the row and column of your starting position, and S is the number of steps you are allowed to take.

The next line contains two decimals P and Q, where P is the probability of meeting a monster in cells with a monster attractor, and Q is the probability of meeting a monster in cells without a monster attractor. P and Q are each given to exactly four decimal places.

Each of the next R lines contains contains C space-separated characters; the j-th character of the i-th line represents the cell at row i and column j. Each element is either .(meaning there is no attractor in that cell) or A (meaning there is an attractor in that cell).

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the largest possible expected number of monsters that the player can catch in the given amount of steps.

y will be considered correct if y is within an absolute or relative error of 10-6 of the correct answer. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.

Limits

1 ≤ T ≤ 100.
0 ≤ Rs < R.
0 ≤ Cs < C.
0 ≤ Q < P ≤ 1.

Small dataset

1 ≤ R ≤ 10.
1 ≤ C ≤ 10.
0 ≤ S ≤ 5.

Large dataset

1 ≤ R ≤ 20.
1 ≤ C ≤ 20.
0 ≤ S ≤ 9.

Sample


Input 
 

Output 
 
2
4 4 0 0 5
0.8000 0.2000
. . . .
. . . .
. . A .
. A . A
10 10 9 1 4
0.6121 0.1000
. . A A . . . . . .
A . . . . . . . . .
. . A . . . . A . .
. . . A A . . . . .
. A A A . . . . . A
A . A A . . . . A .
. A . . . . . A . .
. . . . A A . . . .
. . A . . . A . . A
. . . . A . . A . .
Case #1: 1.6000000
Case #2: 1.0495336

In Case #1, one of the best paths is (0,0)->(0,1)->(0,2)->(1,2)->(2,2)->(2,3). On this path, the expected number of monsters that you will catch is 0.2 + 0.2 + 0.2 + 0.8 + 0.2 = 1.6. Remember that there is no chance of catching a monster before taking your first step, which is why there are five probabilities in the calculation, not six.

In Case #2, one of the best paths is (9,1)->(9,2)->(8,2)->(8,3)->(8,2). On this path, the expected number of monsters that you will catch is 0.1 + 0.6121 + 0.1 + 0.23743359 = 1.04953359. Since we accept results within an absolute or relative error of 10-6 of the correct answer (1.04953359), 1.0495336 is accepted.

Solution

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

typedef long long ll;

const int MAXN = 20;
double ori_grid[MAXN][MAXN], grid[MAXN][MAXN];

double get_res(int R, int C, double grid[MAXN][MAXN], int Rs, int Cs, int S, double res)
{
    if (Rs <0 || Cs <0 || Rs >= R || Cs >= C)
        return 0.0;

    double ori = grid[Rs][Cs];
    res += ori;
    S--;
    // printf("get_res %d %d %d %f\n", Rs, Cs, S, res);
    if (S == 0)
        return res;

    grid[Rs][Cs] = ori * (1 - ori_grid[Rs][Cs]);

    double res1 = get_res(R, C, grid, Rs - 1, Cs, S, res);
    double res2 = get_res(R, C, grid, Rs + 1, Cs, S, res);
    double res3 = get_res(R, C, grid, Rs, Cs - 1, S, res);
    double res4 = get_res(R, C, grid, Rs, Cs + 1, S, res);
    // printf("get_res %f %f %f %f\n", res1, res2, res3, res4); 

    grid[Rs][Cs] = ori;

    return max(max(res1, res2), max(res3, res4));
}

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        cout <<"Invalid input" <> T;
    int R = 0, C = 0, S = 0;
    int Rs = 0, Cs = 0;
    double P = 0, Q = 0;
    double res = 0;
    ofstream out(output.c_str());
    for (int i = 1; i <= T; i++)
    {
        in >> R >> C >> Rs >> Cs >> S >> P >> Q;
        memset(ori_grid, 0, MAXN * MAXN * sizeof(double));
        memset(grid, 0, MAXN * MAXN * sizeof(double));
        char ch = '\0';
        for (int r = 0; r > ch;
                if (ch == 'A')
                    ori_grid[r][c] = grid[r][c] = P;
                else
                    ori_grid[r][c] = grid[r][c] = Q;
                // cout < 0 && R * C > 1)
        {
            double res1 = get_res(R, C, grid, Rs - 1, Cs, S, 0.0);
            double res2 = get_res(R, C, grid, Rs + 1, Cs, S, 0.0);
            double res3 = get_res(R, C, grid, Rs, Cs - 1, S, 0.0);
            double res4 = get_res(R, C, grid, Rs, Cs + 1, S, 0.0);
            // printf("%f %f %f %f\n", res1, res2, res3, res4); 
            res = max(max(res1, res2), max(res3, res4));
        }
        else
            res = 0.0;
        out <<"Case #" < 
 


Note


注意在重复访问重复cell的时候,捕获概率和逃跑概率的计算。


Problem Reference


https://code.google.com/codejam/contest/6274486/dashboard


推荐阅读
  • 尽管我们尽最大努力,任何软件开发过程中都难免会出现缺陷。为了更有效地提升对支持部门的协助与支撑,本文探讨了多种策略和最佳实践,旨在通过改进沟通、增强培训和支持流程来减少这些缺陷的影响,并提高整体服务质量和客户满意度。 ... [详细]
  • Netty框架中运用Protobuf实现高效通信协议
    在Netty框架中,通过引入Protobuf来实现高效的通信协议。为了使用Protobuf,需要先准备好环境,包括下载并安装Protobuf的代码生成器`protoc`以及相应的源码包。具体资源可从官方下载页面获取,确保版本兼容性以充分发挥其性能优势。此外,配置好开发环境后,可以通过定义`.proto`文件来自动生成Java类,从而简化数据序列化和反序列化的操作,提高通信效率。 ... [详细]
  • 深入解析RelativeLayout、LinearLayout与FrameLayout的性能差异
    本文详细分析了FrameLayout和LinearLayout的性能对比,通过具体的测量数据和源码解析,探讨了不同布局在不同场景下的性能表现。 ... [详细]
  • 本文介绍了一种通过设置主题(Theme)来实现快速启动的Android引导页,并详细说明了如何避免因不同屏幕分辨率导致的图片拉伸问题。 ... [详细]
  • 关于进程的复习:#管道#数据的共享Managerdictlist#进程池#cpu个数1#retmap(func,iterable)#异步自带close和join#所有 ... [详细]
  • 本文介绍了如何使用 Gesture Detector 和 overridePendingTransition 方法来实现滑动界面和过渡动画。 ... [详细]
  • 编写SharePoint的EventReceiver需要用到ListTemplateID来进行绑定,下面的列表对于编程和排查错误都是个很好的索引.Listtem ... [详细]
  • 本文介绍了 Oracle SQL 中的集合运算、子查询、数据处理、表的创建与管理等内容。包括查询部门号为10和20的员工信息、使用集合运算、子查询的注意事项、数据插入与删除、表的创建与修改等。 ... [详细]
  • MyBatisCodeHelperPro 2.9.3 最新在线免费激活方法
    MyBatisCodeHelperPro 2.9.3 是一款强大的代码生成工具,适用于多种开发环境。本文将介绍如何在线免费激活该工具,帮助开发者提高工作效率。 ... [详细]
  • 在运行于MS SQL Server 2005的.NET 2.0 Web应用中,我偶尔会遇到令人头疼的SQL死锁问题。过去,我们主要通过调整查询来解决这些问题,但这既耗时又不可靠。我希望能找到一种确定性的查询模式,确保从设计上彻底避免SQL死锁。 ... [详细]
  • 【转】强大的矩阵奇异值分解(SVD)及其应用
    在工程实践中,经常要对大矩阵进行计算,除了使用分布式处理方法以外,就是通过理论方法,对矩阵降维。一下文章,我在 ... [详细]
  • 为了确保iOS应用能够安全地访问网站数据,本文介绍了如何在Nginx服务器上轻松配置CertBot以实现SSL证书的自动化管理。通过这一过程,可以确保应用始终使用HTTPS协议,从而提升数据传输的安全性和可靠性。文章详细阐述了配置步骤和常见问题的解决方法,帮助读者快速上手并成功部署SSL证书。 ... [详细]
  • POJ 2482 星空中的星星:利用线段树与扫描线算法解决
    在《POJ 2482 星空中的星星》问题中,通过运用线段树和扫描线算法,可以高效地解决星星在窗口内的计数问题。该方法不仅能够快速处理大规模数据,还能确保时间复杂度的最优性,适用于各种复杂的星空模拟场景。 ... [详细]
  • 整理于2020年10月下旬:总结过去,展望未来Itistoughtodayandtomorrowwillbetougher.butthedayaftertomorrowisbeau ... [详细]
  • 面试题总结_2019年全网最热门的123个Java并发面试题总结
    面试题总结_2019年全网最热门的123个Java并发面试题总结 ... [详细]
author-avatar
mobiledu2502853597
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有