QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#270404 | #7751. Palindrome Path | ucup-team859 | WA | 1ms | 9864kb | C++14 | 4.8kb | 2023-11-30 20:22:29 | 2023-11-30 20:22:30 |
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> dfp;
int nr = 0;
int nrv = 0;
void dfs(int x, int y) {
vector<int> ordk;
ordk = {0, 1, 2, 3};
++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() {
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});
}
}
}
}
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;
dfp.clear();
dfs(i, j);
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: 0ms
memory: 7812kb
input:
2 2 11 11 1 1 2 2
output:
DRUDLUULDURD
result:
ok Valid Solution (Length = 12).
Test #2:
score: 0
Accepted
time: 0ms
memory: 5684kb
input:
2 2 10 01 1 1 2 2
output:
-1
result:
ok No Solution.
Test #3:
score: 0
Accepted
time: 1ms
memory: 5948kb
input:
1 1 1 1 1 1 1
output:
result:
ok Valid Solution (Length = 0).
Test #4:
score: 0
Accepted
time: 1ms
memory: 7824kb
input:
5 4 1111 1111 1111 1111 1111 4 2 4 2
output:
DDDUUUULLDRDDDRURULURULLRRDLDRDDULDLLUUDDRUUULUULUUURDDUULLDLUDDRDLDRRLLURULURURDDDRDLLUUUUDDD
result:
ok Valid Solution (Length = 94).
Test #5:
score: 0
Accepted
time: 1ms
memory: 7912kb
input:
5 5 11111 10101 11111 10101 11111 1 4 5 5
output:
DLLLURRDDLLUDDDRRUDRRUULRUULRDDDDLLLLUURRUULLLLUURRUULLLLDDDDRLUURLUURRDURRDDDULLDDRRULLLD
result:
ok Valid Solution (Length = 90).
Test #6:
score: 0
Accepted
time: 1ms
memory: 9828kb
input:
5 3 111 100 111 001 111 4 3 3 2
output:
URLLUUDDRRDDLLRRUULLUURRLLLLRRUULLUURRLLDDRRDDUULLRU
result:
ok Valid Solution (Length = 52).
Test #7:
score: 0
Accepted
time: 1ms
memory: 7868kb
input:
5 4 1001 1101 1111 0011 0010 2 2 1 1
output:
ULUDDRUDRDDURUUUDDDLULLUUUULLULDDDUUURUDDRDURDDULU
result:
ok Valid Solution (Length = 50).
Test #8:
score: 0
Accepted
time: 1ms
memory: 7800kb
input:
5 3 101 111 100 111 100 4 1 2 2
output:
URUUUDDDDURRLLUURRUDLLUULLDURRUULLRRUDDDDUUURU
result:
ok Valid Solution (Length = 46).
Test #9:
score: 0
Accepted
time: 1ms
memory: 7772kb
input:
5 5 01110 10110 11110 11011 11100 2 4 5 1
output:
DLDUULRRDDDRLULUDLLUDDRDLRRLULURRRUULLLLUURRRULULRRLDRDDULLDULULRDDDRRLUUDLD
result:
ok Valid Solution (Length = 76).
Test #10:
score: 0
Accepted
time: 1ms
memory: 9796kb
input:
5 3 011 111 110 111 011 3 1 2 1
output:
ULRURDLLDRDLRRDLRULULURRULLURRULULURLDRRLDRDLLDRURLU
result:
ok Valid Solution (Length = 52).
Test #11:
score: 0
Accepted
time: 1ms
memory: 7876kb
input:
4 5 11111 11111 11111 11111 3 2 1 3
output:
UUULLRRRRDLDDLULURLLDDRLUURDRDRRUDLUURULLLLLLLLURUULDURRDRDRUULRDDLLRULULDDLDRRRRLLUUU
result:
ok Valid Solution (Length = 86).
Test #12:
score: 0
Accepted
time: 1ms
memory: 9864kb
input:
5 5 11111 10101 11111 10101 11111 2 5 1 1
output:
UUUULLLLRRDDRRDDLLLLUUUDRLDDRRUDRRUUUULRDDLLUULLLLUULLDDRLUUUURRDURRDDLRDUUULLLLDDRRDDRRLLLLUUUU
result:
ok Valid Solution (Length = 96).
Test #13:
score: 0
Accepted
time: 1ms
memory: 5828kb
input:
4 5 11111 10000 11111 00001 1 3 4 5
output:
DRRRRLLLLDDRRRRDULLLLUURRRRLLLLLLLLRRRRUULLLLUDRRRRDDLLLLRRRRD
result:
ok Valid Solution (Length = 62).
Test #14:
score: 0
Accepted
time: 1ms
memory: 5784kb
input:
3 5 10100 00010 00111 1 3 1 1
output:
-1
result:
ok No Solution.
Test #15:
score: 0
Accepted
time: 1ms
memory: 7796kb
input:
4 5 10001 11111 11100 11111 4 5 3 1
output:
DLLLUULUDRRDDLULDURDRRRLLUURRUDLLLLUULLLLDURRUULLRRRDRUDLULDDRRDULUULLLD
result:
ok Valid Solution (Length = 72).
Test #16:
score: 0
Accepted
time: 1ms
memory: 5764kb
input:
3 5 11111 10100 11111 1 2 3 5
output:
RRDLULLRRRRLLDDLLUDRRRRLLUULLLLUULLRRRRDULLDDLLRRRRLLULDRR
result:
ok Valid Solution (Length = 58).
Test #17:
score: -100
Wrong Answer
time: 1ms
memory: 5928kb
input:
4 5 01110 10101 11011 10111 1 3 2 3
output:
LRDURLLLLRUDRL
result:
wrong answer (2,1) Not Visited