Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R × C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 ≤ R ≤ 500) and C (1 ≤ C ≤ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve the problem: Given a grid with S (sheep), W (wolf), . (empty). We can place dogs (D) on empty cells (.) to block wolves from reaching any sheep. Wolves can move freely (like 4-directionally) as long as they don't step into a cell with a dog. So we need to ensure that for every wolf, there is no path to any sheep via empty cells (including through other wolves? Actually wolves can move through empty cells; they can also pass through other wolves? The problem says: wolves can roam freely by moving to neighboring cells (left, right, up, down). It does not forbid moving into a cell with a wolf? Typically such puzzles assume that multiple wolves can coexist. However, movement into a cell with a wolf might be considered occupied; but since wolves can move, they could move into a cell currently occupied by another wolf? Usually in such problems, each cell can hold only one entity; wolves can move but they cannot share cells? The description: "Each cell is either empty, contains a sheep, a wolf or a dog." So at any given time, each cell has exactly one state. Initially there are no dogs. Wolves move repeatedly. In movement, they would leave a cell and enter another. So they cannot move into a cell already occupied by another wolf, because that cell is not empty. So wolves cannot occupy the same cell. However, we consider the wolves' potential paths: Since wolves can move, they might coordinate? But we just need to ensure no wolf can ever reach a sheep. We are placing dogs to block paths. This is essentially a graph problem: Cells are nodes, edges between adjacent cells (4-neighbor). Wolves cannot step on dogs. So we want to block any path from any W node to any S node, by placing dogs on some . nodes (which are initially empty). Dogs are placed and remain static; wolves can move, but we cannot remove wolves or sheep. The dogs can only be placed on '.' cells initially. We are not allowed to place dogs on cells that originally contain S, W, or presumably on the same cell as S or W. Only empty cells can become dogs.
If there is any W adjacent to an S directly (sharing a side), then obviously there is no way to block because wolves can move into the sheep's cell in one step. The only way to block would be to put a dog in between, but they are adjacent, so there's no empty cell between them. Since we cannot place a dog on S or W, we cannot prevent that wolf from moving into the sheep cell. So it's impossible. So immediate check: if any neighbor of a W is S, output "No".
If not, then perhaps we can always place dogs to protect sheep? Not necessarily; there might be a path that goes around but cannot be blocked because we can only place dogs on empty cells, and maybe we need to block multiple paths but the empty cells are limited? However, we can place many dogs (no need to minimize). So we can potentially place dogs on all empty cells, but we have to ensure we don't block sheep? Actually, placing dogs on all empty cells would definitely block all wolves, because wolves can only move to empty cells? Wait, wolves can move to empty cells, but can they move into a sheep cell? That's the goal. If we fill all empty cells with dogs, wolves cannot move at all because all neighboring cells are either wolves, sheep, dogs? But if there are empty cells that we fill, wolves cannot move into dogs. They might be surrounded by dogs and wolves and maybe sheep? But if there's a wolf adjacent to a sheep, we already said impossible. If no adjacent, then all paths from W to S go through some empty cells. If we fill all empty cells with dogs, then there is no path. However, is it allowed to place dogs on all empty cells? The problem says "You are not allowed to move, remove or add a sheep or a wolf." Dogs can be placed on empty cells. So yes, we could just put dogs on every '.' and then wolves are isolated, they can't move because all adjacent cells are either W, S (but if adjacent to S, that would be impossible as we already check), or D (dogs). But can wolves move into cells containing other wolves? No, because those cells are occupied. So indeed, if we put dogs on all '.' cells, wolves cannot move anywhere, so they cannot reach any sheep as long as no wolf is adjacent to a sheep. So the answer would always be "Yes" if there is no direct adjacency between W and S. But wait, there's a catch: The example 3 had no wolves, but they placed a dog. But they said "the solution would be correct even without him." So in that case, they could have no dogs. But if there are wolves, we might need to place dogs to block. But is simply placing dogs on all empty cells always valid? Let's think: If we place dogs on all empty cells, what about the fact that wolves might be able to move into cells that were originally wolves? But wolves cannot move into cells containing other wolves because they are not empty. So they'd be stuck. However, what about the scenario: A wolf is adjacent to a sheep? That's already impossible. If not, then it's safe. So why does the problem exist? It might be because placing dogs on all empty cells is allowed and solves the problem, but perhaps there is a constraint: dogs cannot be placed such that they are adjacent to a sheep? No such constraint. So the problem would be trivial: check for any W adjacent to S, if none, output Yes and replace all '.' with 'D' (or even leave them as '.' because no dogs needed? Actually we need to output a configuration with dogs placed. It says "Place dogs onto the pasture in such a way that no wolf can reach any sheep". It doesn't require that we place dogs if not needed, but we need to output a configuration (the pasture after placing dogs). So we can output the original grid but with all '.' replaced by 'D' (or some of them). So why are there multiple solutions? That is because we can choose any placement. So it's indeed that simple? But that can't be because the problem is from some contest and it's not that trivial. Perhaps there is a nuance: wolves can move through other wolves? The problem statement: "Each cell is either empty, contains a sheep, a wolf or a dog." That implies each cell can only hold one type at a time. When wolves move, they leave their cell empty. Could a wolf move into a cell that currently contains another wolf? That would violate the single occupancy. Typically in these grid movement problems, they consider that wolves can move sequentially but at any instant only one per cell. So they cannot occupy the same cell. So it's as we thought. So why is this problem considered non-trivial? Let's look at the example 1: They didn't fill all '.' with D. They placed only some D's, not all. So obviously we can do that, but is there any scenario where filling all '.' with D is not allowed? Possibly because we might block the sheep? Sheep are not moving; dogs do not affect sheep directly. So it's fine. Unless there is a hidden condition: "Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell." It does not say wolves cannot move into cells that contain other wolves; but if it is occupied, they cannot. Usually they can't.
Maybe the catch is: When we place dogs, we are not allowed to place them on cells that are adjacent to something? No.
Wait, maybe the problem is that we need to ensure that wolves cannot reach any sheep, but the wolves could also move through other wolves? That would be weird.
Another possibility: The pasture is large (R,C up to 500). If we output a grid with all '.' replaced by 'D', that's acceptable. But maybe the problem expects us to find a minimal number of dogs or something? No, it explicitly says you do not need to minimize their number. So indeed the trivial solution is to put dogs everywhere. So why would there be a "No" case? Only when a wolf is directly adjacent to a sheep. That seems too easy, but is that always correct? Let's test with some edge cases.
Consider a scenario:
W . S
Here W and S are separated by an empty cell. If we put a dog on the empty cell, wolf cannot move to that cell (since it's a dog) and thus cannot reach S. So safe. If we put dogs everywhere, that includes that empty cell.
Consider:
W . . S
With multiple empty cells in a path. If we put dogs on all empty cells, then all paths are blocked. So safe.
Thus the only impossible case is when a wolf is directly adjacent to a sheep because there is no empty cell to block that immediate step.
But is there any scenario where a wolf is not directly adjacent but still cannot be blocked because there is no empty cell to place a dog? For blocking a path, you need to block at least one cell on each path. If we can place dogs on all empty cells, we definitely block all paths. So the only way it's impossible is if there is no way to block all paths, which would happen if there exists a path that does not contain any empty cell? That would mean the path consists solely of W and S cells. But the only way for a path between W and S to have no empty cells is if they are adjacent (directly) because any path of length >1 would need an intermediate cell. Could the intermediate cell be a wolf? Possibly, if there is a chain of wolves: W - W - S? That would require two wolves adjacent, then S adjacent to second wolf. But can a wolf move through other wolves? If they cannot step into a cell with another wolf, then a wolf cannot pass through another wolf. However, perhaps the wolf could move into the cell that the other wolf just vacated? But that's dynamic movement; we need to consider if there exists any sequence of moves that leads a wolf to a sheep, given that wolves can move simultaneously? Typically, these puzzles consider that wolves are like agents that can move arbitrarily, and they can coordinate. But the problem statement: "Wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell." It doesn't say they cannot occupy the same cell, but it's implied by the grid composition: each cell can contain only one entity at a time. It doesn't say multiple wolves can't occupy the same cell either. But usually such puzzles assume that multiple wolves can't occupy same cell because then they'd need to be represented somehow. The grid is given with one character per cell. So at any given moment, each cell's state is one of: empty, sheep, wolf, dog. So if a wolf moves into a cell that already contains a wolf, that would be two wolves in one cell, which can't be represented. So it's not allowed. So wolves cannot move into a cell occupied by another wolf. Thus the path must consist of empty cells (or maybe through sheep? No, they'd eat upon reaching sheep). So all intermediate cells must be empty. So if there is no empty cell on the path, the only possible path is direct adjacency. So indeed the only impossibility is adjacency.
But wait: What about the scenario where a wolf is adjacent to an empty cell that is adjacent to a sheep? That's fine. So the answer is always "Yes" if no adjacent W-S pairs.
But why then the examples show a more interesting placement? In example 1, they placed dogs not on all empty cells, but only some. Maybe they want to show an alternative solution, but the problem statement says "you may print any of them". So we could output all '.' as 'D'. However, we need to verify if any other constraints might be violated. For instance, could placing a dog on a cell that is adjacent to a sheep cause any issue? No, dogs don't affect sheep. So it's fine.
But maybe the problem expects that we cannot place dogs adjacent to sheep? The statement does not say that. So it's allowed.
Maybe there is an additional nuance: The wolves can start moving, and dogs are placed. After placing dogs, we need to ensure that no wolf can reach any sheep. If we place a dog on a cell that is initially a wolf's adjacent empty cell, the wolf cannot move there. But what if the wolf is already adjacent to a sheep via a path that doesn't use empty cells? That's the adjacency case. So it's fine.
Thus the solution appears trivial: check for any adjacent W and S (up/down/left/right). If found, output "No". Otherwise, output "Yes", and output the grid with all '.' replaced by 'D' (or optionally any placement, but easiest is all '.' become 'D').
But wait, look at Example 1's output: They have "..SD.." etc. They placed dogs on some cells but not all. In particular, they have some '.' still in the output (the last row is "......" which becomes "......"? Actually they output "......", meaning they left all those cells empty. So they placed dogs only where needed. So they didn't replace all '.' with D. So maybe they didn't need to. But our trivial solution would replace all '.' with D. That would be a valid answer as per problem: any output is fine as long as safe. So we could output a grid with D's on all empty cells. That would be accepted.
But hold on: Is it always safe to put a dog on every empty cell? Let's examine carefully: Consider a scenario where a wolf is adjacent to a sheep? Already ruled out. So all other cases: the grid may have a wolf and a sheep not adjacent. If we put dogs on all empty cells, then the wolf is surrounded by dogs and possibly other wolves and sheep? Could the wolf still move into a sheep cell? Only if the sheep cell is directly adjacent to the wolf. Since we checked that no such adjacency exists, the wolf has no direct move to sheep. And because all other neighbors are dogs (or wolves, which are occupied), it cannot move at all. So safe.
Now consider a scenario where there is a sheep adjacent to an empty cell, and that empty cell we put a dog. That's fine. So all good.
Thus the problem reduces to checking adjacency. But why would the problem be given with R,C up to 500 and seemingly require BFS? Perhaps there is a subtlety: wolves can move through other wolves if they move in sequence? For example, suppose we have W1 . W2 S, with W1 and W2 not adjacent to each other? Actually, consider W . W S: positions: (1,1)=W, (1,2)=., (1,3)=W, (1,4)=S. If we put dogs on all '.' cells, then (1,2) becomes D. Then can W1 reach S? W1 can't move to (1,2) because it's D. Could W1 move to (1,?)? Only left/right/up/down. So no. Could W2 reach S? W2 is at (1,3). Its neighbor to right is (1,4)=S. But is it directly adjacent? Yes, W2 is adjacent to S. But that would be caught by adjacency check. So we need to check all wolves. So if any wolf is adjacent to a sheep, it's impossible regardless of placing dogs elsewhere. So our check must scan all cells for W and check neighbors for S.
But is it sufficient? Suppose a wolf is not adjacent to a sheep, but there is a path that goes through other wolves? Could a wolf "pass through" another wolf by swapping? For example, two wolves adjacent to each other, and one moves into the other's cell after the other moves away. In typical grid movement puzzles with multiple moving agents, it's possible to have a chain of movements that allows a wolf to move through a series of wolves, because wolves can move one at a time, vacating cells. However, the problem states "wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell." This implies that each wolf can move at any time, possibly multiple wolves moving. The crucial question: Can a wolf move into a cell that is currently occupied by another wolf? That would cause two wolves in same cell, which is not allowed because each cell can only contain one entity. So unless the other wolf moves out at the same time, it's not allowed. In typical discrete-time movement, moves are considered sequentially or simultaneously? The problem doesn't specify turn-based or simultaneous. Usually in such problems about placing barriers, the analysis is based on whether there exists a path for a wolf to reach a sheep, assuming wolves are free to move arbitrarily and can coordinate, but still they cannot occupy the same cell at the same time. However, they could move in a coordinated way such that a wolf moves into a cell that another wolf just vacated. This is akin to allowing wolves to "slide" through each other via empty spaces. In graph terms, the state space of multiple wolves is complex, but the condition for a particular wolf to reach a sheep might depend on whether there is a path of cells (a sequence) from its starting cell to a sheep cell such that each step moves into a cell that is either empty or contains a sheep (the target), but with the added complexity that other wolves can move out of the way. However, if we consider the worst-case scenario where wolves can move arbitrarily, they could potentially "make room" for each other. So we need to consider if it's possible for a wolf to reach a sheep if we place dogs, taking into account that other wolves can move as well. This is a typical "Wolf and Sheep" problem where you need to block all paths. Many problems of this type (like "Ding Dong Sheep" or "Wolf and Sheep with dogs") reduce to checking if any sheep is adjacent to a wolf; otherwise, you can place dogs on all empty cells adjacent to wolves? But there is a known problem: "C. Sheep" from some contest? Actually I recall a Codeforces problem: "C. Sheep" or "Wolves and Sheep"? Let me think. There is a Codeforces problem: "C. Mike and gcd" not that. Alternatively, there is "C. Ayoub's function"? No.
I recall a problem from USACO or something: "Protecting the Flowers"? Not.
Maybe it's from AtCoder? The problem statement: "Bob is a farmer..." This looks like a problem from Codeforces Round #948 (Div. 2) or something like "C. Wolf and Sheep"? Actually, I've seen a problem: "C. Sheep and Wolves" maybe? Let's search memory. There's a known Codeforces problem: "B. Wolves and Sheep" or "A. Sheep"? Not sure.
Wait, maybe it's from the Croatian Open Competition in Informatics? The style.
I recall a problem: "BOJ 16956 늑대와 양" (Wolf and Sheep) from Baekjoon Online Judge. Yes! That's a classic: In that problem, you have to place dogs to block wolves from sheep. The typical solution: If a wolf is adjacent to a sheep, impossible; else, you can place dogs on all empty cells adjacent to wolves. Or, more commonly, the solution is to place dogs on all empty cells. But I'm not entirely sure. Let's recall the BOJ problem 16956. I think it's exactly that: "늑대와 양" (Wolf and Sheep). The problem: Given R×C pasture with W and S, place D to protect sheep. The solution is: Check if any wolf is adjacent to a sheep; if yes, print 0; else, print 1 and then place dogs on all empty cells? Or maybe they place dogs on all empty cells? I remember that problem: The accepted solution is to check adjacency, then output "1" and then the grid with all '.' replaced by 'D'? Actually, the output in BOJ 16956: if safe, print 1 and then the grid where all '.' become 'D'? Wait, that problem requires to place the minimum number of dogs? No, I think the problem statement: "It's possible to place any number of dogs. Print the configuration after placing dogs." And the typical solution: If a wolf is next to a sheep, print 0; else print 1 and fill all empty cells with D. But I'm not entirely sure. Let's check memory: In BOJ 16956, many solutions output "1" and then the grid with dogs placed on all empty cells. But they also could just place dogs around wolves. Actually, I've solved that problem: It's from Baekjoon. The problem: "Sheep" and "Wolves". The solution: If any sheep is adjacent to a wolf, impossible. Else, you can simply place dogs on all empty cells. That's one solution. But there is an alternative: Place dogs on all empty cells adjacent to wolves, because then wolves cannot move (since they are blocked). But is it sufficient to only place dogs on cells adjacent to wolves? Consider a wolf with an empty neighbor that is not blocked; if we don't put a dog there, the wolf can move into that empty cell. But then it might have further path to a sheep. However, if we only block adjacent to wolves, the wolf could move into a cell that is not adjacent to a wolf initially but becomes adjacent after moving. For example: W . . S. If we put dogs only on cells adjacent to W, we would put a dog on the cell right next to W (the first '.'). That would block the wolf. That seems enough. But what if there are multiple wolves and empty cells chain? For each wolf, if we block all empty cells adjacent to wolves, then wolves cannot move at all (since they have no empty neighbor to move into). But careful: After placing dogs on all empty neighbors of wolves, are there any empty cells not adjacent to wolves? Possibly yes, but wolves can't move into them because they are not adjacent initially; but they could if they could move, but they are blocked from moving at all. Because to move to a non-adjacent empty cell, they would need to first move to an adjacent cell, which we have blocked. So it's sufficient to block all immediate empty neighbors of wolves. That uses fewer dogs than all empty cells. But the problem doesn't require minimization, so both are fine.
But is it always sufficient? Let's think: If a wolf has an empty neighbor, we put a dog there. Then the wolf has no empty neighbor to move to (since all its neighbors are either W, S, D, or out-of-bounds). However, could there be a scenario where a wolf has no empty neighbor initially? That is, all its neighbors are either W, S, or out-of-bounds. But we already check that no neighbor is S, so the neighbors are either W or boundary. Then the wolf cannot move initially. But could it later move after other wolves move away? For example, two wolves adjacent to each other: W1 and W2 are adjacent. Initially, each wolf's neighbors include each other (W). If W2 moves away, W1 might get an empty neighbor. But can W2 move away? W2's neighbors might include an empty cell. But if we placed dogs on all empty neighbors of wolves, then W2's empty neighbor would have a dog, so W2 cannot move into it. So W2 cannot move away. Therefore, no wolf can move. So blocking all empty neighbors of wolves ensures that wolves are immobile. That is sufficient. However, careful: If a wolf has an empty neighbor that is also adjacent to another wolf, we place a dog there, blocking both. So all wolves' adjacent empties become dogs. That yields a configuration where no wolf can move to any empty cell. Also, they cannot move to S because adjacent S is impossible. So safe. So this placement works, using at most 4 * number_of_wolves dogs, which is acceptable.
But is there any scenario where a wolf has no empty neighbor initially, but after some other wolf moves, it could gain an empty neighbor? But as argued, if we place dogs on all empty neighbors of wolves, that blocks any wolf from moving, so no dynamic changes. So it's safe.
Thus the problem reduces to adjacency check and then any placement that blocks all immediate empty cells around wolves.
But wait: Could there be a scenario where a wolf is adjacent to an empty cell that is also adjacent to a sheep? If we put a dog there, that's fine.
Thus overall, the answer is always "Yes" unless there is a direct W-S adjacency. So the problem is indeed trivial.
But I recall that the BOJ problem 16956 has a nuance: You cannot place dogs on cells that are adjacent to sheep? No. Actually, I remember reading some discussions: In that problem, you could just put dogs everywhere, but the sample output didn't. The typical accepted solution does: if any adjacent W and S, print 0; else print 1 and then place dogs on all empty cells, or sometimes they just place dogs on all empty cells adjacent to wolves. That's all.
However, let's double-check the problem statement given here: "You are not allowed to move, remove or add a sheep or a wolf." That's exactly from BOJ 16956. Also the sample inputs and outputs match exactly with that problem. Let's compare: The first sample input:
6 6
..S...
..S.W.
.S....
..W...
...W..
......
The output:
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
In this output, they placed dogs not on all empty cells. They placed dogs on many cells but not all; for instance, the last row is "......" unchanged. So they placed dogs only in cells that were initially empty and adjacent to wolves? Let's verify. Wolf positions: (2,5), (4,3?), actually let's list. Row1: ..S... => positions: row1 col3 S. Row2: ..S.W. => col3 S, col5 W? Actually string "..S.W.": characters: 1 '.',2 '.',3 'S',4 '.',5 'W',6 '.'. So W at (2,5). Row3: .S.... => (3,2) S. Row4: ..W... => (4,3) W. Row5: ...W.. => (5,4) W. Row6: ...... => all '.'.
In output, they have dogs at many places: row1: "..SD.." => at col4 D? Actually "..SD.." => positions: 1 '.',2 '.',3 'S',4 'D',5 '.',6 '.'. So D at (1,4). row2: "..SDW." => (2,3) S? Actually "..SDW." => 1 '.',2 '.',3 'S',4 'D',5 'W',6 '.'. D at (2,4). row3: ".SD..." => (3,1) '.', (3,2) 'S', (3,3) 'D', rest '.'? Actually ".SD..." length 6: 1 '.',2 'S',3 'D',4 '.',5 '.',6 '.'. D at (3,3). row4: ".DW..." => 1 '.',2 'D',3 'W',4 '.',5 '.',6 '.'. D at (4,2). row5: "DD.W.." => 1 'D',2 'D',3 '.',4 'W',5 '.',6 '.'. D at (5,1) and (5,2). row6: "......" unchanged.
So which cells got dogs? (1,4), (2,4), (3,3), (4,2), (5,1), (5,2). Are these cells all empty in original? Yes. Are they all adjacent to wolves? Let's see: Wolf at (2,5). Adjacent cells: (2,4) left, (2,6) right, (1,5) up, (3,5) down. Among these, (2,4) is D; others: (2,6) is '.' in output (not D), (1,5) is '.'? (1,5) originally '.'? In original row1 col5 is '.'? Row1 "..S..." => col5 '.'? Actually positions: col1 '.', col2 '.', col3 'S', col4 '.', col5 '.', col6 '.'; so (1,5) is '.' and remains '.' in output? Output row1 "..SD..": col4 D, col5 '.'? Actually "..SD..": after S at col3, then D at col4, then '.' col5, '.' col6? So (1,5) remains '.'. (3,5) row3 col5 is '.' (output ".SD..." -> col5 '.'). So not all adjacent empty cells of wolf (2,5) got dogs; (2,4) got dog, but (2,6), (1,5), (3,5) remain empty. But that's okay because after placing dogs, can the wolf at (2,5) move to those empty cells? (2,6) is empty; (1,5) empty; (3,5) empty. So the wolf could move to (2,6) for instance. Then from there it might reach a sheep? Possibly? Let's see if there is a path to a sheep after dogs placed. The dogs are placed to block certain paths. They didn't fill all empties adjacent to wolves; they did something else. So how does it protect? They must ensure that even if the wolf moves into those empty cells, it cannot reach a sheep because dogs block further moves. They placed dogs in such a pattern to separate the pasture into two halves. So the solution is not simply blocking immediate neighbors. It's about ensuring that there is no path from any wolf to any sheep in the graph after placing dogs. This is a graph separation problem. However, as we argued, if we put dogs on all empty cells, that certainly blocks all paths. So the trivial solution of all '.' to 'D' would also work. In this sample, they didn't do that; they left some empty cells. So both are valid. So indeed our trivial solution would output a grid with all '.' replaced by 'D', which would be a valid answer. But would it be accepted? According to the problem statement, "If there are multiple solutions, you may print any of them." So yes.
But we must confirm that placing dogs on all empty cells is always safe given no direct W-S adjacency. Let's test a scenario: Suppose there is a wolf at (1,1), empty at (1,2) and sheep at (1,3). W . S. Check adjacency: W at (1,1) not adjacent to S at (1,3) because they are two apart. So no direct adjacency. If we replace all '.' with D, then (1,2) becomes D. Then wolf cannot move to (1,2) because it's D. So cannot reach S. Safe.
Now consider a more complex scenario: Suppose there is a wolf at (1,1), empty at (1,2), empty at (1,3), sheep at (1,4). If we fill all '.' with D, (1,2) and (1,3) become D, blocking. Safe.
Now consider scenario where a wolf and sheep are not adjacent, but there is a path that goes through other wolves? Example: W1 at (1,1), W2 at (1,2), S at (1,3). Check adjacency: W2 is adjacent to S. So that would be caught. So no.
What about a scenario where a wolf is adjacent to an empty cell that is adjacent to a sheep, but we place dog on that empty cell? That's safe. So all good.
Thus the trivial solution is correct. However, is there any scenario where filling all empty cells with dogs might inadvertently cause a problem? For instance, if there is a sheep that is completely surrounded by dogs, that's fine. The dogs don't harm sheep.
One might think about the requirement: "You are not allowed to move, remove or add a sheep or a wolf." So we cannot change S or W. But we can add D to empty cells. So it's fine.
Thus the solution is straightforward.
But wait, I've seen some problems where placing dogs on all empty cells might block the ability to protect because dogs themselves might be eaten? No, dogs are not eaten; wolves cannot enter a cell with a dog.
Thus, why then do many solutions on BOJ 16956 use BFS to check if any sheep is adjacent to wolf, and then output 1 and fill all '.' with D? I recall many submissions do exactly that. Actually, I recall a typical code:
if any neighbor of W is S: print(0) else: print(1) then for each cell: if cell=='.': print('D', end='') else: print(cell)
Yes, that's a common solution. Let me verify with the sample: If we output all '.' as 'D', the output would be:
Yes
..SD..
..SDWD? Wait, careful. For row2 original: "..S.W." -> after replacing '.' with D: would become "DDSDWD"? Actually original row2: characters: 1 '.',2 '.',3 'S',4 '.',5 'W',6 '.'. Replace '.' with D: becomes at col1 D, col2 D, col3 S (unchanged), col4 D, col5 W, col6 D => "DDSDWD". That is not the sample output. But it's still valid, per problem. So yes, it's allowed. The sample output is just one possible solution.
Thus we can implement the trivial solution. It's O(R*C). Check all cells: For each cell if it's 'W', examine its four neighbors; if any neighbor is 'S', print "No" and exit. If we finish checking with no such adjacency, print "Yes", then output the grid with all '.' replaced by 'D'.
Edge Cases: R and C up to 500, so fine.
But before finalizing, we need to think about if there is any subtle case that makes the trivial solution invalid.
Potential subtlety: When we replace all '.' with 'D', we are effectively placing dogs on every empty cell. But could this cause a situation where a wolf that was initially not adjacent to a sheep becomes adjacent after placing dogs? No, because dogs are placed on empty cells, not on wolves or sheep. So adjacency relations between W and S remain unchanged. So if originally there was no W adjacent to S, after placing dogs there is still no W adjacent to S (dogs don't change adjacency). However, could the presence of dogs affect the ability of wolves to move such that they could reach a sheep via some indirect route that goes through dogs? No, because dogs block movement. So it only reduces possible paths. So safety is preserved.
Thus the solution is correct.
Double-check the second example: Input: 1 2, "SW". Here W adjacent to S (since they are next to each other). So we output "No". Works.
Third example: No wolves. So adjacency check passes (no W at all). Then we output Yes and the grid with all '.' replaced by D. But the sample output: they output the same grid but with a dog in the center? Actually they output:
Yes
.S...
...S.
S.D..
...S.
.S...
They placed a dog at (3,3) which was originally '.'? Let's see original grid:
.S...
...S.
S....
...S.
.S...
So originally (3,3) was '.'? Row3: "S...." -> positions: col1 S, col2 ., col3 ., col4 ., col5 . So they placed a dog at col3? Actually output row3: "S.D.." -> col1 S, col2 ., col3 D, col4 ., col5 . So they placed a dog at (3,3) but not all '.' become D. That's okay; we could also output with all '.' as D. That would also be a valid answer. So it's fine.
Thus the answer is trivial.
But maybe there is a twist: The problem might be from a contest where the solution is not trivial because they want to minimize dogs? But they explicitly say "You do not need to minimize their number." So it's okay.
However, I recall that in BOJ 16956, the problem statement originally said: "It's possible to place any number of dogs. Print the configuration after placing dogs." And many people solved it by just putting dogs on all empty cells. Indeed, I've seen that solution. So it's correct.
Therefore, the solution:
Read R, C.
Read grid as list of strings or list of list.
For each cell (i,j) if grid[i][j] == 'W':
For each neighbor (ni, nj) in 4 directions:
if within bounds and grid[ni][nj] == 'S': print "No" and exit.
Print "Yes".
For each row in grid:
For each character c:
if c == '.': print('D', end='') else print(c, end='')
print newline.
That's it.
But let's just think: What about if there are no empty cells? For example, grid has only W and S. If there is any adjacency, we output No. If there is no adjacency, then there are no paths because all cells are either W or S. Could a wolf move into a sheep cell if they are not adjacent? They would need to move through a cell, but there are no empty cells, and they cannot move into another wolf's cell, so they cannot move at all. So it's safe. In that case, we output Yes and the grid unchanged (since no '.' to replace). So that's fine.
Now check: Could there be a scenario where a wolf is not adjacent to a sheep but can reach a sheep by moving through other wolves after they move? For example, consider a 2x2 grid:
W .
. S
Positions: (1,1)=W, (1,2)=., (2,1)=., (2,2)=S. No direct adjacency: W adjacent to (1,2) and (2,1), not to S. If we put dogs on all empty cells, we put D on (1,2) and (2,1). Then W cannot move. Safe.
Now consider a case where there is a chain of wolves, and an empty cell that allows movement after swapping. Example: 1x4: W W . S. Check adjacency: W at position 2 is adjacent to S at 4? Actually indices: 1:W, 2:W, 3:., 4:S. Wolf at index 2 is adjacent to index 3 (empty) and index 1 (W), not to S. Wolf at index 1 adjacent to index 2 (W). So no direct adjacency. If we put dogs on all empty cells, we put D at index 3. Then W at index 2 cannot move to index 3, so cannot reach S. W at index 1 cannot move to index 2 because it's occupied by W, and cannot move to other. So safe.
What if we didn't put dogs on all empty cells but only on some, could there be a scenario where putting dogs on all empty cells somehow allows wolves to move? No, dogs block.
Thus trivial solution stands.
One more check: Are dogs allowed to be placed on cells adjacent to wolves? Yes. So no problem.
Thus final solution is simple.
But given that this is a problem for a contest, maybe there is an intention to test more advanced reasoning, but the problem explicitly says you don't need to minimize the number of dogs. So the simplest solution is to place a dog on every empty cell. However, we must be careful: The problem says "Place dogs onto the pasture in such a way that no wolf can reach any sheep". If we place dogs on all empty cells, then certainly wolves cannot reach any sheep because they can't move into dog cells and they can't move into other wolves. But could a wolf move into a sheep cell if it is adjacent? That would have been caught. So yes.
Thus the answer is trivial.
But hold on: There is a nuance: The wolves can roam freely, meaning they can move through other wolves if those wolves also move. However, if we place dogs on all empty cells, then there are no empty cells for wolves to move into. But could wolves move by swapping positions? For example, two wolves adjacent to each other: if they both move simultaneously, could they exchange positions? Usually, if moves are considered simultaneous, it's possible that two wolves swap cells if both move into each other's cells at the same time. But is that allowed? The problem does not specify whether moves are sequential or simultaneous. In many such puzzles, we consider that moves are sequential, meaning one wolf moves at a time, and the grid updates. Simultaneous moves could cause conflicts. Typically, in such problems, the analysis uses the concept of connectivity: if there exists a path of empty cells from a wolf to a sheep, then a wolf can eventually reach a sheep, because you can move wolves one by one to clear the path. But if all empty cells are blocked by dogs, there is no path. However, what about paths that go through other wolves? For example, suppose we have W1 - W2 - S, with W2 adjacent to S, but no empty cells. If we place dogs on all empty cells (none), then W2 is adjacent to S. That's caught by adjacency check, so impossible. So okay.
Consider a scenario with empty cells but we place dogs on all of them. Then there is no empty cell for any wolf to move into. Could a wolf still move into a cell occupied by another wolf if that wolf moves out at the same time? If moves are sequential, a wolf cannot move into a cell that is currently occupied. So to move, the target must be empty at that moment. So if there are no empty cells, no moves possible. Therefore safe.
If moves are simultaneous, could two wolves swap? Suppose two wolves adjacent: positions A and B. If both attempt to move into each other's cell simultaneously, the result would be that they swap. However, after swapping, each wolf still occupies a cell. But does this constitute a move? The problem statement: "by repeatedly moving to the left, right, up or down to a neighboring cell." It doesn't forbid simultaneous moves, but typically in such puzzles, we assume each move is a single step, and wolves can move one after another. But even with simultaneous moves, if there are no empty cells, swapping would require both cells to be empty after the move? Actually, swapping would require that the target cell is occupied at the start by the other wolf, but if both move simultaneously, it might be allowed if the rules permit "teleportation"? Usually, in grid movement problems with multiple movable agents, simultaneous moves are not considered because they cause conflicts unless specified. The problem likely expects the simpler interpretation: wolves move one at a time, and cannot enter occupied cells. This is standard in puzzles like "Wolf and Sheep" where you place barriers. So it's safe.
Given the problem's simplicity and sample, I'm convinced the solution is correct.
Thus we will implement as described.
But wait, the sample output in first example shows they placed dogs not on all empty cells, but they also left many empty cells. Could those empty cells be reachable from wolves? Let's test if the sample configuration is indeed safe. We can simulate: Wolves at (2,5), (4,3), (5,4). Check connectivity to any sheep (S at (1,3),(2,3),(3,2)). In the output grid, there are many '.' cells. Could a wolf from (2,5) move to (2,6) (empty), then further? Possibly it could go to (3,6) (empty in output? row3 col6 is '.' from ".SD..."? row3 col6 '.'? Actually row3 ".SD..." length 6: col1 '.', col2 'S', col3 'D', col4 '.', col5 '.', col6 '.' so col6 is '.'.) So from (2,6) can go down to (3,6) etc. Could eventually reach a sheep? We need to see if there is any path that avoids dogs. The dogs placed might block all paths. It seems plausible that they created a barrier of dogs separating the region containing wolves from region containing sheep. So indeed their placement is valid. So our all-D placement is also valid, but uses more dogs.
Thus answer is trivial.
One more nuance: The problem might have a constraint that we cannot place dogs on cells that are adjacent to sheep? No, nothing says that.
Thus I'll implement as described.
But to be thorough, I'll think if any scenario could break the all-dogs approach. Consider a wolf that is adjacent to a sheep but separated by a dog? That can't happen because if they are adjacent, there is no cell in between. So no.
Thus final solution is just adjacency check + replace '.' with 'D'.
Now, we need to output "Yes"/"No" exactly as given (first letter capital). In the sample, they output "Yes" and "No". So we follow.
Edge Cases: Input lines may have newline characters. We'll handle accordingly.
Now, because the problem is simple, but I should consider if there is any possibility that placing dogs on all empty cells might inadvertently create a scenario where a wolf can move into a sheep because the dog is placed on a cell that was initially a path but after placing dogs, the wolf might be forced to move? No, forced moves are not a thing.
Thus, answer: Implementation straightforward.
Now, as ChatGPT, I need to provide an explanation and code. I'll produce a clear solution.
But wait, the problem says: "Output If it is impossible to protect all sheep, output a single line with the word "No". Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs." So we need to output "Yes" or "No". In the sample, they have "Yes" on its own line, then the grid. So we will output exactly that.
Thus code:
def solve():
import sys
input = sys.stdin.readline
R, C = map(int, input().split())
grid = [list(input().strip()) for _ in range(R)]
# Check for adjacent wolf-sheep
dirs = [(1,0),(-1,0),(0,1),(0,-1)]
for i in range(R):
for j in range(C):
if grid[i][j] == 'W':
for di, dj in dirs:
ni, nj = i+di, j+dj
if 0 <= ni < R and 0 <= nj < C:
if grid[ni][nj] == 'S':
print("No")
return
# If we get here, it's possible
print("Yes")
for i in range(R):
row = ''.join('D' if ch == '.' else ch for ch in grid[i])
print(row)
That's it.
But wait, what about the case where there is a sheep adjacent to a wolf? That is impossible. However, we should also consider that if we place dogs on all empty cells, could that make a wolf adjacent to a sheep? No, because adjacency is about positions; dogs don't change positions of W or S.
Thus final.
Now, I'd like to double-check with the sample inputs.
Sample 1:
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Our code will check adjacency: Let's list W positions: (2,5), (4,3), (5,4). Check neighbors:
(2,5): neighbors: (2,4) is '.'; (2,6) '.'; (1,5) '.'; (3,5) '.'; none are S. (4,3): neighbors: (4,2) '.'; (4,4) '.'; (3,3) '.'; (5,3) '.'; none S. (5,4): neighbors: (5,3) '.'; (5,5) '.'; (4,4) '.'; (6,4) '.'; none S. Also possible S neighbors of other cells? There might be S adjacent to each other? Not relevant. So no adjacency. So prints Yes. Then replace all '.' with D. That would produce grid:
Row1: ..S... -> ..SDD? Actually all '.' become D: so row1: '.' '.' 'S' '.' '.' '.' -> D D S D D D? Wait careful: each '.' becomes D. So original row1 string "..S..." has characters: index0 '.', index1 '.', index2 'S', index3 '.', index4 '.', index5 '.'. So after replacement: index0 D, index1 D, index2 S, index3 D, index4 D, index5 D => "DDSDDD"? But we must ensure we don't change S or W. So row1 becomes "DDSDDD". But the sample output row1 is "..SD..". Our output is different but still valid because it blocks all paths? Let's verify if "DDSDDD" still blocks. Wolf at (2,5) etc. However, note that we placed a dog at (1,1) and (1,2) etc. That's fine. But what about the fact that we placed dogs on all '.' cells, including those that are adjacent to each other? No problem.
But wait: There's a nuance: The problem says "You are not allowed to move, remove or add a sheep or a wolf." We are not doing that. So it's fine.
Thus the sample output is just one possible solution; our output is another.
But is there any scenario where placing dogs on all empty cells could cause a problem because we might be placing a dog on a cell that is adjacent to a wolf and also adjacent to a sheep? That is allowed. So okay.
Thus final answer.
Now, we need to produce a solution in a language; the user expects a ChatGPT answer. Typically they want a Python code.
I'll write the final code with proper reading and writing.
Potential pitfalls: Input might have spaces? No, rows are strings.
Also, if we replace '.' with 'D', we should output exactly R lines.
Now, verify with sample 2: R=1, C=2, grid "SW". Check: W at (0,1) (assuming 0-index). Neighbors: left (0,0) is 'S'. So adjacency found, print "No". Good.
Sample 3: No wolves, adjacency none, prints Yes and grid with '.' replaced by D. But sample output didn't replace all '.' with D; it placed one D. But our output would be different. But it's still valid per problem statement.
Thus final solution.
But let's ensure that our code doesn't accidentally output "Yes" and then grid that is invalid due to adjacency created by dogs? No.
Thus solution is ready.
But I recall that there is a known problem "16956" on BOJ where the typical solution is exactly this, and many people solved it by placing dogs on all '.' cells. I can search memory: In BOJ 16956, the solution is:
if any wolf next to sheep: print(0)
else:
print(1)
for i in range(n):
for j in range(m):
if a[i][j]=='.': print('D',end='')
else: print(a[i][j],end='')
print()
Yes, they printed 1 instead of "Yes", but here they want "Yes"/"No". So it's the same.
Thus final answer is as above.
Now I'll produce the final message with explanation and code. The problem reduces to checking if any wolf is directly adjacent to a sheep. If such an adjacency exists, it is impossible to protect the sheep because there is no empty cell to block the immediate move. Otherwise, we can simply place a dog on every empty cell. This ensures that wolves cannot move (all their possible move targets are either occupied by other wolves, sheep, or dogs), and since no wolf starts next to a sheep, they can never reach one.
Algorithm
Read the grid.
For each cell containing a wolf ('W'), examine its four neighbours. If any neighbour contains a sheep ('S'), output "No" and stop.
If no such adjacency is found, output "Yes" and then the modified grid where every '.' is replaced by 'D'. All other characters remain unchanged.
This solution runs in (O(RC)) time, well within the limits (R, C \le 500).
python
import sys
def solve() -> None:
input = sys.stdin.readline
R, C = map(int, input().split())
grid = [list(input().strip()) for _ in range(R)]
# Directions: up, down, left, right
dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for i in range(R):
for j in range(C):
if grid[i][j] == 'W':
for di, dj in dirs:
ni, nj = i + di, j + dj
if 0 <= ni < R and 0 <= nj < C and grid[ni][nj] == 'S':
print("No")
return
print("Yes")
for i in range(R):
# Replace every '.' with 'D', keep other characters
row = ''.join('D' if ch == '.' else ch for ch in grid[i])
print(row)
if __name__ == "__main__":
solve()