题解:CF2155B Abraham's Great Escape

· · 题解

我们先让 k=n^2-k,即想办法让 k 个格子无法逃出迷宫。 显然当 k=1 时无解,因为 1 个格子无法形成死循环。 可以让这 k 个格子连在一起,互相形成死循环,且位于迷宫的最顶端,并让其他的格子均为 D,即

|||||| | :-: | :-: | :-: | :-: | :-: | | ? | ? | ? | ? | ? | | ? | ? | ? | ? | ? | | ? | ? | ? | D | D | | D | D | D | D | D |

其中 ? 是指还没有确定的字符。

我们考虑先对整行的进行操作,不难想到构造 RRRR...L 的序列,此时就会形成死循环。

现在迷宫变成了

|||||| | :-: | :-: | :-: | :-: | :-: | | R | R | R | R | L | | R | R | R | R | L | | ? | ? | ? | D | D | | D | D | D | D | D |

对于剩下的 ?,我们不能直接构造 RRRR...L 的序列,而是要特判只剩下 1 个点的情况,这时候填上 U 即可。\ 即

|||||| | :-----: | :-: | :-: | :-: | :-: | | R | R | R | R | L | | R | R | R | R | L | | U | D | D | D | D | | D | D | D | D | D |

#include<bits/stdc++.h>
#define pre(i, l, r)    for(int i = (l); i <= (r); ++i)
#define fpre(i, r, l)   for(int i = (r); i >= (l); --i)
#define fr(i, s)    for(int i = 0, _ = s.size(); i < _; ++i)
#define ll long long

using namespace std;

const int maxn = 2e6 + 5;
const int inf = 1e9;

template<typename T>inline void S(T& x){x = 0;int f = 0;char ch = getchar();while(!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while(isdigit(ch)) {x = x * 10 + (ch ^ 48), ch = getchar();}if(f) x = -x;}
template<typename T,typename...type>inline void S(T& x, type&...y)  {S(x);S(y...);}
template<typename T>inline void S(T* b, T* e)   {while(b != e)  S(*b++);}
 //上面是快读,不用管
int t;
int n, k;
char s[103][103];

int main()
{
    S(t);
    while(t--)
    {
        S(n, k);
        k = n * n - k;
        if(k == 1)//只有k=1时无解
        {
            puts("NO");
            continue;
        }
        puts("YES");
        pre(i, 1, n)    pre(j, 1, n)    s[i][j] = 'D';
        int x = k / n;
        pre(i, 1, x)
        {
            pre(j, 1, n - 1)
                s[i][j] = 'R';
            s[i][n] = 'L';
        }
        x = k % n;
        if(x == 1)
            s[k / n + 1][1] = 'U';//特判
        else
        {
            pre(i, 1, x - 1)    s[k / n + 1][i] = 'R';
            s[k / n + 1][x] = 'L';
        }
        pre(i, 1, n)
            pre(j, 1, n + 1)
                putchar((s[i][j] + (string)"\n")[j == n + 1]);//输出矩阵
    }
    return 0;
}