博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Educational Codeforces Round 88 (Rated for Div. 2)B. New Theatre Square-------题目+题解
阅读量:4034 次
发布时间:2019-05-24

本文共 3187 字,大约阅读时间需要 10 分钟。

B. New Theatre Square

来源:

You might have remembered Theatre square from the problem 1A. Now it’s finally getting repaved.

The square still has a rectangular shape of n×m meters. However, the picture is about to get more complicated now. Let ai,j be the j-th square in the i-th row of the pavement.

You are given the picture of the squares:

if ai,j= “*”, then the j-th square in the i-th row should be black;

if ai,j= “.”, then the j-th square in the i-th row should be white.
The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles:

1×1 tiles — each tile costs x burles and covers exactly 1 square;

1×2 tiles — each tile costs y burles and covers exactly 2 adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into 1×1 tiles.
You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles.

What is the smallest total price of the tiles needed to cover all the white squares?

Input

The first line contains a single integer t (1≤t≤500) — the number of testcases. Then the description of t testcases follow.

The first line of each testcase contains four integers n, m, x and y (1≤n≤100; 1≤m≤1000; 1≤x,y≤1000) — the size of the Theatre square, the price of the 1×1 tile and the price of the 1×2 tile.

Each of the next n lines contains m characters. The j-th character in the i-th line is ai,j. If ai,j= “*”, then the j-th square in the i-th row should be black, and if ai,j= “.”, then the j-th square in the i-th row should be white.

It’s guaranteed that the sum of n×m over all testcases doesn’t exceed 105.

Output

For each testcase print a single integer — the smallest total price of the tiles needed to cover all the white squares in burles.

Example

inputCopy
4
1 1 10 1
.
1 2 10 1
2 1 10 1
.
.
3 3 3 7
…*
.
.
outputCopy
10
1
20
18
Note
In the first testcase you are required to use a single 1×1 tile, even though 1×2 tile is cheaper. So the total price is 10 burles.

In the second testcase you can either use two 1×1 tiles and spend 20 burles or use a single 1×2 tile and spend 1 burle. The second option is cheaper, thus the answer is 1.

The third testcase shows that you can’t rotate 1×2 tiles. You still have to use two 1×1 tiles for the total price of 20.

In the fourth testcase the cheapest way is to use 1×1 tiles everywhere. The total cost is 6⋅3=18.

题意:
给你n * m的方格, * 代表黑色格子 . 代表白色格子,其中黑色格子已经确定,现有一些 1 * 1 和 1 * 2的白色格子,且不能旋转,花费分别为x和y,问最少花费多少钱

思路:

  • 若x<y,全都铺设1 * 1的花费最少
  • 若x>=y,则尽量铺设 1 * 2的,其次是1 * 1的

然后一行一行的遍历就好了

代码:

#include
#include
#include
#include
#include
#include
using namespace std;char a[101][1001];int main(){ int t; cin >> t; while (t--) { int n, m, x, y; cin >> n >> m >> x >> y; int s = 0, d = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> a[i][j]; } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { if (a[i][j] == '.') { if (j == m) { d++; } else { if (a[i][j + 1] == '.') { s++; j++; } else d++; } } } int ans = d * x; if (y >= x * 2) { ans += s * 2 * x; } else ans += s * y; cout << ans << endl; } return 0;}

转载地址:http://zofdi.baihongyu.com/

你可能感兴趣的文章
以太网基础知识
查看>>
慢慢欣赏linux 内核模块引用
查看>>
kprobe学习
查看>>
慢慢欣赏linux phy驱动初始化2
查看>>
慢慢欣赏linux CPU占用率学习
查看>>
2020年终总结
查看>>
linux内核学习(4)建立正式内核的页式内存映射, 以x86 32位模式为例
查看>>
Homebrew指令集
查看>>
React Native(一):搭建开发环境、出Hello World
查看>>
React Native(二):属性、状态
查看>>
JSX使用总结
查看>>
React Native(四):布局(使用Flexbox)
查看>>
React Native(七):Android双击Back键退出应用
查看>>
Android自定义apk名称、版本号自增
查看>>
adb command not found
查看>>
Xcode 启动页面禁用和显示
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>