E. Down or Right

Codeforces
IDCF1023E
Time1000ms
Memory256MB
Difficulty
constructive algorithmsinteractivematrices
English · Original
Chinese · Translation
Formal · Original
_This is an interactive problem._ Bob lives in a square grid of size $n \times n$, with rows numbered $1$ through $n$ from top to bottom, and columns numbered $1$ through $n$ from left to right. Every cell is either allowed or blocked, but you don't know the exact description of the grid. You are given only an integer $n$. Bob can move through allowed cells but only in some limited directions. When Bob is in an allowed cell in the grid, he can move **down or right** to an adjacent cell, if it is allowed. You can ask at most $4 \cdot n$ queries of form "_?_ $r_1$ $c_1$ $r_2$ $c_2$" ($1 \le r_1 \le r_2 \le n$, $1 \le c_1 \le c_2 \le n$). The answer will be "_YES_" if Bob can get from a cell $(r_1, c_1)$ to a cell $(r_2, c_2)$, and "_NO_" otherwise. In particular, if one of the two cells (or both) is a blocked cell then the answer is "_NO_" for sure. Since Bob doesn't like short trips, you can only ask queries with the manhattan distance between the two cells at least $n - 1$, i.e. the following condition must be satisfied: $(r_2 - r_1) + (c_2 - c_1) \ge n - 1$. It's guaranteed that Bob can get from the top-left corner $(1, 1)$ to the bottom-right corner $(n, n)$ and your task is to find a way to do it. You should print the answer in form "_! S_" where $S$ is a string of length $2 \cdot n - 2$ consisting of characters '_D_' and '_R_', denoting moves down and right respectively. The down move increases the first coordinate by $1$, the right move increases the second coordinate by $1$. If there are multiple solutions, any of them will be accepted. You should terminate immediately after printing the solution. ## Input The only line of the input contains an integer $n$ ($2 \le n \le 500$) — the size of the grid. ## Output When you are ready to print the answer, print a single line containing "_! S_" where where $S$ is a string of length $2 \cdot n - 2$ consisting of characters '_D_' and '_R_', denoting moves down and right respectively. The path should be a valid path going from the cell $(1, 1)$ to the cell $(n, n)$ passing only through allowed cells. [samples] ## Interaction You can ask at most $4 \cdot n$ queries. To ask a query, print a line containing "_?_ $r_1$ $c_1$ $r_2$ $c_2$" ($1 \le r_1 \le r_2 \le n$, $1 \le c_1 \le c_2 \le n$). After that you should read a single line containing "_YES_" or "_NO_" depending on the answer of the query. "_YES_" means Bob can go from the cell $(r_1, c_1)$ to the cell $(r_2, c_2)$, "_NO_" means the opposite. Note that the grid is fixed before the start of your solution and does not depend on your queries. After printing a query do not forget to output end of line and flush the output. Otherwise you will get _Idleness limit exceeded_ or other negative verdict. To do this, use: * _fflush(stdout)_ or _cout.flush()_ in C++; * _System.out.flush()_ in Java; * _flush(output)_ in Pascal; * _stdout.flush()_ in Python; * see documentation for other languages. Answer "_BAD_" instead of "_YES_" or "_NO_" means that you made an invalid query. Exit immediately after receiving "_BAD_" and you will see _Wrong answer_ verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream. ## Note The first example is shown on the picture below. <center>![image](https://espresso.codeforces.com/4bcdeab2ec8d0a3c5e6d71bf5a192d3236646d15.png)</center>To hack, use the following input format: The first line should contain a single integer $n$ ($2 \le n \le 500$) — the size of the grid. Each of the next $n$ lines should contain a string of $n$ characters '_#_' or '_._', where '_#_' denotes a blocked cell, and '_._' denotes an allowed cell. For example, the following text encodes the example shown above: 4 ..#. #... ###. ....
_This is an interactive problem._ Bob lives in a square grid of size $n \times n$, with rows numbered $1$ through $n$ from top to bottom, and columns numbered $1$ through $n$ from left to right. Every cell is either allowed or blocked, but you don't know the exact description of the grid. You are given only an integer $n$. Bob can move through allowed cells but only in some limited directions. When Bob is in an allowed cell in the grid, he can move *down or right* to an adjacent cell, if it is allowed. You can ask at most $4 \cdot n$ queries of form "_? _$r_1$ $c_1$ $r_2$ $c_2$" ($1 \le r_1 \le r_2 \le n$, $1 \le c_1 \le c_2 \le n$). The answer will be "_YES_" if Bob can get from a cell $(r_1, c_1)$ to a cell $(r_2, c_2)$, and "_NO_" otherwise. In particular, if one of the two cells (or both) is a blocked cell then the answer is "_NO_" for sure. Since Bob doesn't like short trips, you can only ask queries with the manhattan distance between the two cells at least $n -1$, i.e. the following condition must be satisfied: $(r_2 -r_1) + (c_2 -c_1) \ge n -1$. It's guaranteed that Bob can get from the top-left corner $(1, 1)$ to the bottom-right corner $(n, n)$ and your task is to find a way to do it. You should print the answer in form "_! S_" where $S$ is a string of length $2 \cdot n -2$ consisting of characters '_D_' and '_R_', denoting moves down and right respectively. The down move increases the first coordinate by $1$, the right move increases the second coordinate by $1$. If there are multiple solutions, any of them will be accepted. You should terminate immediately after printing the solution. The only line of the input contains an integer $n$ ($2 \le n \le 500$) — the size of the grid. When you are ready to print the answer, print a single line containing "_! S_" where where $S$ is a string of length $2 \cdot n -2$ consisting of characters '_D_' and '_R_', denoting moves down and right respectively. The path should be a valid path going from the cell $(1, 1)$ to the cell $(n, n)$ passing only through allowed cells. You can ask at most $4 \cdot n$ queries. To ask a query, print a line containing "_? _$r_1$ $c_1$ $r_2$ $c_2$" ($1 \le r_1 \le r_2 \le n$, $1 \le c_1 \le c_2 \le n$). After that you should read a single line containing "_YES_" or "_NO_" depending on the answer of the query. "_YES_" means Bob can go from the cell $(r_1, c_1)$ to the cell $(r_2, c_2)$, "_NO_" means the opposite. Note that the grid is fixed before the start of your solution and does not depend on your queries. After printing a query do not forget to output end of line and flush the output. Otherwise you will get _Idleness limit exceeded_ or other negative verdict. To do this, use: Answer "_BAD_" instead of "_YES_" or "_NO_" means that you made an invalid query. Exit immediately after receiving "_BAD_" and you will see _Wrong answer_ verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream. The first example is shown on the picture below. To hack, use the following input format: The first line should contain a single integer $n$ ($2 \le n \le 500$) — the size of the grid. Each of the next $n$ lines should contain a string of $n$ characters '_#_' or '_._', where '_#_' denotes a blocked cell, and '_._' denotes an allowed cell. For example, the following text encodes the example shown above: ## Input The only line of the input contains an integer $n$ ($2 \le n \le 500$) — the size of the grid. ## Output When you are ready to print the answer, print a single line containing "_! S_" where where $S$ is a string of length $2 \cdot n -2$ consisting of characters '_D_' and '_R_', denoting moves down and right respectively. The path should be a valid path going from the cell $(1, 1)$ to the cell $(n, n)$ passing only through allowed cells. [samples] ## Interaction You can ask at most $4 \cdot n$ queries. To ask a query, print a line containing "_? _$r_1$ $c_1$ $r_2$ $c_2$" ($1 \le r_1 \le r_2 \le n$, $1 \le c_1 \le c_2 \le n$). After that you should read a single line containing "_YES_" or "_NO_" depending on the answer of the query. "_YES_" means Bob can go from the cell $(r_1, c_1)$ to the cell $(r_2, c_2)$, "_NO_" means the opposite.Note that the grid is fixed before the start of your solution and does not depend on your queries.After printing a query do not forget to output end of line and flush the output. Otherwise you will get _Idleness limit exceeded_ or other negative verdict. To do this, use: _fflush(stdout)_ or _cout.flush()_ in C++; _System.out.flush()_ in Java; _flush(output)_ in Pascal; _stdout.flush()_ in Python; see documentation for other languages. Answer "_BAD_" instead of "_YES_" or "_NO_" means that you made an invalid query. Exit immediately after receiving "_BAD_" and you will see _Wrong answer_ verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream. ## Note The first example is shown on the picture below. To hack, use the following input format:The first line should contain a single integer $n$ ($2 \le n \le 500$) — the size of the grid.Each of the next $n$ lines should contain a string of $n$ characters '_#_' or '_._', where '_#_' denotes a blocked cell, and '_._' denotes an allowed cell.For example, the following text encodes the example shown above:4..#.#...###.....
**Definitions** Let $ n \in \mathbb{Z} $ with $ 2 \leq n \leq 500 $ be the grid size. Let $ G = (V, E) $ be a directed grid graph where: - $ V = \{ (i,j) \mid 1 \leq i,j \leq n \} $, - An edge $ (i,j) \to (i+1,j) $ (down) exists iff cell $ (i,j) $ and $ (i+1,j) $ are both allowed, - An edge $ (i,j) \to (i,j+1) $ (right) exists iff cell $ (i,j) $ and $ (i,j+1) $ are both allowed. Let $ s = (1,1) $, $ t = (n,n) $. It is given that $ t $ is reachable from $ s $ via a path consisting only of down and right moves through allowed cells. Let $ \mathcal{Q} $ be the set of valid queries: For $ (r_1, c_1), (r_2, c_2) \in V $ with $ r_1 \leq r_2 $, $ c_1 \leq c_2 $, and $ (r_2 - r_1) + (c_2 - c_1) \geq n - 1 $, a query $ ?\ r_1\ c_1\ r_2\ c_2 $ returns $ \text{YES} $ if there exists a path from $ (r_1, c_1) $ to $ (r_2, c_2) $ using only down/right moves through allowed cells, and $ \text{NO} $ otherwise. If either $ (r_1, c_1) $ or $ (r_2, c_2) $ is blocked, the answer is $ \text{NO} $. **Constraints** 1. At most $ 4n $ queries may be asked. 2. Each query must satisfy $ (r_2 - r_1) + (c_2 - c_1) \geq n - 1 $. 3. The grid is fixed and adversarial, but $ s \to t $ is guaranteed to be possible. **Objective** Find a path $ P = (v_0, v_1, \dots, v_{2n-2}) $ from $ s = (1,1) $ to $ t = (n,n) $, where each $ v_i = (r_i, c_i) $, $ v_{i+1} $ is either $ (r_i+1, c_i) $ or $ (r_i, c_i+1) $, and all intermediate cells are allowed. Output the sequence of moves as a string $ S \in \{ \text{D}, \text{R} \}^{2n-2} $, where D denotes a down move and R denotes a right move. **Strategy (implicit)** Use binary search-like queries along diagonals to reconstruct the path incrementally, leveraging the constraint that Manhattan distance ≥ $ n-1 $ to probe reachability across large segments, and reconstruct the exact path by iteratively determining the next move (D or R) at each step using at most 4 queries per row/column.
Samples
Input #1
4
 
YES
 
NO
 
YES
 
YES
Output #1
? 1 1 4 4
 
? 1 2 4 3
 
? 4 1 4 4
 
? 1 4 4 4
 
! RDRRDD
API Response (JSON)
{
  "problem": {
    "name": "E. Down or Right",
    "description": {
      "content": "_This is an interactive problem._ Bob lives in a square grid of size $n \\times n$, with rows numbered $1$ through $n$ from top to bottom, and columns numbered $1$ through $n$ from left to right. Ever",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF1023E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "_This is an interactive problem._\n\nBob lives in a square grid of size $n \\times n$, with rows numbered $1$ through $n$ from top to bottom, and columns numbered $1$ through $n$ from left to right. Ever...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "_This is an interactive problem._\n\nBob lives in a square grid of size $n \\times n$, with rows numbered $1$ through $n$ from top to bottom, and columns numbered $1$ through $n$ from left to right. Ever...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $ with $ 2 \\leq n \\leq 500 $ be the grid size.  \nLet $ G = (V, E) $ be a directed grid graph where:  \n- $ V = \\{ (i,j) \\mid 1 \\leq i,j \\leq n \\} $,  \n- An edge...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments