/*def legal_triangle(y, x, size, dir, height, width) # dir = -1 or 1 # O(1)
return x >= 0 &&
(x + size) <= (width) &&
(y + (dir * size) + 1) >= 0 &&
(y + (dir * size)) <= height
end*/
#include <iostream>
#include <cmath>
#include <vector>
bool legal_triangle(int y, int x, int size, int dir, int height, int width) {
return (x >= 0) &&
((x + size) <= width) &&
((y + (dir * size) + 1) >= 0) &&
((y + (dir * size)) <= height);
}
/*def in_triangle(tri_y, tri_x, tri_size, tri_dir, cell_y, cell_x) # O(1)
tri_y2 = tri_y + tri_size * tri_dir
offset = ((((tri_y) % 2 == 1) && ((cell_y) % 2 == 0)) ? -1 : 0)
if tri_dir == 1
dy = (tri_y - cell_y) / 2.0
dyl = dy.ceil.to_i
dyr = dy.floor.to_i
return (tri_y <= cell_y && cell_y < tri_y2) && # dir = 1
(tri_x - offset) <= (cell_x + dyl) && # both
(cell_x + offset) < (tri_x + (tri_size + dyr)) # both
else
dy = (cell_y - tri_y) / 2.0
dyl = dy.ceil.to_i
dyr = dy.floor.to_i
return (tri_y2 <= cell_y && cell_y <= tri_y) && # dir = -1
(tri_x - offset) <= (cell_x + dyl) && # both
(cell_x + offset) < (tri_x + (tri_size + dyr)) # both
end
end*/
bool in_triangle(int tri_y, int tri_x, int tri_size, int tri_dir, int cell_y, int cell_x) {
int tri_y2 = tri_y + tri_size * tri_dir;
int offset = ((((tri_y) % 2 == 1) && ((cell_y) % 2 == 0)) ? -1 : 0);
float dy;
int dyl, dyr;
if (tri_dir == 1) {
dy = (tri_y - cell_y) / 2.0;
dyl = (int) ceil(dy);
dyr = (int) floor(dy);
return (tri_y <= cell_y && cell_y < tri_y2) &&
(tri_x - offset) <= (cell_x + dyl) &&
(cell_x + offset) < (tri_x + (tri_size + dyr));
} else {
dy = (cell_y - tri_y) / 2.0;
dyl = (int) ceil(dy);
dyr = (int) floor(dy);
return (tri_y2 <= cell_y && cell_y <= tri_y) &&
(tri_x - offset) <= (cell_x + dyl) &&
(cell_x + offset) < (tri_x + (tri_size + dyr));
}
}
/*
def get_value(y, x, size, dir, fishies)
val = 0
fishies.each do |fish|
if in_triangle(y, x, size, dir, fish[0], fish[1])
val += fish[2]
#print("#{fish} is in tri")
end
end
return val # total - val for true value
end
*/
int get_value(int y, int x, int size, int dir, std::vector<int> *fishies) {
int val = 0;
int fish[3];
for (long unsigned int i = 0; i < fishies->size(); i++) {
fish = fishies->at(i);
if (in_triangle(y, x, size, dir, fish[0], fish[1])) {
val += fish[2];
}
}
return val;
}
int main() {
int height, width, count;
std::cin >> height >> width >> count;
std::vector<int, 3> fishies;
int total = 0;
int min_size = ((height <= 10) ? 1 : 10);
int y, x, t, total;
char ct;
for (int i = 0; i < count; i++) {
std::cin >> y >> x >> ct;
t = ((ct == 'H') ? 1 : -10)
fishies.push_back([y - 1, x - 1, t]);
total += t;
}
int size, dir;
bool legal;
int max_value = 0;
int value;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
size = min_size;
dir = -1;
c = true;
while (c) {
legal = legal_triangle(y, x, size, dir, height, width);
if (legal) {
value = get_value(y, x, size, dir, *fishies);
if (max_value < value) {
max_value = value;
}
size += 1;
} else {
if (dir == -1) {
size = min_size;
dir = 1;
} else {
c = false;
}
}
}
}
}
std::cout << total - max_value;
return 0;
}
/*
height, width, count = gets.chomp.split(" ").map(&:to_i)
fishies = []
total = 0
min_size = (height <= 10 ? 1 : 10)
count.times do
y, x, t = gets.chomp.split(" ")
t = (t == "H" ? 1 : -10)
fishies << [y.to_i-1, x.to_i-1, t]
total += t
end
values = []
(0...height).each do |y|
(0...width).each do |x|
size = min_size
dir = -1
continue = true
while continue
legal = legal_triangle(y, x, size, dir, height, width)
if legal
values << get_value(y, x, size, dir, fishies)
size += 1
#puts total - values[-1]
else
if dir == -1
size = min_size
dir = 1
else
continue = false
end
end
end
end
end
puts(total - values.min)
*/