[Codeforces 1023.E]Down or Right(交互题)

E. Down or Right

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
This is an interactive problem.

Bob lives in a square grid of size $n×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≤r_1≤r_2≤n$, $1≤c_1≤c_2≤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)≥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≤n≤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.

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≤r_1≤r_2≤n$, $1≤c_1≤c_2≤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.

Example

input output
4
YES
NO
YES
YES
? 1 1 4 4
? 1 2 4 3
? 4 1 4 4
? 1 4 4 4
! RDRRDD

解题思路

从 $(1,1)$ 尽可能向下走,即询问? x y n n,到达对角线为止;
再从 $(n,n)$ 尽可能向左走,即询问? 1 1 x y,到达对角线为止。
这样,两次到达的对角线一定是同一个点。
询问次数为 $2\cdot n$。


Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<bits/stdc++.h>

using namespace std;

int n;
char ans[5];
vector<char> v1, v2;

inline bool ask(int r1, int c1, int r2, int c2){
printf("? %d %d %d %d\n", r1, c1, r2, c2);
fflush(stdout);
scanf("%s", ans);
return ans[0] == 'Y';
}

inline bool can(int r1, int c1, int r2, int c2){
return r1 >= 1 && r1 <= n && c1 >= 1 && c1 <= n && r2 >= 1 && r2 <= n && c2 >= 1 && c2 <= n && (r2 - r1) + (c2 - c1) >= n - 1;
}

int main(){
scanf("%d", &n);
int nowx = 1, nowy = 1;
while(nowx + nowy < n + 1){
if(nowx < n){
if(ask(nowx + 1, nowy, n, n)){
nowx++;
v1.push_back('D');
}
else{
nowy++;
v1.push_back('R');
}
}
else{
nowy++;
v1.push_back('R');
}
}
nowx = nowy = n;
while(nowx + nowy > n + 1){
if(nowy > 1){
if(ask(1, 1, nowx, nowy - 1)){
nowy--;
v2.push_back('R');
}
else{
nowx--;
v2.push_back('D');
}
}
else{
nowx--;
v2.push_back('D');
}
}
printf("! ");
for(int i = 0; i < v1.size(); i++)
putchar(v1[i]);
for(int i = v2.size() - 1; i >= 0; i--)
putchar(v2[i]);
putchar(10);
return 0;
}