B. New Theatre Square

Problem - B - Codeforces

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×mn×m meters. However, the picture is about to get more complicated now. Let ai,jai,j be the jj-th square in the ii-th row of the pavement.

You are given the picture of the squares:

  • if ai,j=ai,j= "*", then the jj-th square in the ii-th row should be black;
  • if ai,j=ai,j= ".", then the jj-th square in the ii-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×11×1 tiles — each tile costs xx burles and covers exactly 11 square;
  • 1×21×2 tiles — each tile costs yy burles and covers exactly 22 adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into 1×11×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 tt (1≤t≤5001≤t≤500) — the number of testcases. Then the description of tt testcases follow.

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

Each of the next nn lines contains mm characters. The jj-th character in the ii-th line is ai,jai,j. If ai,j=ai,j= "*", then the jj-th square in the ii-th row should be black, and if ai,j=ai,j= ".", then the jj-th square in the ii-th row should be white.

It's guaranteed that the sum of n×mn×m over all testcases doesn't exceed 105105.

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

input

Copy

4
1 1 10 1
.
1 2 10 1
..
2 1 10 1
.
.
3 3 3 7
..*
*..
.*.

output

Copy

10
1
20
18

Note

In the first testcase you are required to use a single 1×11×1 tile, even though 1×21×2 tile is cheaper. So the total price is 1010 burles.

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

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

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

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll; 
const int N=1010;
char s[N];
int m,n,x,k,t,y;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>x>>y;
        ll sum=0;
        while (n -- )
        {
            for(int j=0;j<m;j++)
          {
              cin>>s[j];//一层一层来
          } 
           
            for(int i=0;i<m;i++)
            {
                 
                if(s[i]=='.')
                {
                    int count=1;
                    int j=i;
                   for(int j=i;j<m;j++)//找到连续一段都是‘.’的,求个数。
                   {
                       if(s[j]=='.')
                       {
                           count++;
                           s[j]='*';
                       }
                       else break;
                   }
                     count-=1;
                     ll sum1=0,sum2=0;
                     if(count%2==0)sum1=count/2*y;//第一种情况,全铺成1*2的瓷板
                    else sum1=count/2*y+x;//还剩全铺成1*1的
                     sum2=count*x;//第二种情况,全铺成1*1的
                    sum+=min(sum1,sum2);//找最小的就行
                    i=-1;//从头开始重新扫荡这一层
                }
            }
            
        }
        cout<<sum<<endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值