QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#479143#7751. Palindrome Pathucup-team052#WA 11ms62628kbC++143.6kb2024-07-15 15:23:202024-07-15 15:23:21

Judging History

你现在查看的是最新测评结果

  • [2024-07-15 15:23:21]
  • 评测
  • 测评结果:WA
  • 用时:11ms
  • 内存:62628kb
  • [2024-07-15 15:23:20]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const char dc[4] = {'R', 'L', 'D', 'U'};

const int N = 905;

struct atom {
	int x, y, d;
	atom (int a = 0, int b = 0, int c = 0) : x(a), y(b), d(c) {}
};

char c[35][35];
queue < pair <int, int> > q;
vector <atom> adj[N][N], radj[N][N];
vector <int> ans;
int frx[N][N], fry[N][N], fr[N][N], vis[N][N];
int frx2[N][N], fry2[N][N], fr2[N][N], vis2[N][N], vis3[N][N];
int n, m, sx, sy, tx, ty;

inline int s(int x, int y) { return (x - 1) * m + y; }

int seq[N * N], len;
void solve(int x, int y) {
	if (!frx[x][y]) return;
	solve(frx[x][y], fry[x][y]);
	seq[++len] = fr[x][y];
}

void solve2(int x, int y) {
	if (!frx2[x][y]) return;
	seq[++len] = fr2[x][y];
	solve2(frx2[x][y], fry2[x][y]);
}

void go(int &x, int &y, int d) {
	int tx = x + dx[d], ty = y + dy[d];
	if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && c[tx][ty] == '1') x = tx, y = ty;
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%s", c[i] + 1);
	scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
	q.push(make_pair(s(sx, sy), s(tx, ty))); vis[s(sx, sy)][s(tx, ty)] = 1;
	while (!q.empty()) {
		pair <int, int> t = q.front(); q.pop();
		int ux1 = (t.first - 1) / m + 1, uy1 = (t.first - 1) % m + 1;
		int ux2 = (t.second - 1) / m + 1, uy2 = (t.second - 1) % m + 1;
		for (int d = 0; d < 4; d++) {
			for (int i = 0; i <= 1; i++) {
				int x1, y1, x2, y2;
				x1 = ux1 + dx[d]; y1 = uy1 + dy[d];
				if (x1 < 1 || x1 > n || y1 < 1 || y1 > m || c[x1][y1] == '0') x1 = ux1, y1 = uy1;
				if (i) {
					x2 = ux2 + dx[d ^ 1], y2 = uy2 + dy[d ^ 1];
					if (x2 < 1 || x2 > n || y2 < 1 || y2 > m || c[x2][y2] == '0') continue;
				} else {
					x2 = ux2 + dx[d], y2 = uy2 + dy[d];
					if (x2 >= 1 && x2 <= n && y2 >= 1 && y2 <= m && c[x2][y2] == '1') continue;
					x2 = ux2; y2 = uy2;
				}
				int t1 = s(x1, y1), t2 = s(x2, y2);
				adj[t.first][t.second].push_back(atom(t1, t2, d));
				radj[t1][t2].push_back(atom(t.first, t.second, d));
				if (vis[t1][t2]) continue;
				vis[t1][t2] = 1; frx[t1][t2] = t.first; fry[t1][t2] = t.second; fr[t1][t2] = d; q.push(make_pair(t1, t2));
			}
		}
	}
	q.push(make_pair(s(sx, sy), s(tx, ty))); vis2[s(sx, sy)][s(tx, ty)] = 1;
	while (!q.empty()) {
		pair <int, int> t = q.front(); q.pop();
		for (auto v : radj[t.first][t.second]) {
			int t1 = v.x, t2 = v.y;
			if (vis2[t1][t2]) continue;
			vis2[t1][t2] = 1; frx2[t1][t2] = t.first; fry2[t1][t2] = t.second; fr2[t1][t2] = v.d; q.push(make_pair(t1, t2));
		}
	}
	vis3[sx][sy] = 1;
	int nx = sx, ny = sy;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (c[i][j] == '0' || vis3[i][j]) continue;
			int tt = s(i, j);
			for (int k = 1; k <= n * m; k++) {
				if (vis[tt][k] && vis2[tt][k]) {
					len = 0;
					solve(tt, k); solve2(tt, k);
					for (int t = 1; t <= len; t++) {
						ans.push_back(seq[t]);
						go(nx, ny, seq[t]); vis3[nx][ny] = 1;
					}
					assert(nx == sx && ny == sy);
					break;
				}
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			int t = s(i, j);
			if (vis[t][t]) {
				solve(t, t);
				for (int k = 1; k <= len; k++) ans.push_back(seq[k]);
				for (auto i : ans) printf("%c", dc[i]), go(nx, ny, i);
				reverse(ans.begin(), ans.end());
				for (auto i : ans) printf("%c", dc[i]), go(nx, ny, i);
				assert(nx == tx && ny == ty);
				// for (int k = len; k >= 1; k--) printf("%c", dc[seq[k]]);
				return 0;
			}

		}
	}
	printf("-1\n");
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 11ms
memory: 61976kb

input:

2 2
11
11
1 1 2 2

output:

RLDLURDULRDULRLDUUDLRLUDRLUDRULDLR

result:

ok Valid Solution (Length = 34).

Test #2:

score: 0
Accepted
time: 0ms
memory: 49356kb

input:

2 2
10
01
1 1 2 2

output:

-1

result:

ok No Solution.

Test #3:

score: 0
Accepted
time: 4ms
memory: 49868kb

input:

1 1
1
1 1 1 1

output:


result:

ok Valid Solution (Length = 0).

Test #4:

score: -100
Wrong Answer
time: 7ms
memory: 62628kb

input:

5 4
1111
1111
1111
1111
1111
4 2 4 2

output:

RRRLLLLDUULLLLRRUUDRRRRLLLLUUDDDULLLLRRURRRRLLLUUDDDULLLLRRURRRRLLUUDDDULLLLRRURRRRLDUULLLLRRUUDRRRRLUUDDDULLLLRRURRRRDUULLLLRRUUDRRRRUUDDDULLLLRRURRRRUUDDDULLLLRRURRRRLLLLDUUUUDLLLLRRRRURRLLLLUDDDUURRRRURRLLLLUDDDUURRRRDUURRLLLLUUDRRRRURRLLLLUDDDUULRRRRDUURRLLLLUUDLRRRRURRLLLLUDDDUULLRRRRURRLLLLUDD...

result:

wrong answer End Point Is (2,4), Not (er = 4, ec = 2)