New Theatre Square (Codeforces 1359B)
Tags: Greedy, two pointers

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 \times m$$$ meters. However, the picture is about to get more complicated now. Let $$$a_{i,j}$$$ be the $$$j$$$-th square in the $$$i$$$-th row of the pavement.You are given the picture of the squares:  if $$$a_{i,j} = $$$ "*", then the $$$j$$$-th square in the $$$i$$$-th row should be black;  if $$$a_{i,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 \times 1$$$ tiles — each tile costs $$$x$$$ burles and covers exactly $$$1$$$ square;  $$$1 \times 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 \times 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 \le t \le 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 \le n \le 100$$$; $$$1 \le m \le 1000$$$; $$$1 \le x, y \le 1000$$$) — the size of the Theatre square, the price of the $$$1 \times 1$$$ tile and the price of the $$$1 \times 2$$$ tile.Each of the next $$$n$$$ lines contains $$$m$$$ characters. The $$$j$$$-th character in the $$$i$$$-th line is $$$a_{i,j}$$$. If $$$a_{i,j} = $$$ "*", then the $$$j$$$-th square in the $$$i$$$-th row should be black, and if $$$a_{i,j} = $$$ ".", then the $$$j$$$-th square in the $$$i$$$-th row should be white.It's guaranteed that the sum of $$$n \times 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.

Note: In the first testcase you are required to use a single $$$1 \times 1$$$ tile, even though $$$1 \times 2$$$ tile is cheaper. So the total price is $$$10$$$ burles.In the second testcase you can either use two $$$1 \times 1$$$ tiles and spend $$$20$$$ burles or use a single $$$1 \times 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 \times 2$$$ tiles. You still have to use two $$$1 \times 1$$$ tiles for the total price of $$$20$$$.In the fourth testcase the cheapest way is to use $$$1 \times 1$$$ tiles everywhere. The total cost is $$$6 \cdot 3 = 18$$$.

Sample Inputs:
4
1 1 10 1
.
1 2 10 1
..
2 1 10 1
.
.
3 3 3 7
..*
*..
.*.

Sample Outputs:
10
1
20
18

Solution (Python):
import math, os, sys, random
from math import comb
from collections import defaultdict
from fractions import Fraction
from decimal import Decimal
if os.path.exists('input.txt'):
    sys.stdin = open('input.txt', 'r')
    sys.stdout = open('output.txt', 'w')
input = sys.stdin.readline

def check(s1, s2):
    cnt = 0
    for i in range(len(s1)):
        if s1[i] != s2[i]:
            cnt += 1
    if cnt <= 1:
        return True
    else:
        return False

def solve():
    (n, m, x, y) = map(int, input().split())
    arr = []
    for _ in range(n):
        s = input().split()
        s = ''.join(s)
        arr.append(s)
    (x_cnt, y_cnt) = (0, 0)
    if 2 * x <= y:
        y = 2 * x
    for i in range(n):
        cnt = 0
        for j in range(m):
            if arr[i][j] == '*':
                if cnt == 1:
                    cnt = 0
                    x_cnt += 1
            else:
                cnt += 1
            if cnt == 2:
                cnt = 0
                y_cnt += 1
        if cnt == 1:
            x_cnt += 1
    print(x_cnt * x + y_cnt * y)
for _ in range(int(input())):
    solve()
sys.stdout.close()
sys.stdin.close()

Solution (Python):
t = int(input())
for _ in range(t):
    total = 0
    (n, m, x, y) = map(int, input().split())
    for _ in range(n):
        a = input()
        if 2 * x < y:
            b = a.count('.')
            total += b * x
        else:
            d = 0
            while d < m:
                if a[d] == '.':
                    if d + 1 < m and a[d + 1] == '.':
                        total += y
                        d += 2
                    else:
                        total += x
                        d += 1
                else:
                    d += 1
    print(total)
7

Solution (Python):
for _ in range(int(input())):
    (n, m, x, y) = map(int, input().split())
    a = b = 0
    for _ in range(n):
        c = str(input())
        k = 0
        for i in c:
            if i == '.':
                k += 1
                a += 1
            else:
                k = 0
            if k == 2:
                b += 1
                k = 0
    print(min(a * x, b * y + (a - b * 2) * x))