QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#270401 | #7751. Palindrome Path | ucup-team859 | WA | 1ms | 10200kb | C++14 | 5.0kb | 2023-11-30 20:19:51 | 2023-11-30 20:19:52 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
using ll = long long;
using ull = unsigned long long;
string to_string(const string &s) {
return '"' + s + '"';
}
string to_string(bool b) {
return b ? "true" : "false";
}
template <typename A, typename B>
string to_string(const pair<A, B> &p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename T>
string to_string(const T &v) {
string s = "{";
bool first = true;
for (const auto &it : v) {
if (!first)
s += ", ";
else
first = false;
s += to_string(it);
}
return s += "}";
}
void debug_out() {
cerr << endl;
}
template <typename T, typename... Args>
void debug_out(const T &first, const Args&... rest) {
cerr << to_string(first) << " ";
debug_out(rest...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
auto startTime = high_resolution_clock::now();
int get_time() {
auto stopTime = chrono::high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stopTime - startTime);
return duration.count(); // in ms
}
struct usu {
int x, y, x2, y2;
};
int n, m;
usu tt[31][31][31][31];
int marked[31][31];
int viz[31][31][31][31];
int a[31][31];
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int dx2[] = {1, -1, 0, 0};
const int dy2[] = {0, 0, 1, -1};
const int rk[] = {1, 0, 3, 2};
const char ch[] = {'U', 'D', 'L', 'R'};
inline bool in_bounds(int i, int j) {
return i >= 1 && i <= n && j >= 1 && j <= m && a[i][j];
}
vector<int> ordk;
vector<int> dfp;
int nr = 0;
int nrv = 0;
void dfs(int x, int y) {
++nrv;
marked[x][y] = nr;
shuffle(ordk.begin(), ordk.end(), rng);
for (auto k : ordk) {
int i = dx[k] + x;
int j = dy[k] + y;
if (in_bounds(i, j) && marked[i][j] != nr) {
dfp.push_back(k);
dfs(i, j);
dfp.push_back(rk[k]);
}
}
}
void print_path(int x, int y, int x2, int y2) {
string ans;
while (viz[x][y][x2][y2] != -1) {
int k = viz[x][y][x2][y2] - 1;
ans.push_back(ch[k]);
auto tmp = tt[x][y][x2][y2];
x = tmp.x;
y = tmp.y;
x2 = tmp.x2;
y2 = tmp.y2;
}
reverse(ans.begin(), ans.end());
string str;
for (auto it : dfp)
str.push_back(ch[it]);
for (auto it = dfp.rbegin(); it != dfp.rend(); ++it)
str.push_back(ch[*it]);
string sol = ans + str;
reverse(ans.begin(), ans.end());
sol += ans;
for (auto it : sol)
cout << it;
cout << '\n';
}
void solve() {
ordk = {0, 1, 2, 3};
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
string s;
cin >> s;
for (int j = 1; j <= m; ++j) {
a[i][j] = s[j - 1] - '0';
}
}
int x, y, x2, y2;
cin >> x >> y >> x2 >> y2;
queue<usu> q;
viz[x][y][x2][y2] = -1;
q.push({x, y, x2, y2});
int sx = -1, sy = -1;
while (!q.empty()) {
x = q.front().x;
y = q.front().y;
x2 = q.front().x2;
y2 = q.front().y2;
q.pop();
for (int k = 0; k < 4; ++k) {
int i = dx[k] + x;
int j = dy[k] + y;
int i2 = dx2[k] + x2;
int j2 = dy2[k] + y2;
if (!in_bounds(i, j)) {
i -= dx[k];
j -= dy[k];
}
if (in_bounds(i2, j2)) {
if (!viz[i][j][i2][j2]) {
tt[i][j][i2][j2] = {x, y, x2, y2};
viz[i][j][i2][j2] = k + 1;
q.push({i, j, i2, j2});
}
}
i2 = dx[k] + x2;
j2 = dy[k] + y2;
if (!in_bounds(i2, j2)) {
i2 = x2;
j2 = y2;
if (!viz[i][j][i2][j2]) {
tt[i][j][i2][j2] = {x, y, x2, y2};
viz[i][j][i2][j2] = k + 1;
q.push({i, j, i2, j2});
}
}
}
}
int tot = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
tot += a[i][j];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (a[i][j] == 0)
continue;
for (int t = 0; t <= 300; ++t) {
++nr;
nrv = 0;
dfp.clear();
dfs(i, j);
if (nrv != tot)
break;
int i2 = i, j2 = j;
for (auto it : dfp) {
i2 += dx[it];
j2 += dy[it];
if (!in_bounds(i2, j2)) {
i2 -= dx[it];
j2 -= dy[it];
}
}
for (auto it = dfp.rbegin(); it != dfp.rend(); ++it) {
i2 += dx[*it];
j2 += dy[*it];
if (!in_bounds(i2, j2)) {
i2 -= dx[*it];
j2 -= dy[*it];
}
}
if (viz[i][j][i2][j2]) {
print_path(i, j, i2, j2);
return;
}
}
}
}
cout << "-1\n";
}
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7716kb
input:
2 2 11 11 1 1 2 2
output:
DRUDLUULDURD
result:
ok Valid Solution (Length = 12).
Test #2:
score: 0
Accepted
time: 1ms
memory: 5720kb
input:
2 2 10 01 1 1 2 2
output:
-1
result:
ok No Solution.
Test #3:
score: 0
Accepted
time: 1ms
memory: 5888kb
input:
1 1 1 1 1 1 1
output:
result:
ok Valid Solution (Length = 0).
Test #4:
score: 0
Accepted
time: 1ms
memory: 9816kb
input:
5 4 1111 1111 1111 1111 1111 4 2 4 2
output:
DDDUUUULLDDDDRUURRULURLLDURDRDDDLUDRUULLDDLUUUUUUUULDDLLUURDULDDDRDRUDLLRULURRUURDDDDLLUUUUDDD
result:
ok Valid Solution (Length = 94).
Test #5:
score: 0
Accepted
time: 1ms
memory: 7792kb
input:
5 5 11111 10101 11111 10101 11111 1 4 5 5
output:
DLLLURLDDRRDDRRUUUULLDURRDDLRDDLLLLUDRRUULLUURRUULLUURRDULLLLDDRLDDRRUDLLUUUURRDDRRDDLRULLLD
result:
ok Valid Solution (Length = 92).
Test #6:
score: 0
Accepted
time: 1ms
memory: 7808kb
input:
5 3 111 100 111 001 111 4 3 3 2
output:
URLLUURRLLDDRRDDLLRRUULLUUUULLUURRLLDDRRDDLLRRUULLRU
result:
ok Valid Solution (Length = 52).
Test #7:
score: 0
Accepted
time: 0ms
memory: 10052kb
input:
5 4 1001 1101 1111 0011 0010 2 2 1 1
output:
UDRRUULLULDDLLLUUDRLDRRDDURLURUUUURULRUDDRRDLRDUULLLDDLULLUURRDU
result:
ok Valid Solution (Length = 64).
Test #8:
score: 0
Accepted
time: 0ms
memory: 9844kb
input:
5 3 101 111 100 111 100 4 1 2 2
output:
RURULLDUUUUDRRUDLLDDRRRRDDLLDURRDUUUUDLLURUR
result:
ok Valid Solution (Length = 44).
Test #9:
score: 0
Accepted
time: 0ms
memory: 7896kb
input:
5 5 01110 10110 11110 11011 11100 2 4 5 1
output:
DLDUUDRUDDDRLULLLDDRRLUDLUUUDRRRULULRRLULURRRDUUULDULRRDDLLLULRDDDURDUUDLD
result:
ok Valid Solution (Length = 74).
Test #10:
score: 0
Accepted
time: 0ms
memory: 7812kb
input:
5 3 011 111 110 111 011 3 1 2 1
output:
ULLRURDLLDDRRDLRULUDLUURRULLURRUULDULURLDRRDDLLDRURLLU
result:
ok Valid Solution (Length = 54).
Test #11:
score: 0
Accepted
time: 1ms
memory: 7780kb
input:
4 5 11111 11111 11111 11111 3 2 1 3
output:
UUULLRDRRDLDLULUUDDRDLRRRRUUULLRRDDDLLURULLUULLURULLDDDRRLLUUURRRRLDRDDUULULDLDRRDRLLUUU
result:
ok Valid Solution (Length = 88).
Test #12:
score: 0
Accepted
time: 1ms
memory: 7908kb
input:
5 5 11111 10101 11111 10101 11111 2 5 1 1
output:
UUUULLLLDDRRDDRRUUUULLLRDURRDDLRDDLLUULLDDRLUUUUUUUULRDDLLUULLDDRLDDRRUDRLLLUUUURRDDRRDDLLLLUUUU
result:
ok Valid Solution (Length = 96).
Test #13:
score: 0
Accepted
time: 1ms
memory: 7816kb
input:
4 5 11111 10000 11111 00001 1 3 4 5
output:
DRRRRLLLLDDRRRRDULLLLUURRRRLLLLLLRRRRUULLLLUDRRRRDDLLLLRRRRD
result:
ok Valid Solution (Length = 60).
Test #14:
score: 0
Accepted
time: 1ms
memory: 5788kb
input:
3 5 10100 00010 00111 1 3 1 1
output:
-1
result:
ok No Solution.
Test #15:
score: 0
Accepted
time: 1ms
memory: 9840kb
input:
4 5 10001 11111 11100 11111 4 5 3 1
output:
DLLUULLUDDRURRRUDLLDDRRLLLLRRUULDLUUUULDLUURRLLLLRRDDLLDURRRURDDULLUULLD
result:
ok Valid Solution (Length = 72).
Test #16:
score: 0
Accepted
time: 1ms
memory: 7788kb
input:
3 5 11111 10100 11111 1 2 3 5
output:
RRDLDULLDDRRRRLLUULRRRLLDDLLUUUULLDDLLRRRLUULLRRRRDDLLUDLDRR
result:
ok Valid Solution (Length = 60).
Test #17:
score: 0
Accepted
time: 1ms
memory: 5932kb
input:
4 5 01110 10101 11011 10111 1 3 2 3
output:
-1
result:
ok No Solution.
Test #18:
score: 0
Accepted
time: 1ms
memory: 7816kb
input:
5 5 11111 11111 11111 11111 11111 1 3 5 2
output:
LLLRDRDLDRDLLUUUDDDRRRRUUUULDDDUUURDDDDLLULURUUDLULLULDUURULULLDDDDRUUUDDDLUUUURRRRDDDUUULLDRDLDRDRLLL
result:
ok Valid Solution (Length = 102).
Test #19:
score: 0
Accepted
time: 1ms
memory: 7808kb
input:
5 5 11111 10101 11111 10101 11111 5 1 2 3
output:
UUULULRRDDRRDDLLUDLLUURLUDDDRRRRUULLUURRDULLLLLLLLUDRRUULLUURRRRDDDULRUULLDULLDDRRDDRRLULUUU
result:
ok Valid Solution (Length = 92).
Test #20:
score: 0
Accepted
time: 1ms
memory: 7896kb
input:
5 5 11111 10000 11111 00001 11111 4 5 5 3
output:
ULLDLLDUURLDDRRRRDDLLLLRRRRUULLLLUURRRRLLLLLLRRRRUULLLLUURRRRLLLLDDRRRRDDLRUUDLLDLLU
result:
ok Valid Solution (Length = 84).
Test #21:
score: 0
Accepted
time: 1ms
memory: 9712kb
input:
5 5 01010 10101 10101 11001 10011 4 1 5 4
output:
-1
result:
ok No Solution.
Test #22:
score: 0
Accepted
time: 1ms
memory: 8092kb
input:
5 5 10101 11111 10101 11111 11111 3 1 2 4
output:
LUUUDRRUDRRDDLLLLDRRRRLLLLUUDRRUDRRUUUDLLLLUULLLLDUUURRDURRDUULLLLRRRRDLLLLDDRRDURRDUUUL
result:
ok Valid Solution (Length = 88).
Test #23:
score: 0
Accepted
time: 0ms
memory: 9880kb
input:
5 5 00001 11111 01110 01111 01111 1 5 5 2
output:
DLLDDDRURDULDLLUUULRDDDRUURLURRUURRULRUURDDDRLUUULLDLUDRURDDDLLD
result:
ok Valid Solution (Length = 64).
Test #24:
score: 0
Accepted
time: 0ms
memory: 7840kb
input:
5 5 01011 10111 11011 10101 01110 4 1 2 3
output:
-1
result:
ok No Solution.
Test #25:
score: 0
Accepted
time: 0ms
memory: 10200kb
input:
10 8 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 7 7 3 6
output:
UUUUUUURRRRRLLLLLLLDDRUURRDDDLLLDDRURRRDRUUURULLURRRDDDDLDRDDLULLLLURLDLDRDRRDLLLLURLUUDDDRRRRRRULULLRRDRDLLULLULURRRRRDRDDUUUULUUDRUUUULLLDRRDLLDURDDDLULLLDLUURRRULUDRUULLDDLUUUULDDLLUURDULURRRUULDLLLULDDDRUDLLDRRDLLLUUUURDUULUUUUDDRDRRRRRULULLULLDRDRRLLULURRRRRRDDDUULRULLLLDRRDRDLDLRULLLLULDDRDLDD...
result:
ok Valid Solution (Length = 354).
Test #26:
score: -100
Wrong Answer
time: 0ms
memory: 10076kb
input:
10 6 111111 101010 111111 101010 111111 101010 111111 101010 111111 101010 1 6 2 3
output:
-1
result:
wrong answer Solution Exists, But -1 Found.