-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode2155B.cpp
More file actions
85 lines (73 loc) · 1.57 KB
/
Copy pathcode2155B.cpp
File metadata and controls
85 lines (73 loc) · 1.57 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Contest : Codeforces
* Problem : 2155B - Abraham's Great Escape
* Link : https://codeforces.com/problemset/problem/2155/B
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fastio ios::sync_with_stdio(0); cin.tie(0);
vector<vector<bool>> escape;
int n, k;
unordered_map<char, pair<int, int>> dir = {
{'U', {-1, 0}},
{'D', {1, 0}},
{'R', {0, 1}},
{'L', {0, -1}},
};
bool validPos(int i, int j) {
if (i >= 0 && i < n && j >= 0 && j < n) return true;
return false;
}
void solve() {
cin >> n >> k;
if (n * n - 1 == k) {
cout << "NO\n";
return;
}
else if (n * n == k) {
cout << "YES\n";
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) { cout << "U"; }
cout << '\n';
}
return;
}
cout << "YES\n";
escape.assign(n, vector<bool>(n, false));
int cnt = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (auto &[c, d] : dir) {
int x = i + d.first,
y = j + d.second;
if (cnt < k) {
if (x < 0 || x >= n || y < 0 || y >= n) {
cout << c;
escape[i][j] = true;
cnt++;
}
else if (validPos(x, y) && escape[x][y]) {
cout << c;
escape[i][j] = true;
cnt++;
}
break;
}
else {
if (validPos(x, y) && !escape[x][y]) {
cout << c;
break;
}
}
}
}
cout << '\n';
}
}
int main() {
fastio
int tc; cin >> tc;
while (tc--) solve();
return 0;
}