First string includes 4 integers: N, M, A, B (1 ≤ N, M ≤ 300, A, B ≤ 1000).
Each next line include M-symbols. Symbol .
is a clean cell, *
and **
are dirty.
I need to find the sum for cleaning if A
its sum of **
cell, B
is sum of *
.
What I can do better?
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::string;
using std::vector;
int main() {
int n, m, a, b;
cin >> n >> m >> a >> b;
vector<string> parquet;
int itr = 0;
while (itr < n) {
string cell;
cin >> cell;
parquet.push_back(cell);
itr++;
}
int sum = 0;
itr = 0;
while (itr < parquet.size()) {
for (int i = 0; i < parquet[itr].size(); i++) {
if (parquet[itr][i] == '*') {
if (parquet[itr][i + 1] == '*') {
i++;
sum += a;
} else {
sum += b;
}
}
}
itr++;
}
cout << sum;
}
.*
" is going to cause you to access out of bounds coordinates. \$\endgroup\$if (parquet[i][j + 1] != EOF && parquet[i][j + 1] == '*')
, right? \$\endgroup\$if (parquet[itr][i + 1] == '*')
toif(i + 1 < parquet[itr].size() && parquet[itr][i+1] == '*')
. \$\endgroup\$