QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#310517 | #7751. Palindrome Path | A_programmer | WA | 0ms | 3544kb | C++17 | 2.3kb | 2024-01-21 15:06:31 | 2024-01-21 15:06:31 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int n, m, sx, sy, ex, ey, cnt;
bool a[35][35], vis[35][35];
int lst[35][35];
int dx[4] = {0, 1, -1, 0};
int dy[4] = {1, 0, 0, -1};
char ans[1000005];
queue<int> q;
void dfs(int x, int y)
{
vis[x][y] = 1;
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i], ny = y + dy[i];
if (a[nx][ny] || vis[nx][ny]) continue;
q.push(i);
lst[nx][ny] = 3 - i;
dfs(nx, ny);
q.push(3 - i);
}
}
void find(int x, int y)
{
if (x == sx && y == sy) return;
find(x + dx[lst[x][y]], y + dy[lst[x][y]]);
q.push(3 - lst[x][y]);
}
void work(int x, int y, int op)
{
int nx = ex, ny = ey, nw = 0;
if (op == 0)
{
while (!a[x - 1][y] && !a[nx + 1][ny]) x--, nx++, ans[++cnt] = 'L', nw++;
if (a[nx + 1][ny]) ans[++cnt] = 'R';
else ans[++cnt] = 'L', ans[++cnt] = 'R';
for (int i = 1; i <= nw; i++) ans[++cnt] = 'R';
}
else if (op == 1)
{
while (!a[x][y - 1] && !a[nx][ny + 1]) y--, ny++, ans[++cnt] = 'U', nw++;
if (a[nx][ny + 1]) ans[++cnt] = 'D';
else ans[++cnt] = 'U', ans[++cnt] = 'D';
for (int i = 1; i <= nw; i++) ans[++cnt] = 'D';
}
else if (op == 2)
{
while (!a[x][y + 1] && !a[nx][ny - 1]) y++, ny--, ans[++cnt] = 'D', nw++;
if (a[nx][ny - 1]) ans[++cnt] = 'U';
else ans[++cnt] = 'D', ans[++cnt] = 'U';
for (int i = 1; i <= nw; i++) ans[++cnt] = 'U';
}
else
{
while (!a[x + 1][y] && !a[nx - 1][ny]) x++, nx--, ans[++cnt] = 'R', nw++;
if (a[nx - 1][ny]) ans[++cnt] = 'L';
else ans[++cnt] = 'R', ans[++cnt] = 'L';
for (int i = 1; i <= nw; i++) ans[++cnt] = 'L';
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
char s[35];
cin >> s + 1;
for (int j = 1; j <= m; j++) a[i][j] = s[j] ^ '1';
}
for (int i = 0; i <= n + 1; i++) a[i][0] = a[i][m + 1] = 1;
for (int i = 0; i <= m + 1; i++) a[0][i] = a[n + 1][i] = 1;
cin >> sx >> sy >> ex >> ey;
dfs(sx, sy);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (!a[i][j] && !vis[i][j])
{
cout << "-1";
return 0;
}
find(ex, ey);
while (q.size())
{
work(sx, sy, q.front());
sx += dx[q.front()], sy += dy[q.front()];
q.pop();
}
cout << "\n";
for (int i = 1; i <= cnt; i++) cout << ans[i];
for (int i = cnt; i; i--) cout << ans[i];
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3544kb
input:
2 2 11 11 1 1 2 2
output:
RDRLRDURLLRDDRLLRUDRLRDR
result:
wrong answer End Point Is (1,1), Not (er = 2, ec = 2)