E. Two Labyrinths

Codeforces
IDCF10018E
Time2000ms
Memory256MB
Difficulty
English · Original
Formal · Original
A labyrinth is the rectangular grid, each of the cells of which is either free or wall, and it's possible to move only between free cells sharing a side. Constantine and Mike are the world leaders of composing the labyrinths. Each of them has just composed one labyrinth of size n × m, and now they are blaming each other for the plagiarism. They consider that the plagiarism takes place if there exists such a path from the upper-left cell to the lower-right cell that is the shortest for both labyrinths. Resolve their conflict and say if the plagiarism took place. In the first line two integers n and m (1 ≤ n, m ≤ 500) are written — the height and the width of the labyrinths. In the next n lines the labyrinth composed by Constantine is written. Each of these n lines consists of m characters. Each character is equal either to «_#_», which denotes a wall, or to «_._», which denotes a free cell. The next line is empty, and in the next n lines the labyrinth composed by Mike is written in the same format. It is guaranteed that the upper-left and the lower-right cells of both labyrinths are free. Output «_YES_» if there exists such a path from the upper-left to the lower-right cell that is the shortest for both labyrinths. Otherwise output «_NO_». ## Input In the first line two integers n and m (1 ≤ n, m ≤ 500) are written — the height and the width of the labyrinths.In the next n lines the labyrinth composed by Constantine is written. Each of these n lines consists of m characters. Each character is equal either to «_#_», which denotes a wall, or to «_._», which denotes a free cell.The next line is empty, and in the next n lines the labyrinth composed by Mike is written in the same format. It is guaranteed that the upper-left and the lower-right cells of both labyrinths are free. ## Output Output «_YES_» if there exists such a path from the upper-left to the lower-right cell that is the shortest for both labyrinths. Otherwise output «_NO_». [samples]
**Definitions** Let $ n, m \in \mathbb{Z} $ with $ 1 \leq n, m \leq 500 $. Let $ C = (c_{i,j})_{1 \leq i \leq n, 1 \leq j \leq m} \in \{ \text{`.`}, \text{`#'} \}^{n \times m} $ be Constantine’s labyrinth. Let $ M = (m_{i,j})_{1 \leq i \leq n, 1 \leq j \leq m} \in \{ \text{`.`}, \text{`#'} \}^{n \times m} $ be Mike’s labyrinth. Assume $ c_{1,1} = c_{n,m} = \text{`.`} $ and $ m_{1,1} = m_{n,m} = \text{`.`} $. Let $ G_C = (V_C, E_C) $ be the grid graph of Constantine’s labyrinth: - $ V_C = \{ (i,j) \mid c_{i,j} = \text{`.`} \} $, - $ E_C = \{ ((i,j), (i',j')) \mid |i - i'| + |j - j'| = 1 \} $. Let $ G_M = (V_M, E_M) $ be the grid graph of Mike’s labyrinth, defined analogously. Let $ d_C(u,v) $ denote the shortest path distance between nodes $ u, v \in V_C $ in $ G_C $, and $ d_M(u,v) $ analogously in $ G_M $. Let $ s = (1,1) $, $ t = (n,m) $. **Constraints** 1. $ s, t \in V_C \cap V_M $. 2. Both $ G_C $ and $ G_M $ are connected graphs with $ s, t \in V_C $ and $ s, t \in V_M $. **Objective** Determine whether: $$ \exists \, P \text{ (path from } s \text{ to } t \text{)} \quad \text{such that} \quad |P| = d_C(s,t) = d_M(s,t) $$ where $ |P| $ is the number of edges in path $ P $, and $ P $ is valid in both labyrinths (i.e., all cells of $ P $ are free in both $ C $ and $ M $). Equivalently: Is the set of shortest paths from $ s $ to $ t $ in $ G_C $ intersecting the set of shortest paths from $ s $ to $ t $ in $ G_M $, under the constraint that all cells along the path are free in both labyrinths? Output "YES" if such a path exists, "NO" otherwise.
API Response (JSON)
{
  "problem": {
    "name": "E. Two Labyrinths",
    "description": {
      "content": "A labyrinth is the rectangular grid, each of the cells of which is either free or wall, and it's possible to move only between free cells sharing a side. Constantine and Mike are the world leaders of",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10018E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "A labyrinth is the rectangular grid, each of the cells of which is either free or wall, and it's possible to move only between free cells sharing a side.\n\nConstantine and Mike are the world leaders of...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, m \\in \\mathbb{Z} $ with $ 1 \\leq n, m \\leq 500 $.  \nLet $ C = (c_{i,j})_{1 \\leq i \\leq n, 1 \\leq j \\leq m} \\in \\{ \\text{`.`}, \\text{`#'} \\}^{n \\times m} $ be Constantine’s l...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments