/*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>
#include <array>
bool legal_triangle(long int y, long int x, long int size, long int dir, long int height, long 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(long int tri_y, long int tri_x, long int tri_size, long int tri_dir, long int cell_y, long int cell_x) {
long int tri_y2 = tri_y + tri_size * tri_dir;
long int offset = ((((tri_y) % 2 == 1) && ((cell_y) % 2 == 0)) ? -1 : 0);
float dy;
long int dyl, dyr;
if (tri_dir == 1) {
dy = (tri_y - cell_y) / 2.0;
dyl = (long int) ceil(dy);
dyr = (long 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 = (long int) ceil(dy);
dyr = (long 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]
#prlong int("#{fish} is in tri")
end
end
return val # total - val for true value
end
*/
long int get_value(long int y, long int x, long int size, long int dir, std::vector<std::array<long int, 3>> *fishies) {
long int val = 0;
std::array<long int, 3> fish;
for (long unsigned long 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;
}
long int main() {
long int height, width, count;
std::cin >> height >> width >> count;
std::vector<std::array<long int, 3>> fishies;
long int total = 0;
long int min_size = ((height <= 10) ? 1 : 10);
long int y, x, t;
char ct;
std::array<long int, 3> temp;
for (long int i = 0; i < count; i++) {
std::cin >> y >> x >> ct;
t = ((ct == 'H') ? 1 : -10);
temp[0] = y - 1;
temp[1] = x - 1;
temp[2] = t;
fishies.push_back(temp);
total += t;
}
long int size, dir;
bool legal, c;
long int max_value = 0;
long int value;
for (long int y = 0; y < height; y++) {
for (long 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)
*/