QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#394447#7751. Palindrome PathLiWenXWA 1ms3852kbC++203.1kb2024-04-20 14:51:152024-04-20 14:51:15

Judging History

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

  • [2024-04-20 14:51:15]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3852kb
  • [2024-04-20 14:51:15]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int n,m;
char ch[35][35];
int id(int x,int y){
	return (x-1)*m+y;
}
int fa[1001];
int find(int x){
	if(x==fa[x]) return x;
	return fa[x]=find(fa[x]);
}
void merge(int x,int y){
	x=find(x),y=find(y);
	if(x==y) return ;
	fa[x]=y;
}
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int f[35][35][4];
vector<char> ans;
int sx,sy,tx,ty;
void push(int flag,int p){
	while(p--){
		if(flag==0) ans.push_back('R');
		if(flag==1) ans.push_back('L');
		if(flag==2) ans.push_back('D');
		if(flag==3) ans.push_back('U');
	}
}
void move(int x,int y,int flag){
	int k=f[x][y][flag^1];
	int p=f[tx][ty][flag];
//	cout<<k<<" "<<p<<'\n';
	if(k<p) push(flag^1,k+1),push(flag,k+1);
	else push(flag^1,p),push(flag,p+1);
}
void move(int x,int y,int X,int Y){
//	cout<<x<<' '<<y<<' '<<X<<' '<<Y<<'\n';
	if(x==X&&y==Y) return ;
	if(Y==y+1) move(x,y,0);
	if(Y==y-1) move(x,y,1);
	if(X==x+1) move(x,y,2);
	move(x,y,3);
}
bool vis[35][35];
void dfs(int x,int y,int X,int Y){
	vis[x][y]=1;
	for(int i=0;i<4;i++){
		int u=x+dx[i],v=y+dy[i];
		if(u<1||u>n||v<1||v>m) continue;
		if(ch[u][v]=='0') continue;
		if(vis[u][v]) continue;
		vis[u][v]=1;
		move(x,y,u,v);
		dfs(u,v,x,y);
	}
	move(x,y,X,Y); 
}
vector<pair<int,int> > vec;
bool flag;
void find(int x,int y){
	vis[x][y]=1;
	if(flag) return ;
	vec.push_back(make_pair(x,y));
	if(x==tx&&y==ty){
		flag=1;
		return ;
	}
	for(int i=0;i<4;i++){
		int u=x+dx[i],v=y+dy[i];
		if(u<1||u>n||v<1||v>m) continue;
		if(ch[u][v]=='0') continue;
		if(vis[u][v]) continue;
		vis[u][v]=1;
		find(u,v);
	}
	if(flag) return ;
	vec.pop_back();
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			fa[id(i,j)]=id(i,j);
			cin>>ch[i][j];
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(ch[i][j]=='0') continue;
			for(int k=0;k<4;k++){
				int x=i+dx[k];
				int y=j+dy[k];
				if(x<1||x>n||y<1||y>m) continue;
				if(ch[x][y]=='0') continue;
				merge(id(i,j),id(x,y));
			}
		}
	}
	cin>>sx>>sy>>tx>>ty;
	if(ch[sx][sy]=='0'||ch[tx][ty]=='0'){
		cout<<-1;return 0;
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(ch[i][j]=='0') continue;
			if(find(id(i,j))!=find(id(sx,sy))){
				cout<<-1;return 0;
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(ch[i][j]=='0') continue;
			int s=0;
			for(int k=j+1;k<=m;k++){
				if(ch[i][k]=='0') break;
				s++;
			}
			f[i][j][0]=s;
			s=0;
			for(int k=j-1;k;k--){
				if(ch[i][k]=='0') break;
				s++;
			}
			f[i][j][1]=s;
			s=0;
			for(int k=i+1;k<=n;k++){
				if(ch[k][j]=='0') break;
				s++;
			}
			f[i][j][2]=s;
			s=0;
			for(int k=i-1;k;k--){
				if(ch[k][j]=='0') break;
				s++;
			}
			f[i][j][3]=s;
		}
	}
//	cout<<f[3][2][3]<<'\n';
//	move(4,3,3,3);
	for(char u:ans) cout<<u;
	dfs(sx,sy,sx,sy);
	memset(vis,0,sizeof(vis));
	find(sx,sy);
	for(int i=1;i<(int)vec.size();i++){
		move(vec[i-1].first,vec[i-1].second,vec[i].first,vec[i].second);
	}
	stack<char> s;
	for(char u:ans){
		cout<<u;s.push(u);
	}
//	cout<<'\n';
	while(!s.empty()){
		cout<<s.top();s.pop();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3852kb

input:

2 2
11
11
1 1 2 2

output:

RDUUDDUURLDURDUDURLDUURDUUDDUUUUDDUUDRUUDLRUDUDRUDLRUUDDUUDR

result:

ok Valid Solution (Length = 60).

Test #2:

score: 0
Accepted
time: 1ms
memory: 3492kb

input:

2 2
10
01
1 1 2 2

output:

-1

result:

ok No Solution.

Test #3:

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

input:

1 1
1
1 1 1 1

output:


result:

ok Valid Solution (Length = 0).

Test #4:

score: 0
Accepted
time: 1ms
memory: 3608kb

input:

5 4
1111
1111
1111
1111
1111
4 2 4 2

output:

LLRRDDUULLRRRDDUUUDDDDUURLDURLLDURLLDUDUDDUULRDDDUUULLRRDDDUUULLRRRDDDUUUDDDUUURLDDDUUUURLLDDDUUUURLLDDDUUUUDDDUUUULRDDDUUUULLRRDDDUUUULLRRRDDDUUUURLDDDUUUURLLDDDUUUURLLDDDUUUUUDDDDUUUULRDDDUUUULLRRDDDUUUULLRRRDDDUUUUUDDDDDUUUURLDDDUUURLLDDDUUURLLDDDUUUUDDDDDUUUUDDDDUULRDULLRRDULLRRRDUDURLDDUURLLDDU...

result:

ok Valid Solution (Length = 602).

Test #5:

score: 0
Accepted
time: 1ms
memory: 3648kb

input:

5 5
11111
10101
11111
10101
11111
1 4 5 5

output:

RDUDDDDDUUUUUDDDDDUUUURLDDDUUURRLLDURRRLLLDDDUUURRRRLLLLDUDDDDUUUDDDUURDURDURDURDUDUDDDUURLDURRLLDUDUDDDUURRRLLLDURRRRLLLLDUDUDDUUDDDUUUDDDDUUUURDDDDUUUUURDUDDDDDUUUUUDDDDUUUURRRLLLDDDDUUUUURRRRLLLLDUDDDDDUUUUUDDDDDUUUURDDDUUURDURDDDUUURDUDDDUUUDDDDUUUURLDDDDUUUUURDUDDDDDUUUUUDDDDDUUUURLDDDUUURRLLDU...

result:

ok Valid Solution (Length = 692).

Test #6:

score: -100
Wrong Answer
time: 1ms
memory: 3620kb

input:

5 3
111
100
111
001
111
4 3 3 2

output:

DURLURLLULRULRRUUURLURLLUUULRULRRURLURLLUDUDULRULRRUDUURLUULRUUDURRLURLUDUDULLRULRURRLURLUUULLRULRUUURRLURLULLRULRUD

result:

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