Once upon a time there were several little pigs and several wolves on a two-dimensional…
- User
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n × m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves? Input
The first line contains integers n and m (1 ≤ n, m ≤ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each — that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number — the maximal number of little pigs that may be eaten by the wolves.
Examples
Input
2 3 PPW W.P
Output
2
Input
3 3 P.W .P. W.P
Output
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
Assistant
We need to solve a problem. Let's restate: There's an n x m grid (n,m <=10). Cells contain '.' (empty), 'P' (pig), or 'W' (wolf). The constraint: each little pig has at most one wolf adjacent to it (i.e., in the four cardinal directions). So each pig can be adjacent to 0 or 1 wolf. However, a wolf can be adjacent to multiple pigs. Each wolf can eat at most one adjacent pig (choose one of its adjacent pigs, if any). Once a pig is eaten, it's gone. We want to maximize the number of pigs eaten.
Given the constraint that each pig has at most one adjacent wolf, what is the maximum number of wolves that can eat a pig? This is essentially a matching problem: we have wolves and pigs as nodes. There is an edge between a wolf and a pig if they are adjacent. Each pig can be matched to at most one wolf (since it can be eaten only once). Each wolf can be matched to at most one pig. We need maximum matching. However, there is additional constraint that each pig has degree <=1 (given by problem statement). That simplifies matching: The graph is a bipartite graph where all edges are between wolves and pigs. Since pigs have degree 0 or 1, each pig is incident to at most one wolf. This means the graph is a collection of star-like subgraphs centered on wolves? Actually, a wolf can be connected to multiple pigs, but each pig is connected to at most one wolf. So the graph is a set of disjoint "stars" where the center is a wolf, and leaves are pigs. However, it's not necessarily disjoint because a pig is only connected to at most one wolf, so each pig belongs to at most one star. But what if two wolves are adjacent to the same pig? That can't happen because then the pig would have at least two adjacent wolves, violating the condition. So indeed each pig is adjacent to at most one wolf. So each pig has 0 or 1 incident edges. Therefore, the bipartite graph has all pigs with degree 0 or 1. The wolves can have arbitrary degree.
Now we want maximum matching where each wolf matches to at most one pig. Since each pig has at most one incident edge, if a pig has degree 1 (i.e., adjacent to a wolf), then that pig can only be matched to that wolf. However, a wolf may be adjacent to multiple pigs; it can match to at most one of them. So the problem reduces to: For each wolf, decide to match it to one of its adjacent pigs (if any), but ensuring that no pig is matched to more than one wolf. Since each pig is only adjacent to that wolf (if any), the only conflict is if two wolves share the same pig? That cannot happen due to constraint. So conflicts can only arise when a pig is adjacent to a wolf and we try to match both? Actually, the pig's unique adjacency means if we match a wolf to a pig, that pig is not connected to any other wolf, so it's safe. The only potential conflict is that a wolf may have multiple pigs, but matching to one pig doesn't affect other wolves because those pigs are only adjacent to this wolf? Wait, check: Could a pig be adjacent to a wolf, and that wolf be adjacent to other pigs? Yes. Those other pigs are also adjacent only to that same wolf? Possibly. So consider a wolf W with adjacent pigs P1, P2, ..., Pk. Each of these pigs is adjacent only to W (since if they were adjacent to another wolf, that would be degree >1). So the star: W connected to several pigs, each pig only connected to W. In such a star, maximum matching size is min(1, k) = 1 if k>=1, because the wolf can match to at most one pig, and each pig can match to at most one, but only one edge can be selected because the wolf is the limiting factor. So essentially, each wolf with at least one adjacent pig can eat exactly one pig (if we choose to let it eat). There's no conflict between different wolves because each pig belongs to exactly one wolf. So the maximum number of pigs eaten is simply the number of wolves that have at least one adjacent pig? Wait, but careful: Is it possible that a pig is adjacent to no wolf (degree 0), then it can't be eaten. So those don't count. For each wolf, if it has at least one adjacent pig, we can have it eat one pig. But could there be a scenario where two wolves share a pig? No, because that would give the pig degree 2, which is prohibited. So indeed, the maximum matching is simply the count of wolves that have at least one adjacent pig? Let's test with examples.
Example 1: 2 3 PPW W.P Let's map grid: rows: row0: P P W; row1: W . P
Coordinates (0-index). Let's find adjacency: Wolves at (0,2) and (1,0). Check adjacent pigs: W at (0,2): neighbors: (0,1) = P, (1,2) = '.'? Actually (1,2) is '.'? The grid row1: "W.P" => positions: col0='W', col1='.', col2='P'. So (1,2) is 'P'. So W(0,2) adjacent to (0,1) P and (1,2) P. So it has pigs. W at (1,0): neighbors: (0,0) P, (1,1) ., (2,0) out of bounds, (1,-1) out. So adjacent to (0,0) P. So also has pig. So both wolves have adjacent pigs. According to my reasoning, max = 2. Output is 2. Good.
Example 2: 3 3 P.W .P. W.P Grid: row0: P . W; row1: . P .; row2: W . P Wolves at (0,2) and (2,0). Check adjacency: W(0,2): neighbors: (0,1)=., (0,3) out, (1,2)=., (-1,2) out -> no pigs. W(2,0): neighbors: (1,0)=., (2,1)=., (3,0) out, (2,-1) out -> no pigs. So no wolf has adjacent pig. So max = 0. Output 0.
So seems trivial: count wolves that have at least one adjacent pig. But wait, is there any scenario where a wolf has adjacent pig(s), but we cannot let that wolf eat because that pig might also be adjacent to another wolf? But condition says each pig has at most one wolf adjacent. So if a pig is adjacent to a wolf, it's only that wolf. So no conflict.
However, consider a scenario: Wolf W1 has pig P1 adjacent. Wolf W2 has pig P2 adjacent. No conflict. So both can eat.
What about a wolf with two adjacent pigs; we can only eat one, but that's okay; the maximum number of pigs eaten is still the number of wolves that have at least one pig, because we can choose one pig for each such wolf. That yields exactly that many pigs eaten. But could we ever eat more than the number of wolves? No, because each wolf eats at most one pig. Could we ever eat more than the number of pigs with degree >=1? Since each pig with degree 1 can be eaten only if the adjacent wolf eats it, but if a wolf has multiple pigs, it can only eat one, so the other pigs are uneaten. So the maximum number of eaten pigs is limited by the number of wolves that have at least one adjacent pig. But could there be a case where a pig has degree 0, but could be eaten indirectly? No.
Wait, but the problem statement: "They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf." So it's exactly the matching scenario.
Given the condition "there will be at most one wolf adjacent to each little pig", the maximum matching equals the number of wolves that have at least one neighbor pig? Let's test a more complex scenario: Suppose there are two wolves and one pig. The pig is adjacent to both wolves? That would violate the condition because the pig would have two adjacent wolves. So cannot happen. So the pig can be adjacent to at most one wolf. So if there is one pig adjacent to a wolf, that wolf can eat it, the other wolf cannot eat any pig because it has no adjacent pig (unless it has its own pig). So total eaten = 1 if that wolf has at least one pig. That equals number of wolves with adjacent pigs.
But what about a wolf with a pig that is also adjacent to another pig? No.
Thus answer is simply: For each cell that is 'W', check its four neighbors; if any neighbor is 'P', then increment answer, and also maybe mark that pig as eaten? But careful: Could there be a case where a pig is adjacent to two wolves? No by condition. So no pig will be counted twice. So we can just count wolves that have at least one adjacent pig. However, there is a subtlety: Suppose a wolf has multiple adjacent pigs. Counting the wolf once is correct, because it can only eat one. So answer is number of wolves that have at least one adjacent pig.
But is it always possible to realize that maximum? Could there be a conflict where two wolves share a pig? Not allowed. Could there be a case where a wolf's only adjacent pig is also adjacent to another wolf? Not allowed. So yes, each such wolf can simply eat any adjacent pig. There's no competition for pigs.
But wait: The process is sequential: One by one, each wolf will choose one of the little pigs adjacent to it (if any). Since the pigs disappear after being eaten, could this affect other wolves' options? For example: Wolf A has two adjacent pigs P1 and P2. Wolf B has only P2 adjacent? That would violate the condition because P2 would have two wolves adjacent. So not allowed. What about Wolf A has pigs P1 and P2. Wolf B has pig P2? Not allowed. So the only possible interaction is if Wolf A eats a pig that is also adjacent to Wolf B? Not allowed. So no conflict.
But there could be a scenario where Wolf A and Wolf B are both adjacent to distinct pigs, but those pigs are not overlapping. So sequential order doesn't matter.
Thus the answer is simply: count the number of 'W' cells where at least one of its four neighbors is 'P'.
But wait, is there any scenario where a wolf has an adjacent pig, but we cannot eat that pig because after some other wolf eats, something changes? Since the pig is only adjacent to that wolf, eating it only removes that pig. That does not affect any other wolf because no other wolf was adjacent to that pig. So it's fine.
Thus the solution seems trivial.
However, the problem is from Codeforces: I recall problem "Little Pigs and Wolves" (CF 116B?). Actually there is a problem: "Little Pigs and Wolves" - I think it's Codeforces Round #116? Actually I recall a problem: There is a grid with pigs and wolves. The condition "at most one wolf adjacent to each little pig" is given. The solution is indeed to count the number of wolves that have an adjacent pig, because each pig can be adjacent to at most one wolf, so it's safe. Many people solved it that way. So it's correct.
But let's double-check: Could there be a case where a wolf has adjacent pig, but if it eats that pig, then another wolf loses its only adjacent pig because it was the same? That would require pig adjacent to both wolves, not allowed. So no.
Thus answer is sum over each 'W' of (any neighbor is 'P')? However, we must ensure that we don't double-count the same pig if two wolves are adjacent to it? Not allowed. So just do that.
But wait: The input guarantees that each pig has at most one adjacent wolf. It does not guarantee that each wolf has at most one adjacent pig, obviously. So counting wolves that have any pig is fine.
Let's test some custom edge cases:
n=1,m=3: W P W. Here pigs: P at (0,1). Wolves at (0,0) and (0,2). Check adjacency: Wolf at (0,0) neighbor right (0,1) is P -> has pig. Wolf at (0,2) neighbor left (0,1) is P -> has pig. But pig has two wolves adjacent, which violates condition. However, input guarantee says at most one wolf adjacent to each pig. So such input would not be allowed. So we don't consider.
Now consider: n=2,m=2: W P . W Wolves at (0,0) and (1,1). P at (0,1). Check: Wolf(0,0) neighbor right is P -> has pig. Wolf(1,1) neighbor up is (0,1) P -> also has pig. But pig is adjacent to both wolves, violating condition. Not allowed.
Consider allowed: n=2,m=2: W P . . Wolf only at (0,0) adjacent to P, other cells: (1,1) maybe empty. That's allowed. So answer = 1.
Consider: n=2,m=3: W . P . W . Wolves at (0,0) and (1,1). Pigs at (0,2). Check adjacency: Wolf(0,0) neighbor right is '.'; down is '.'; so no pig. Wolf(1,1) neighbors: up (0,1) '.'; left (1,0) '.'; right (1,2) '.'; down (2,1) out -> no pig. So no wolf has pig. But maybe pig (0,2) is adjacent to something else? No. So answer 0. That's fine.
Now consider: n=1,m=3: W P . Here pig adjacent to wolf left, but wolf right? none. Condition satisfied. Wolf has pig. Answer =1.
Now consider a case where a wolf has multiple pigs, e.g., n=2,m=2: W . P P W at (0,0); pigs at (1,0) and (1,1). Adjacency: Wolf(0,0) neighbor down (1,0) is P -> has pig. So answer =1. But could we eat two pigs? No, because only one wolf. So maximum eaten is 1. Our count gives 1, correct.
Thus the answer is indeed number of wolves with at least one adjacent pig.
But wait: Is there any subtle scenario where a wolf has an adjacent pig but that pig is also adjacent to another wolf? Not allowed. So safe.
Thus we can simply scan grid, for each 'W', check four directions for 'P', if found increment ans and break (to avoid counting multiple times per wolf). That yields answer.
But is there any scenario where a pig is adjacent to a wolf but we shouldn't count that wolf because maybe that pig could be eaten by another wolf? There is no other wolf. So no.
Thus trivial solution.
But the problem is probably from Codeforces round # something, and many participants did greedy: for each wolf, if adjacent pig, eat it and mark pig as eaten. But they also needed to mark pig as eaten to avoid double counting if two wolves adjacent to same pig, but that's not possible due to constraint, but sometimes they don't rely on constraint? Actually constraint is given, so it's safe. Some solutions count for each wolf any adjacent pig and increment, because no conflict.
Alternatively, they might have done it as a bipartite matching, but given constraints, they could also solve with max flow, but the simple solution is enough.
Thus answer: count of wolves that have at least one adjacent pig.
But let's double-check the problem statement: "It is guaranteed that there will be at most one wolf adjacent to any little pig." Yes.
So we can output the number of wolves that have at least one pig neighbor.
Thus the solution code is straightforward.
But wait: Could there be a scenario where a wolf has an adjacent pig, but that pig is also adjacent to another pig? That doesn't matter.
Thus answer is trivial.
Now, we must produce final output: print integer.
Now the user asked: "Please reason step by step, and put your final answer within \boxed{}." They want solution reasoning and final answer. Since it's a problem, they likely want the final answer as a number for given input? Actually they gave examples, but we need to produce the solution? The user did not provide a specific input; they want the solution to the problem in general? The conversation is: "You are a mathematician..." with problem statement and examples. The user likely wants the solution approach and final answer in terms of algorithm, not a specific number. But the instruction says: "Please reason step by step, and put your final answer within \boxed{}." Typically for such problems, the final answer is the maximum number, but that's variable. However, I suspect they want the answer expressed as: The maximum number equals the number of wolves that have at least one adjacent pig. But maybe they want the code or the method? The instruction from system: "You should output the final answer within \boxed{}." Usually for math problems, the final answer is a numeric value. But here it's an open-ended problem with input. Perhaps they expect a solution explanation and final answer as "the number of wolves that are adjacent to at least one pig". But the box would then contain that description. However, typical format for these types of questions (like from Codeforces) is to output the answer after computing based on given input, but here no specific input is given. Actually, the user provided the problem statement and asks: "Please reason step by step, and put your final answer within \boxed{}." This is ambiguous. Perhaps they want the solution approach, and the final answer is the answer for the examples? But they gave two examples. Usually, a tutor would explain the reasoning and then give the answer. But the final answer might be the output for the specific input? But the problem asks to print a single number for each test case; they didn't give a test case. Possibly they want the general formula. But in many such interactions, the final answer is the algorithm: "Count the number of wolves that have at least one adjacent pig." But they might expect code. But the instruction says "put your final answer within \boxed{}". In typical math problem, the answer is a number or expression.
I think we should produce the solution explanation, and then inside the box we could put the expression: (\text{number of wolves with at least one adjacent pig}). However, the instruction is from the system prompt: "You are a mathematician... When you think about a math problem, you think step by step and then put your final answer in \boxed{}." That suggests that the final answer should be a numeric answer or a closed-form expression. But this is a programming problem, not a math problem with a fixed answer. However, the problem could be interpreted as: Determine the maximum number of little pigs that may be eaten, given the constraints. And from reasoning, we deduced it's simply the number of wolves that have at least one adjacent pig. That is a description, not a number.
But perhaps they want a function: Let A be the set of wolves; for each wolf w, if there exists adjacent pig, count++; answer = count.
I think the best is to state: The maximum number equals the number of wolves that are adjacent to at least one pig. And then put that in the box as a statement? However, the typical final answer in such problems is a number for given input, but no input provided.
Given the examples: For first example answer is 2, for second answer is 0. But they already gave those.
Maybe the user wants us to solve the problem for a specific hidden test case? Not likely.
Let's read the conversation: "You are a mathematician... Write a solution to the following math problem: ..." The problem is the one described. They ask: "Please reason step by step, and put your final answer within \boxed{}." So we need to produce a solution that reads input and outputs the answer. But as a language model, we cannot run code, but we can provide reasoning and final answer. I think the expectation is to output the answer as the number for the given input? But there is no input given besides the examples. The problem statement includes examples but not a specific input to solve. So it's a general problem.
Maybe they expect the answer to be: "The maximum number is the number of wolves that have at least one pig neighbor." That can be placed in \boxed{} as text. But it's unconventional.
Alternatively, maybe they want to derive a formula: answer = sum_{wolves} [exists adjacent pig]. That is essentially it.
Given the instructions, I will provide a step-by-step reasoning and conclude that the answer is the number of wolves with an adjacent pig. I'll put that description in \boxed{}.
But I've seen similar interactions where the final answer is a statement like "the number of wolves that have at least one adjacent pig". So I'll do that.
I'll produce a thorough explanation.
Step-by-step:
- Represent wolves and pigs as nodes, edges between adjacent cells.
- Constraint: each pig has degree ≤ 1.
- Each wolf can eat at most one adjacent pig, each pig can be eaten at most once.
- This is a bipartite matching problem: maximum matching in a graph where all pigs have degree 0 or 1.
- In such a graph, any pig with degree 1 can only be matched to its unique adjacent wolf. However, a wolf may have multiple such pigs; it can be matched to at most one.
- Thus, the maximum matching size equals the number of wolves that have at least one pig neighbor (since each such wolf can be matched to one of its adjacent pigs, and there is no conflict because each pig is adjacent to at most one wolf).
- Therefore, answer = count of 'W' cells that have a 'P' in any of the four orthogonal neighboring cells.
Proof: Since each pig is adjacent to at most one wolf, no two wolves compete for the same pig. Therefore, each wolf that has any adjacent pig can eat one pig, and all such choices are independent. So the maximum total is exactly the number of wolves with at least one adjacent pig.
Thus final answer: \boxed{\text{the number of wolves adjacent to at least one pig}}.
But maybe they'd like to see a more mathematical expression: (#{ (i,j) : grid[i][j] = 'W' \land \exists (dx,dy) \in {(1,0),(-1,0),(0,1),(0,-1)} \text{ such that } grid[i+dx][j+dy] = 'P' }).
That can be put in box.
I'll put that. The problem reduces to a bipartite matching between wolves and pigs. Given that each pig has at most one adjacent wolf, no pig can be claimed by more than one wolf. Consequently, each wolf that has at least one neighboring pig can eat one pig, and these choices do not interfere. Therefore, the maximum number of pigs eaten is exactly the number of wolves that are adjacent to at least one pig.
Thus, the answer is:
[ \boxed{\text{the number of wolves that have at least one adjacent pig}} ]
In terms of the input, it is the count of cells containing 'W' for which at least one of the four orthogonal neighbors contains a 'P'.