題目
題意:
對於每一個白塊,你可以用的方式取翻轉,然後分別要花費,現在問你將全部反轉成黑塊需要的最小費用是多少。
思路:
對於遍歷到的每一個白塊,我們首先看看左邊是否存在一個白塊,如果存在,那麼就看哪一種翻轉費用少,如果只有一個白塊,那麼只有這種翻轉。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <cctype>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return ;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1:1;
ret = (c == '-') ? 0:(c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return ;
}
int xxxxxxxxx = 1;
inline void outi(int x) {if (x > 9) outi(x / 10);putchar(x % 10 + '0');}
inline void outl(ll x) {if (x > 9) outl(x / 10);putchar(x % 10 + '0');}
inline void debug(ll x) {cout << xxxxxxxxx++ << " " << x << endl;}
inline void debugs(string s) {cout << s << endl;}
const int maxn = 1e3 + 10;
char s[maxn][maxn];
int main() {
int t, n, m, x, y;
read(t);
while (t--) {
int ans = 0;
read(n), read(m), read(x), read(y);
for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i][j] == '.' && s[i][j + 1] == '.') {
s[i][j] = '*';
s[i][j + 1] = '*';
ans += min(2 * x, y);
} else if (s[i][j] == '.') ans += x;
}
}
outi(ans);
putchar('\n');
}
return 0;
}