Count how many wolves can eat pigs on a small grid by checking adjacent cells.
You are given a 2D grid made of three kinds of cells:
'.' — empty cell'P' — a pig'W' — a wolfA wolf can eat a pig if they are in adjacent cells, horizontally or vertically. Each wolf can eat at most one pig, and each pig can be eaten by at most one wolf.
Find the maximum number of pigs that can be eaten.
Choose pairings between neighboring wolves and pigs so that the number of eaten pigs is as large as possible.
'.', 'P', or 'W'.Example 1
Input
3 3 P.W .W. P.P
Output
2
Explanation
The wolf in the top row can eat the pig to its left or right, and the bottom wolf can eat one of the remaining adjacent pigs. In total, 2 pigs can be eaten.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.