A. Protect Sheep

Codeforces
IDCF948A
Time1000ms
Memory256MB
Difficulty
brute forcedfs and similargraphsimplementation
English · Original
Chinese · Translation
Formal · Original
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. [samples] ## 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.
Bob 是一名农民。他有一个广阔的牧场,养了许多羊。最近,由于狼的袭击,他丢失了一些羊。因此,他决定放置一些牧羊犬,以保护所有羊。 牧场是一个由 #cf_span[R × C] 个单元格组成的矩形。每个单元格要么为空,要么包含一只羊、一只狼或一条狗。羊和狗始终停留在原地,但狼可以自由地在牧场中游荡,通过反复向左、右、上或下移动到相邻的单元格。当狼进入一只羊所在的单元格时,它会吃掉这只羊。然而,任何狼都不能进入有狗的单元格。 最初没有狗。请在牧场上放置狗,使得没有任何狼能到达任何羊,或者判断这是不可能的。注意,由于你拥有许多狗,你*不需要*最小化狗的数量。 第一行包含两个整数 #cf_span[R] (#cf_span[1 ≤ R ≤ 500]) 和 #cf_span[C] (#cf_span[1 ≤ C ≤ 500]),分别表示行数和列数。 接下来的 #cf_span[R] 行每行是一个由恰好 #cf_span[C] 个字符组成的字符串,表示牧场的一行。其中,'_S_' 表示一只羊,'_W_' 表示一只狼,'_._' 表示空单元格。 如果无法保护所有羊,请输出一行单词 "_No_"。 否则,输出一行单词 "_Yes_"。然后输出 #cf_span[R] 行,表示放置狗之后的牧场。再次说明,'_S_' 表示羊,'_W_' 表示狼,'_D_' 表示狗,'_._' 表示空地。你不允许移动、移除或添加任何羊或狼。 如果有多种解决方案,你可以输出其中任意一种。你不需要最小化狗的数量。 在第一个例子中,我们可以将牧场分成两部分,一部分包含狼,另一部分包含羊。注意,位置 (2,1) 的羊是安全的,因为狼不能对角线移动。 在第二个例子中,没有空位可以放置狗来守护那只唯一的羊。 在第三个例子中,没有狼,因此任务非常简单。我们在中心放置了一条狗,以观察牧场的宁静,但即使不放狗,解也是正确的。 ## Input 第一行包含两个整数 #cf_span[R] (#cf_span[1 ≤ R ≤ 500]) 和 #cf_span[C] (#cf_span[1 ≤ C ≤ 500]),分别表示行数和列数。接下来的 #cf_span[R] 行每行是一个由恰好 #cf_span[C] 个字符组成的字符串,表示牧场的一行。其中,'_S_' 表示一只羊,'_W_' 表示一只狼,'_._' 表示空单元格。 ## Output 如果无法保护所有羊,请输出一行单词 "_No_"。否则,输出一行单词 "_Yes_"。然后输出 #cf_span[R] 行,表示放置狗之后的牧场。再次说明,'_S_' 表示羊,'_W_' 表示狼,'_D_' 表示狗,'_._' 表示空地。你不允许移动、移除或添加任何羊或狼。如果有多种解决方案,你可以输出其中任意一种。你不需要最小化狗的数量。 [samples] ## Note 在第一个例子中,我们可以将牧场分成两部分,一部分包含狼,另一部分包含羊。注意,位置 (2,1) 的羊是安全的,因为狼不能对角线移动。在第二个例子中,没有空位可以放置狗来守护那只唯一的羊。在第三个例子中,没有狼,因此任务非常简单。我们在中心放置了一条狗,以观察牧场的宁静,但即使不放狗,解也是正确的。
**Definitions** Let $ R, C \in \mathbb{Z} $ be the dimensions of the grid, with $ 1 \leq R, C \leq 500 $. Let $ G \in \{ \text{S}, \text{W}, \text{.} \}^{R \times C} $ be the initial grid, where: - $ \text{S} $ denotes a sheep, - $ \text{W} $ denotes a wolf, - $ \text{.} $ denotes an empty cell. Let $ D \subseteq \{ (i,j) \in [R] \times [C] \mid G[i][j] = \text{.} \} $ be the set of positions where dogs may be placed. **Constraints** 1. Wolves can move in four directions (up, down, left, right) to adjacent cells. 2. A wolf can reach a sheep if and only if there exists a path from the wolf’s cell to the sheep’s cell through empty cells (and no dog). 3. Dogs block wolf movement; no wolf may enter a cell containing a dog. 4. Sheep and wolves cannot be moved, removed, or added. 5. Dogs may only be placed on initially empty cells ($ \text{.} $). **Objective** Determine whether there exists a set $ D $ such that no wolf can reach any sheep via any path of adjacent empty cells. - If such a $ D $ exists, output "Yes" and the grid with dogs placed on positions in $ D $. - Otherwise, output "No".
Samples
Input #1
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output #1
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input #2
1 2
SW
Output #2
No
Input #3
5 5
.S...
...S.
S....
...S.
.S...
Output #3
Yes
.S...
...S.
S.D..
...S.
.S...
API Response (JSON)
{
  "problem": {
    "name": "A. Protect Sheep",
    "description": {
      "content": "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 protec",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF948A"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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 protec...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Bob 是一名农民。他有一个广阔的牧场,养了许多羊。最近,由于狼的袭击,他丢失了一些羊。因此,他决定放置一些牧羊犬,以保护所有羊。\n\n牧场是一个由 #cf_span[R × C] 个单元格组成的矩形。每个单元格要么为空,要么包含一只羊、一只狼或一条狗。羊和狗始终停留在原地,但狼可以自由地在牧场中游荡,通过反复向左、右、上或下移动到相邻的单元格。当狼进入一只羊所在的单元格时,它会吃掉这只羊。然而,任...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ R, C \\in \\mathbb{Z} $ be the dimensions of the grid, with $ 1 \\leq R, C \\leq 500 $.  \nLet $ G \\in \\{ \\text{S}, \\text{W}, \\text{.} \\}^{R \\times C} $ be the initial grid, where: ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments