QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#316100#7751. Palindrome Pathucup-team2279WA 0ms3624kbC++141.3kb2024-01-27 17:21:572024-01-27 17:21:57

Judging History

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

  • [2024-01-27 17:21:57]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3624kb
  • [2024-01-27 17:21:57]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N=32,dx[]={0,0,1,-1},dy[]={1,-1,0,0};
int n,m,cnt,sx,sy,ex,ey,b[N][N],vis[N][N];
string str="RLDU",ans;
void dfs1(int x,int y){
	if(!b[x][y]||vis[x][y]) return;
	vis[x][y]=1;cnt--;
	for(int i:{0,1,2,3}) dfs1(x+dx[i],y+dy[i]);
}
void upd(int o){
	if(!b[ex+dx[o]][ey+dy[o]]) ans+=str[o];
	else if(b[ex-dx[o]][ey-dy[o]]) ans+=str[o],ex-=dx[o],ey-=dy[o];
	else for(int i=1;;i++){
		if(!b[ex+dx[o]*i][ey+dy[o]*i]){
			ans+=string(i-1,str[o^1])+string(i,str[o]);
			break;
		}
		if(b[sx-dx[o]*i][sy-dy[o]*i]){
			ans+=string(i,str[o^1])+string(i,str[o]);
			break;
		}
	}
	sx+=dx[o];sy+=dy[o];
}
void dfs2(int op){
	vis[sx][sy]=1;
	if(op&&abs(sx-ex)+abs(sy-ey)<2){
		cout<<ans;
		if(sy+1==ey) cout<<'R';
		if(sy-1==ey) cout<<'L';
		if(sx+1==ex) cout<<'D';
		if(sx-1==ex) cout<<'U';
		reverse(begin(ans),end(ans));
		cout<<ans;
		exit(0);
	}
	for(int i:{0,1,2,3}){
		int x=sx+dx[i],y=sy+dy[i];
		if(!b[x][y]||vis[x][y]) continue;
		upd(i);dfs2(op);upd(i^1);
	}
}
int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		string s;
		cin>>s;
		for(int j=0;j<m;j++) cnt+=b[i][j+1]=s[j]-'0';
	}
	cin>>sx>>sy>>ex>>ey;
	dfs1(sx,sy);
	if(cnt){
		cout<<-1;
		return 0;
	}
	memset(vis,0,sizeof vis);
	dfs2(0);
	memset(vis,0,sizeof vis);
	dfs2(1);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3584kb

input:

2 2
11
11
1 1 2 2

output:

RDRLLRDUURLLRDRLLRUUDRLLRDR

result:

ok Valid Solution (Length = 27).

Test #2:

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

input:

2 2
10
01
1 1 2 2

output:

-1

result:

ok No Solution.

Test #3:

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

input:

1 1
1
1 1 1 1

output:


result:

ok Valid Solution (Length = 0).

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 3600kb

input:

5 4
1111
1111
1111
1111
1111
4 2 4 2

output:

RLRDLLLUULLLRRRRLRLRDULLLDULLLRRRRLRLRLLLDLLLRRRRLRLRDLLLDDLLLRRRRLRLRDDDDUUUUULLLRLRDLLLLLDRLRLLLUUUUUDDDDRLRLRRRRLLLDDLLLDRLRLRRRRLLLDLLLRLRLRRRRLLLUDLLLUDRLRLRRRRLLLUULLLDRLR

result:

wrong answer (2,3) Not Visited