Edu CF Round 88 --B. New Theatre Square(贪心orDP)

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 10^5.

Output

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

input

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

output

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的方格,*表示需要黑砖,'.'表示需要白砖,黑砖已经铺好了,有两种规格的白砖,1x1和1x2,价格分别为x,y,问你铺完白砖所需的最小价格。

贪心解法:因为从规格得知,肯定就是行与行是独立的,我们可以发现如果2*x<=y的话,那么全部用1x1的白砖即可,否则,所以我们对于每一行每一白色连续区间贪心使用1x2的白砖即可。

#include 
using namespace std;
char a[105][1005];
void solve()
{
    int n,m,x,y;
    scanf("%d%d%d%d",&n,&m,&x,&y);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++) scanf(" %c",&a[i][j]);
    int ans=0;
    if(x*2<=y)//直接全部使用1x1白砖
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++) if(a[i][j]=='.') ans+=x;
    }else
    {
        for(int i=1;i<=n;i++)
        {
            int sum=0;//记录有多少连续白砖
            a[i][m+1]='?';//这样就不用特地考虑最后一列是'.'情况
            for(int j=1;j<=m+1;j++)
            {
                if(a[i][j]=='.') sum++;
                else ans+=(sum/2)*y+(sum%2)*x,sum=0;
            }
        }
    }
    printf("%d\n",ans);
}
int main()
{
    int t=1;
    scanf("%d",&t);
    while(t--) solve();
    return 0;
}

DP解法:f[i][j][0|1]表示前i-1行到第i行第j列且(i,j)使用第(1,2)种的最小价格,那么如果(i,j)是白色,f[i][j][0]就可以由min(f[i][j-1][0],f[i][j-1][1])+x得到,如果他的左边也是白色,那么可以考虑使用1x2的白砖,可以由f[i][j][1]=min(f[i][j-1][0]+y-x,f[i][j-1][1]+x)得到,那么最后min(f[n][m][0],f[n][m][1])就是答案。

#include 
using namespace std;
int f[105][1005][2];
//代表前i-1行到第i行第j列且(i,j)使用第(1,2)种的最小价格
char a[105][1005];
void solve()
{
    int n,m,x,y;
    scanf("%d%d%d%d",&n,&m,&x,&y);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            scanf(" %c",&a[i][j]);
            f[i][j][0]=f[i][j][1]=0x3f3f3f3f;//为避免f[n][m][0||1]可能取不到的情况
        }
    f[0][0][0]=f[0][0][1]=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(i!=1&&j==1)//每一行初始由上一行最后一列继承下来
            {
                f[i][0][0]=f[i-1][m][0];
                f[i][0][1]=f[i-1][m][1];
            }
            if(a[i][j]=='.')
            {
                f[i][j][0]=min(f[i][j-1][0],f[i][j-1][1])+x;
                if(a[i][j-1]=='.')//如果能形成连续白砖,可以选择第二种
                {
                    f[i][j][1]=min(f[i][j-1][0]+y-x,f[i][j-1][1]+x);
                    //要么前一个拼到后面,要么自己单独用1x1
                }
            }else//如果是黑色,拷贝转移即可
            {
                f[i][j][0]=f[i][j-1][0];
                f[i][j][1]=f[i][j-1][1];
            }
        }
    }
    printf("%d\n",min(f[n][m][0],f[n][m][1]));//两者取min即是答案
}
int main()
{
    int t=1;
    scanf("%d",&t);
    while(t--) solve();
    return 0;
}

你可能感兴趣的:(c++,c语言,算法)